Migration from Twig to Latte

Are you migrating a project written in Twig to the more modern Latte? We have a tool to make the migration easier. Try it out online.

You can download the tool from GitHub or install it using Composer:

composer create-project latte/tools

The converter doesn't use simple regular expression substitutions, instead it uses the Twig parser directly, so it can handle any complex syntax.

A script twig-to-latte.php is used to convert from Twig to Latte:

twig-to-latte.php input.twig.html [output.latte]

Conversion

The conversion requires manual editing of the result, since the conversion cannot be done unambiguously. Twig uses dot syntax, where {{ a.b }} can mean $a->b, $a['b'] or $a->getB(), which cannot be distinguished during compilation. The converter therefore converts everything to $a->b.

Some functions, filters or tags have no equivalent in Latte, or may behave slightly differently.

Example

The input file might look like this:

{% use "blocks.twig" %}
<!DOCTYPE html>
<html>
	<head>
		<title>{{ block("title") }}</title>
	</head>
	<body>
		<h1>{% block title %}My Web{% endblock %}</h1>
		<ul id="navigation">
		{% for item in navigation %}
			{% if not item.active %}
				<li>{{ item.caption }}</li>
			{% else %}
				<li><a href="{{ item.href }}">{{ item.caption }}</a></li>
			{% endif %}
		{% endfor %}
		</ul>
	</body>
</html>

After converting to Latte, we get this template:

{import 'blocks.latte'}
<!DOCTYPE html>
<html>
	<head>
		<title>{include title}</title>
	</head>
	<body>
		<h1>{block title}My Web{/block}</h1>
		<ul id="navigation">
		{foreach $navigation as $item}
			{if !$item->active}
				<li>{$item->caption}</li>
			{else}
				<li><a href="{$item->href}">{$item->caption}</a></li>
			{/if}
		{/foreach}
		</ul>
	</body>
</html>
version: 3.0