twig模板获取全局变量的方法_PHP

WBOY
Release: 2016-05-27 10:37:28
Original
956 people have browsed it

本文实例讲述了twig模板获取全局变量的方法。分享给大家供大家参考,具体如下:

简单介绍下,twig是一模板引擎,可以简化php的编写过程,提高效率。

而Symfony模板里,就自带了twig这一模板引擎。

twig的语法用的多的,也就三种,也是比较容易的

1、{{..}},这个在twig里是代表输出变量的意思,举个例子:

{{ id }}

Copy after login

2、{%…%},这个在twig里代表的是进行判断,举个例子(取自twig官方文档示例):

判断值是否为真:

{% if online == false %}
  <p>Our website is in maintenance mode. Please, come back later.</p>
{%endif%}

Copy after login

判断值是否为空:

{% if users %}
  <ul>
   {% for user in users %}
    <li>{{ user.username|e }}</li>
   {% endfor %}
  </ul>
{% endif %}

Copy after login

判断能否找到这个值:

{% if users is defined %}
  <li>{{ user.username|e }}</li>
{% endif %}

Copy after login

3、{#…#},这个事注释的意思

大概的语法介绍完了,然后就是这个如何用twig输出全局变量。
搜了半天都没找到,最后翻墙找到了,下面上代码

{{ app.session.get('varname') }}

Copy after login

上面的varname 就是需要输出的session数组里对应的值的名字。

目前还在继续学习twig这个模板引擎。欢迎指正,补充。

本文永久地址:http://blog.it985.com/6823.html
本文出自 IT985博客 ,转载时请注明出处及相应链接。

更多关于PHP模板相关内容感兴趣的读者可查看本站专题:《PHP模板技术总结》、《smarty模板入门基础教程》、《codeigniter入门教程》及《ThinkPHP入门教程》

希望本文所述对大家PHP程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!