Echo variables and math
{% set name = 'Hello Buddy' %}
{{ "Hi #{name}, how are you today" }}
{{ "A math operation = #{(1 + 2) * (4 * 5)}" }}
{% for user in users %}
<li>{{ user.username|e }}</li>
{% else %}
<li><em>no users available</em></li>
{% endfor %}
{% block toolbar %}
{# The content of the base.html.twig will be retrieved and printed in this inherited view too#}
{{ parent() }}
<a href="path_to_something">
User Normal Action 3
</a>
<a href="path_to_something">
User Normal Action 4
</a>
{% endblock %}
{% set data = {
"Hey": "Ho",
"What": 12,
"Value" : true
}%}
{{ data|json_encode()|raw }}
{{ data|json_encode(constant('JSON_PRETTY_PRINT'))}}
class StarterSite extends TimberSite {
function add_to_twig($twig){
/* this is where you can add your own fuctions to twig */
$twig->addExtension(new Twig_Extension_StringLoader());
$function = new Twig_SimpleFunction('my_twig)function', function ($my_var) {
// do your stuff here with $my_var
});
$twig->addFunction($function);
return $twig;
}
}
new StarterSite();
Collected from: