Home  >  Article  >  Backend Development  >  How to obtain global variables in twig template, twig template global variables_PHP tutorial

How to obtain global variables in twig template, twig template global variables_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:59:13934browse

How twig template obtains global variables, twig template global variables

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.

Articles you may be interested in:

  • How to use js variables in twig
  • A summary of examples of common statements in twig templates
  • Introductory tutorial on how to use the Twig template engine
  • Example of using PHP template engine Twig in Yii framework

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1099082.htmlTechArticleTwig template method to obtain global variables, twig template global variables This article describes the method of twig template to obtain global variables. Share it with everyone for your reference, the details are as follows: Brief introduction...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn