Twig is a modern template engine for PHP. It's used by popular frameworks like Symfony and Drupal.
The following Handlebars to Twig conversions are currently supported:
hbs input:
<div class="entry">
{{!-- comment 1 --}}
{{! comment 2 }}
inline {{! comment 3 }} test
<!-- html comment -->
</div>
htl output:
<div class="entry">
{# comment 1 #}
{# comment 2 #}
inline {# comment 3 #} test
<!-- html comment -->
</div>
hbs input:
<h1>{{ foo }}</h1>
<h1>{{ foo.bar }}</h1>
htl output:
<h1>{{ foo }}</h1>
<h1>{{ foo.bar }}</h1>
hbs input:
{{#each users }}
{{ name }}
{{#each comments }}
{{ ../name }} {{title}}
{{#each commenter }}
{{../../name}} {{name}}
{{/each}}
{{/each}}
{{/each}}
htl output:
{% for users_i in users %}
{{ users_i.name }}
{% for comments_i in users_i.comments %}
{{ users_i.name }} {{ comments_i.title }}
{% for commenter_i in comments_i.commenter %}
{{ users_i.name }} {{ commenter_i.name }}
{% endfor %}
{% endfor %}
{% endfor %}
hbs input:
{{ escaped }}
{{{ notEscaped }}}
htl output:
{{ escaped }}
{{ notEscaped|raw }}
hbs input:
{{#if foo }}
Foo
{{/if}}
htl output:
{% if foo %}
Foo
{% endif %}
hbs input:
{{#if foo as isFoo }}
Foo
{{else}}
Bar
{{/if}}
htl output:
{% if foo %}
Foo
{% else %}
Bar
{% endif %}
hbs input:
{{#if foo }}
Foo
{{else if bar }}
Bar
{{/if}}
htl output:
{% if foo %}
Foo
{% elif bar %}
Bar
{% endif %}
hbs input:
{{#if foo }}
Foo
{{else}}
{{# if bar }}
Bar
{{/if}}
Baz
{{/if}}
htl output:
{% if foo %}
Foo
{% else %}
{% if bar %}
Bar
{% endif %}
Baz
{% endif %}
hbs input:
{{#if foo }}
Foo
{{else if bar }}
Bar
{{else}}
Foobar
{{/if}}
htl output:
{% if foo %}
Foo
{% elif bar %}
Bar
{% else %}
Foobar
{% endif %}
hbs input:
{{#each users }}
<a href="{{ foobar }}">link</a>
{{/each}}
htl output:
{% for users_i in users %}
<a href="{{ users_i.foobar }}">link</a>
{% endfor %}
hbs input:
{{#each users }}
<a href="{{ foobar }}">{{ this }}</a>
{{/each}}
htl output:
{% for users_i in users %}
<a href="{{ users_i.foobar }}">{{ users_i }}</a>
{% endfor %}
hbs input:
{{#each users }}
{{ @index }}: {{ this }}
{{/each}}
htl output:
{% for users_i in users %}
{{ loop.index0 }}: {{ users_i }}
{% endfor %}
hbs input:
{{#each map as |value key| }}
{{ key }}: {{ value }}
{{/each}}
htl output:
{% for key, value in map|cast_to_array %}
{{ key }}: {{ value }}
{% endfor %}
hbs input:
{{#each map }}
{{ @key }}: {{ this }}
{{/each}}
htl output:
{% for key, map_i in map|cast_to_array %}
{{ key }}: {{ map_i }}
{% endfor %}
hbs input:
{{#each users as |user| }}
{{ user.name }}
{{#each comments as |comment| }}
{{ user.name }} {{comment.title}}
{{/each}}
{{/each}}
htl output:
{% for user in users %}
{{ user.name }}
{% for comment in user.comments %}
{{ user.name }} {{ comment.title }}
{% endfor %}
{% endfor %}
hbs input:
{{#each users }}
{{#if foo }}
{{#each comments }}
{{#if title}}
<a href="{{ id }}">{{ name }}</a>
{{/if}}
{{/each}}
{{else}}
{{#each comments }}
{{id}}
{{/each}}
{{/if}}
{{/each}}
htl output:
{% for users_i in users %}
{% if users_i.foo %}
{% for comments_i in users_i.comments %}
{% if comments_i.title %}
<a href="{{ comments_i.id }}">{{ comments_i.name }}</a>
{% endif %}
{% endfor %}
{% else %}
{% for comments_i in users_i.comments %}
{{ comments_i.id }}
{% endfor %}
{% endif %}
{% endfor %}
hbs input:
{{#each users }}
<a href="{{ id }}">{{ name }}</a>
{{#each comments }}
{{ name }}
{{/each}}
{{/each}}
htl output:
{% for users_i in users %}
<a href="{{ users_i.id }}">{{ users_i.name }}</a>
{% for comments_i in users_i.comments %}
{{ comments_i.name }}
{% endfor %}
{% endfor %}
hbs input:
{{> foo/bar.hbs }}
htl output:
{% include 'foo/bar.html' %}
hbs input:
{{! this is not supported in django }}
{{> myPartial myOtherContext }}
htl output:
{# this is not supported in django #}
{% include 'myPartial.html' with myOtherContext %}
hbs input:
{{> (lookup . 'myVariable') }}
htl output:
{% include myVariable %}
hbs input:
{{> myPartial name=firstName age=18 foo="bar" baz=true }}
htl output:
{% include 'myPartial.html' with {'name':firstName, 'age':18, 'foo':"bar", 'baz':true} %}