教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 twig获取全局变量使用例子

twig获取全局变量使用例子

发布时间:2017-12-11   编辑:jiaochengji.com
教程集为您提供twig获取全局变量使用例子等资源,欢迎您收藏本站,我们将为您提供最新的twig获取全局变量使用例子资源
twig获取全局变量对于新手来讲还是非常的陌生了,如果各位不知道如何在twig中使用全局变量的话可以和一聚教程小编来看看喽。

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

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

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

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5415')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5415>

{{ id }}

</td></tr></table>

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1268')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1268>

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

</td></tr></table>

判断值是否为空:

 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3004')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3004>{% if users %}
   <ul>
     {% for user in users %}
       <li>{{ user.username|e }}</li>
     {% endfor %}
   </ul>
{% endif %}</td></tr></table>

判断能否找到这个值:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1060')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1060>{% if users is defined %}
    <li>{{ user.username|e }}</li>
{% endif %}
</td></tr></table>

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

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

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4289')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4289>

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

</td></tr></table>

上面的varname 就是需要输出的session数组里对应的值的名字。
目前还在继续学习twig这个模板引擎。欢迎指正,补充。

您可能感兴趣的文章:
twig获取全局变量使用例子
php global static与$GLOBALS的区别
Twig中的for循环使用方法
php全局变量global的使用示例
Python变量作用域
PHP获取域名的几个全局变量
谈谈PHP中的global全局变量
Javascript声明全局变量的三种方法
golang 全局变量
php $$特殊变量(输出变量字符串对应变量的值)

[关闭]
~ ~