Home > Article > Backend Development > TWIG Template Design Quick Start Manual Chinese_PHP Tutorial
Written several articles about twig. . I haven’t even written a quick start or anything like that. Write now
Summary
The template of twig is an ordinary text file and does not require a special extension. .html, .htm or .twig are all acceptable.
Variables and expressions within the template will be parsed and replaced at runtime, and tags will control the logic of the template
Below is a minimal template to illustrate some basic things
{% for i in 0..3 %}
{{ i }},
{% endfor %}
{% for i in 0..3 %}
{{ i }},
{% endfor %}| Use a filter
{# output will be HELLO #}
{{ "hello"|upper }}
{# output will be HELLO #}
{{ "hello"|upper }}~ Force string concatenation
{{ "Hello " ~ name ~ "!" }}
{{ "Hello " ~ name ~ "!" }}?: Ternary operator
{{ foo ? 'yes' : 'no' }}
{{ foo ? 'yes' : 'no' }}. [] Gets the properties of an object. For example, the following are equal.
{{ foo.bar }}
{{ foo['bar'] }}
{{ foo.bar }}
{{ foo['bar'] }}
You can also insert an expression inside a string, usually a variable. The format is #{expression}
{{ "foo #{bar} baz" }}
{{ "foo #{1 + 2} baz" }}
{{ "foo #{bar} baz" }}
{{ "foo #{1 + 2} baz" }}
White space control
Like php, the first newline character after the TWIG template tag will be automatically deleted, and the remaining whitespace (including spaces, tabs, newlines, etc.) will be output as is.
Use the spaceless tag to remove the white space between these HTML tags
{% spaceless %}
{% endspaceless %}
{# output will befoo#}
{% spaceless %}
{% endspaceless %}
{# output will befoo#} Using the - operator, you can easily delete the space between the TWIG tag before or after it and the html tag.{% set value = 'no spaces' %}
{% set value = 'no spaces' %}
{#- No leading/trailing whitespace -#}
{%- if true -%}
{{- value -}}
{%- endif -%}
{# output 'no spaces' #}
{% set value = 'no spaces' %}
{#- No leading/trailing whitespace -#}
{%- if true -%}
{{- value -}}
{%- endif -%}
{# output 'no spaces' #}
End, if you insist on reading this, congratulate yourself, you have mastered some more knowledge, congratulations
Excerpted from jiaochangyun’s column
http://www.bkjia.com/PHPjc/478452.html