Skip to content

Instantly share code, notes, and snippets.

@james2doyle
Created December 24, 2019 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save james2doyle/4af1671173f19a5ec82cf7d8179a95d5 to your computer and use it in GitHub Desktop.
Save james2doyle/4af1671173f19a5ec82cf7d8179a95d5 to your computer and use it in GitHub Desktop.
An example file of most of the things you can do in Twig. Used for building syntax highlighting
{#
This is the base template used as the application layout which contains the
common elements and decorates all the other templates.
See https://symfony.com/doc/current/book/templating.html#template-inheritance-and-layouts
#}
{% do 1 + 2 %}
{{ attribute(foo, 'data-foo') }}
{% set foo = 'foo' %}
{% set foo = [1, 2] %}
{% set foo = {'foo': 'bar'} %}
{{ elem|u.snake|u.wordwrap(5)|u.camel.title|u.truncate(8, '...') }}
{{ greeting ~ name|lower|date('Y-m-d', 'Europe/Paris') }} {# Hello fabien
{# use parenthesis to change precedence
{{ (greeting ~ name)|lower }} {# hello fabien #}
{# keys as expressions (the expression must be enclosed into parentheses) #}
{% set foo = 'foo' %}
{{ (foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz' }}
{% set foo = [1, {"foo": "bar"}] %}
{% if 'Fabien' starts with 'F' %}{% endif %}
{% if 'Fabien' ends with 'n' %}{% endif %}
{# returns true #}
{{ 1 in [1, 2, 3] }}
{{ 'cd' in 'abcde' }}
{% if 1 not in [1, 2, 3] %}
{# is equivalent to #}
{% if not (1 in [1, 2, 3]) %}
{# find out if a variable is odd #}
{{ name is odd }}
{% if post.status is constant('Post::PUBLISHED') %}
{% if post.status is not constant('Post::PUBLISHED') %}
{% if post.status is not null %}
{% if loop.index is divisible by(3) %}
{{ list|join(', ') }}
{{ name|striptags|title }}
{% endif %}
{% block title-special %}
Index
{% endblock %}
{% apply upper %}
This text becomes uppercase
{% endapply %}
{% for i in range(0, 3) %}
{{ i }},
{% endfor %}
{% for i in range(low=1, high=10, step=2) %}
{{ i }},
{% endfor %}
{{ data|convert_encoding('UTF-8', 'iso-2022-jp') }}
{# versus #}
{{ data|convert_encoding(from='iso-2022-jp', to='UTF-8') }}
{# the first argument is the date format, which defaults to the global date format if null is passed #}
{{ "now"|date(null, "Europe/Paris") }}
{# or skip the format value by using a named argument for the time zone #}
{{ "now"|date(timezone="Europe/Paris") }}
{{ "now"|date('d/m/Y H:i', timezone="Europe/Paris") }}
{% if users|length > 0 %}
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
<li>{{ user.username|u }}</li>
{% endfor %}
</ul>
{% endif %}
{{ include('sidebar.html') }}
{% for box in boxes %}
{{ include('render_box.html') }}
{% endfor %}
{{ include('sections/articles/sidebar.html') }}
{% extends "base.html" %}
{% block head %}
{{ parent() }}
<style type="text/css">
.important { color: #336699; }
</style>
{% endblock %}
{% block content %}
<h1>Index</h1>
<p class="important">
Welcome to my awesome homepage.
</p>
{% endblock %}
{% block sidebar %}
<h3>Table Of Contents</h3>
...
{{ parent() }}
{% endblock %}
{{ user.username|e }}
{{ user.username|e('js') }}
{{ user.username|e('css') }}
{{ user.username|e('url') }}
{{ user.username|e('html_attr') }}
{% autoescape %}
Everything will be automatically escaped in this block (using the HTML strategy)
{% endautoescape %}
{% autoescape 'js' %}
Everything will be automatically escaped in this block (using the JS strategy)
{% endautoescape %}
{{ '{{' }}
{% set greeting = 'Hello ' %}
{% set name = 'Fabien' %}
{{ greeting ~ name|lower }} {# Hello fabien #}
{# use parenthesis to change precedence #}
{{ (greeting ~ name)|lower }} {# hello fabien #}
{# keys as expressions (the expression must be enclosed into parentheses) #}
{% set foo = 'foo' %}
{{ (foo): 'foo', (1 + 1): 'bar', (foo ~ 'b'): 'baz' }}
{% set foo = [1, {"foo": "bar"}] %}
{% if 'Fabien' starts with 'F' %}{% endif %}
{% if 'Fabien' ends with 'n' %}{% endif %}
{# returns true #}
{{ 1 in [1, 2, 3] }}
{{ 'cd' in 'abcde' }}
{% if item is true or thing is false %}
content
{% endif %}
{% if 1 not in [1, 2, 3] %}
{# is equivalent to #}
{% if not (1 in [1, 2, 3]) %}
{# find out if a variable is odd #}
{{ name is odd }}
{{ name is even }}
{{ name is true }}
{% if post.status is constant('Post::PUBLISHED') %}
{% if post.status is not constant('Post::PUBLISHED') %}
{% if post.status is not null %}
{% if phone matches '/^[\\d\\.]+$/' %}{% endif %}
{# is equivalent to #}
{% if not (post.status is constant('Post::PUBLISHED')) %}
{{ 1..5 }}
{% block hey %}
{% deprecated 'The "hey" block is deprecated, use "greet" instead.' %}
{{ block('greet') }}
{% endblock %}
{% block greet %}
Hey you!
{% endblock %}
{# equivalent to #}
{{ range(1, 5) }}
{{ (1..5)|join(', ') }}
{{ foo ? 'yes' : 'no' }}
{{ foo ?: 'no' }} is the same as {{ foo ? foo : 'no' }}
{{ foo ? 'yes' }} is the same as {{ foo ? 'yes' : '' }}
{# returns the value of foo if it is defined and not null, 'no' otherwise #}
{{ foo ?? 'no' }}
{{ "foo #{bar} baz" }}
{{ "foo #{1 + 2} baz" }}
{% set value = 'no spaces' %}
{#- No leading/trailing whitespace -#}
{%- if true -%}
{{- value -}}
{%- endif -%}
{# output 'no spaces' #}
<li>
{{ value }} </li>
{# outputs '<li>\n no spaces </li>' #}
<li>
{{- value }} </li>
{# outputs '<li>no spaces </li>' #}
<li>
{{~ value }} </li>
{# outputs '<li>\nno spaces </li>' #}
{% apply spaceless %}
<div>
<strong>foo bar</strong>
</div>
{% endapply %}
{# output will be <div><strong>foo bar</strong></div> #}
<!DOCTYPE html>
<html lang="{{ app.request.locale }}">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>{% block title %}Symfony Demo application{% endblock %}</title>
<link rel="alternate" type="application/rss+xml" title="{{ 'rss.title'|trans }}" href="{{ path('blog_rss') }}">
{% block stylesheets %}
{{ encore_entry_link_tags('app') }}
{% endblock %}
<link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
</head>
<body id="{% block body_id %}{% endblock %}">
{% block header %}
<header>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header col-md-3 col-lg-2">
<a class="navbar-brand" href="{{ path('homepage') }}">
Symfony Demo
</a>
<button type="button" class="navbar-toggle"
data-toggle="collapse"
data-target=".navbar-collapse">
<span class="sr-only">{{ 'menu.toggle_nav'|trans }}</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
{% block header_navigation_links %}
<li>
<a href="{{ path('blog_index') }}">
<i class="fa fa-home" aria-hidden="true"></i> {{ 'menu.homepage'|trans }}
</a>
</li>
{% if is_granted('ROLE_ADMIN') %}
<li>
<a href="{{ path('admin_post_index') }}">
<i class="fa fa-lock" aria-hidden="true"></i> {{ 'menu.admin'|trans }}
</a>
</li>
{% endif %}
{% endblock %}
<li>
<a href="{{ path('blog_search') }}"> <i class="fa fa-search"></i> {{ 'menu.search'|trans }}</a>
</li>
{% if app.user %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" id="user">
<i class="fa fa-user" aria-hidden="true"></i>
<span class="caret"></span>
<span class="sr-only">{{ app.user.fullname }}</span>
</a>
<ul class="dropdown-menu user" role="menu" aria-labelledby="user">
<li>
<a href="{{ path('user_edit') }}">
<i class="fa fa-edit" aria-hidden="true"></i> {{ 'menu.user'|trans }}
</a>
</li>
<li class="divider"></li>
<li>
<a href="{{ path('security_logout') }}">
<i class="fa fa-sign-out" aria-hidden="true"></i> {{ 'menu.logout'|trans }}
</a>
</li>
</ul>
</li>
{% endif %}
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" id="locales">
<i class="fa fa-globe" aria-hidden="true"></i>
<span class="caret"></span>
<span class="sr-only">{{ 'menu.choose_language'|trans }}</span>
</a>
<ul class="dropdown-menu locales" role="menu" aria-labelledby="locales">
{% for locale in locales() %}
<li {% if app.request.locale == locale.code %}aria-checked="true" class="active"{% else %}aria-checked="false"{% endif %} role="menuitem"><a href="{{ path(app.request.get('_route', 'blog_index'), app.request.get('_route_params', [])|merge({_locale: locale.code})) }}">{{ locale.name|capitalize }} <small>{{ locale.code[0:2] }}</small></a></li>
{% endfor %}
</ul>
</li>
</ul>
</div>
</div>
</div>
</header>
{% endblock %}
<div class="container body-container">
{% block body %}
<div class="row">
<div id="main" class="col-sm-9">
{{ include('default/_flash_messages.html.twig') }}
{% block main %}{% endblock %}
</div>
<div id="sidebar" class="col-sm-3">
{% block sidebar %}
{{ render_esi(controller('Symfony\\Bundle\\FrameworkBundle\\Controller\\TemplateController::templateAction', {
'template': 'blog/about.html.twig',
'sharedAge': 600,
'_locale': app.request.locale
})) }}
{% endblock %}
</div>
</div>
{% endblock %}
</div>
{% block footer %}
<footer>
<div class="container">
<div class="row">
<div id="footer-copyright" class="col-md-6">
<p>&copy; {{ 'now'|date('Y') }} - The Symfony Project</p>
<p>{{ 'mit_license'|trans }}</p>
</div>
<div id="footer-resources" class="col-md-6">
<p>
<a href="https://twitter.com/symfony" title="Symfony Twitter">
<i class="fa fa-twitter" aria-hidden="true"></i>
</a>
<a href="https://www.facebook.com/SensioLabs" title="SensioLabs Facebook">
<i class="fa fa-facebook" aria-hidden="true"></i>
</a>
<a href="https://symfony.com/blog/" title="Symfony Blog">
<i class="fa fa-rss" aria-hidden="true"></i>
</a>
</p>
</div>
</div>
</div>
</footer>
{% endblock %}
{% block javascripts %}
{{ encore_entry_script_tags('app') }}
{% endblock %}
{# it's not mandatory to set the timezone in localizeddate(). This is done to
avoid errors when the 'intl' PHP extension is not available and the application
is forced to use the limited "intl polyfill", which only supports UTC and GMT #}
<!-- Page rendered on {{ 'now'|localizeddate('long', 'long', null, 'UTC') }} -->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment