Home > Article > Backend Development > How to obtain global variables in twig template, twig template global variables_PHP tutorial
The example in this article describes the method of twig template obtaining global variables. Share it with everyone for your reference, the details are as follows:
A brief introduction, twig is a template engine that can simplify the writing process of PHP and improve efficiency.
The Symfony template comes with the twig template engine.
Twig’s syntax is widely used, there are only three types, and it is relatively easy
1. {{..}}, this represents the output variable in twig , for example:
{{ id }}
2. {%...%}, this represents judgment in twig , for example (taken from twig official document example):
Judge whether the value is true:
{% if online == false %} <p>Our website is in maintenance mode. Please, come back later.</p> {%endif%}
Judge whether the value is empty:
{% if users %} <ul> {% for user in users %} <li>{{ user.username|e }}</li> {% endfor %} </ul> {% endif %}
Determine whether this value can be found:
{% if users is defined %} <li>{{ user.username|e }}</li> {% endif %}
3. {#…#}, the meaning of this comment
The general syntax introduction is over, and then there is how to use twig to output global variables.
I searched for a long time but couldn’t find it. Finally I found it through the wall. Here is the code
{{ app.session.get('varname') }}
The varname above is the name of the corresponding value in the session array that needs to be output.
I am still continuing to learn the template engine twig. Corrections and additions are welcome.
The permanent address of this article: http://blog.it985.com/6823.html
This article comes from IT985 Blog. Please indicate the source and corresponding link when reprinting.
Readers who are interested in more content related to PHP templates can check out the special topics of this site: "Summary of PHP Template Technology", "Basic Tutorial for Getting Started with Smarty Templates", "Introductory Tutorial for Codeigniter" and "Introductory Tutorial for ThinkPHP"
I hope this article will be helpful to everyone in PHP programming.