搜索
首页php教程php手册Twig 的 tags学习(中文) 之二

Twig 的 tags学习(中文) 之二

Jun 13, 2016 am 10:46 AM
barsettwig中文变量学习标签赋值

set标签
主要是用来给变量赋值的。

 

 

{% set foo = 'foo' %} 
 
{% set foo = [1, 2] %} 
 
{% set foo = {'foo': 'bar'} %} 
 
{% set foo = 'foo' ~ 'bar' %} 
 
{% set foo, bar = 'foo', 'bar' %} 
{% set foo = 'foo' %}

{% set foo = [1, 2] %}

{% set foo = {'foo': 'bar'} %}

{% set foo = 'foo' ~ 'bar' %}

{% set foo, bar = 'foo', 'bar' %}
其中 'foo'~'bar' 这个我没怎么看明白,测试了一下,可能是字符串连接的。

set还有一种用法,就是把 块内的内容赋值给变量


{% set foo %} 
 

 
{% endset %} 
{% set foo %}
 
{% endset %}


extends标签
这个标签用来表示本模板继承自另外一个模板。和php一样,twig不支持多重继承,所以你只能有一个extends标签,而且要在模板的最上方。

我们先来定义一个“基模板” base.html 他就像一个骨架一个。


 
 
   

 
        {% block head %} 
             
            {% block title %}{% endblock %} - My Webpage 
        {% endblock %} 
     
     
       
{% block content %}{% endblock %}
 
         
     
 


   
        {% block head %}
           
            {% block title %}{% endblock %} - My Webpage
        {% endblock %}
   
   
       
{% block content %}{% endblock %}

       
   

{% block %}标签定义了4个区块(block head, block title, block content, block footer),可以让子模板来填充内容。block的作用就是告诉模板引擎,这里面的内容可以被子模板覆盖。

一个子模板大概类似于这样的


{% extends "base.html" %} 
 
{% block title %}Index{% endblock %} 
{% block head %} 
    {{ parent() }} 
     
{% endblock %} 
{% block content %} 
   

Index

 
   

 
        Welcome on my awesome homepage. 
   

 
{% endblock %} 
{% extends "base.html" %}

{% block title %}Index{% endblock %}
{% block head %}
    {{ parent() }}
   
{% endblock %}
{% block content %}
   

Index


   


        Welcome on my awesome homepage.
   


{% endblock %}
extends是非常关键的,它告诉模板引擎,本模板继承自另一个模板(base.html)。当模板引擎解析到本模板时,会首先载入父模板。extends标签应该是模板内的第一个标签。

如果子模板没有定义block footer ,那么父模板会用默认值代替。


注意:block标签的名字是不能重复的。如果你想让同一个block多次打印。可以使用block函数


{% block title %}{% endblock %} 

{{ block('title') }}

 
{% block body %}{% endblock %} 
{% block title %}{% endblock %}

{{ block('title') }}


{% block body %}{% endblock %}

父block
也许你会需要 父block的内容。可以使用parent函数,这很有用比如你想往一个block里添加内容而不是覆盖时。


{% block sidebar %} 
   

Table Of Contents

 
    ... 
    {{ parent() }} 
{% endblock %} 
{% block sidebar %}
   

Table Of Contents


    ...
    {{ parent() }}
{% endblock %}


命名endblock
模板引擎 允许你命名结束标记,这样可读性会提高很多。但个人觉得没啥用处。

{% block sidebar %} 
    {% block inner_sidebar %} 
        ... 
    {% endblock inner_sidebar %} 
{% endblock sidebar %} 
{% block sidebar %}
    {% block inner_sidebar %}
        ...
    {% endblock inner_sidebar %}
{% endblock sidebar %}

嵌套block
允许你嵌套生成block ,来形成更复杂的block


{% for item in seq %} 
   

  • {% block loop_item %}{{ item }}{% endblock %}
  •  
    {% endfor %} 
    {% for item in seq %}
       
  • {% block loop_item %}{{ item }}{% endblock %}

  • {% endfor %}


    简写block
    以下这两种写法是等效的


    {% block title %} 
        {{ page_title|title }} 
    {% endblock %} 
     
    {% block title page_title|title %} 
    {% block title %}
        {{ page_title|title }}
    {% endblock %}

    {% block title page_title|title %}

     


    动态继承
    你可以用一个变量来继承不同的模板。


    {% extends some_var %} 
    {% extends some_var %}
    如果变量是一个twig模板对象,也可以。


    $layout = $twig->loadTemplate('some_layout_template.twig'); 
     
    $twig->display('template.twig', array('layout' => $layout)); 
    $layout = $twig->loadTemplate('some_layout_template.twig');

    $twig->display('template.twig', array('layout' => $layout));
    1.2版本更新 你可以传递一个数组,twig会选择第一个存在的模板,来继承。


    {% extends ['layout.html', 'base_layout.html'] %} 
    {% extends ['layout.html', 'base_layout.html'] %}


    条件继承
    这个很简单自己看吧,


    {% extends standalone ? "minimum.html" : "base.html" %} 
    {% extends standalone ? "minimum.html" : "base.html" %}

     

    block标签
    参见 extends标签

     

     

    include标签
    载入一个模板,返回渲染的内容。载入的模板可以使用当前模板的变量{% include 'header.html' %} 
        Body 
    {% include 'footer.html' %} 
    {% include 'header.html' %}
        Body
    {% include 'footer.html' %}

    你可以给模板添加变量


    {# the foo template will have access to the variables from the current context and the foo one #} 
    {% include 'foo' with {'foo': 'bar'} %} 
     
    {% set vars = {'foo': 'bar'} %} 
    {% include 'foo' with vars %} 
    {# the foo template will have access to the variables from the current context and the foo one #}
    {% include 'foo' with {'foo': 'bar'} %}

    {% set vars = {'foo': 'bar'} %}
    {% include 'foo' with vars %}
    你也可以使用 only 关键字 来禁止载入的模板使用当前模板的变量,只能使用include 时with的变量{# only the foo variable will be accessible #} 
    {% include 'foo' with {'foo': 'bar'} only %} 
     
    {# no variable will be accessible #} 
    {% include 'foo' only %} 
    {# only the foo variable will be accessible #}
    {% include 'foo' with {'foo': 'bar'} only %}

    {# no variable will be accessible #}
    {% include 'foo' only %}

    载入的模板名也可以是一个twig表达式


    {% include some_var %} 
    {% include ajax ? 'ajax.html' : 'not_ajax.html' %} 
    {% include some_var %}
    {% include ajax ? 'ajax.html' : 'not_ajax.html' %}
    也可以用twig模板对象


    $template = $twig->loadTemplate('some_template.twig'); 
     
    $twig->loadTemplate('template.twig')->display(array('template' => $template)); 
    $template = $twig->loadTemplate('some_template.twig');

    $twig->loadTemplate('template.twig')->display(array('template' => $template));

    1.2版本新加内容,可以在模板加上 ignore missing 关键字,这样当模板不存在的时候就不会引发错误。

     

     

    {% include "sidebar.html" ignore missing %} 
    {% include "sidebar.html" ignore missing with {'foo': 'bar} %} 
    {% include "sidebar.html" ignore missing only %} 
    {% include "sidebar.html" ignore missing %}
    {% include "sidebar.html" ignore missing with {'foo': 'bar} %}
    {% include "sidebar.html" ignore missing only %}1.2版本新加内容,你可以给include传递一个数组,他会自动载入第一个存在的模板{% include ['page_detailed.html', 'page.html'] %} 
    {% include ['page_detailed.html', 'page.html'] %}

    import 标签


    twig允许把一些常用的代码放入到macros(宏)里,这些macros被不同的模板导入。

    有两种方法导入模板,你可以导入整个模板到一个变量里,或者只导入需要的几个macros

    假如我们有个助手模块,来帮助我们渲染表单(forms.html)


    {% macro input(name, value, type, size) %} 
         
    {% endmacro %} 
     
    {% macro textarea(name, value, rows) %} 
         
    {% endmacro %} 
    {% macro input(name, value, type, size) %}
       
    {% endmacro %}

    {% macro textarea(name, value, rows) %}
       
    {% endmacro %}
    最简单,最灵活的办法就是导入整个模板。(把模板导入到 forms变量里)


    {% import 'forms.html' as forms %} 
     

     
       
    Username
     
       
    {{ forms.input('username') }}
     
       
    Password
     
       
    {{ forms.input('password', null, 'password') }}
     
     

    {{ forms.textarea('comment') }}

     
    {% import 'forms.html' as forms %}


       
    Username

       
    {{ forms.input('username') }}

       
    Password

       
    {{ forms.input('password', null, 'password') }}


    {{ forms.textarea('comment') }}


    或者你可以导入模板的名字到当前的名字空间下。 (导入input,textarea 并把input重名为input_field)

     

    {% from 'forms.html' import input as input_field, textarea %} 
     

     
       
    Username
     
       
    {{ input_field('username') }}
     
       
    Password
     
       
    {{ input_field('password', '', 'password') }}
     
     

    {{ textarea('comment') }}

     
    {% from 'forms.html' import input as input_field, textarea %}


       
    Username

       
    {{ input_field('username') }}

       
    Password

       
    {{ input_field('password', '', 'password') }}


    {{ textarea('comment') }}

    如果是当前模板内定义的macros,那就不必导入了,直接使用特殊变量_self


    {# index.html template #} 
     
    {% macro textarea(name, value, rows) %} 
         
    {% endmacro %} 
     

    {{ _self.textarea('comment') }}

     
    {# index.html template #}

    {% macro textarea(name, value, rows) %}
       
    {% endmacro %}

    {{ _self.textarea('comment') }}


    那么你仍然可以导入_self到一个变量里,尽管这看起来很。。。没用。。


    {# index.html template #} 
     
    {% macro textarea(name, value, rows) %} 
         
    {% endmacro %} 
     
    {% import _self as forms %} 
     

    {{ forms.textarea('comment') }}

     
    {# index.html template #}

    {% macro textarea(name, value, rows) %}
       
    {% endmacro %}

    {% import _self as forms %}

    {{ forms.textarea('comment') }}


    from标签
    参见 import标签

    摘自 jiaochangyun的专栏
     

    声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    AI Hentai Generator

    AI Hentai Generator

    免费生成ai无尽的。

    热门文章

    R.E.P.O.能量晶体解释及其做什么(黄色晶体)
    3 周前By尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.最佳图形设置
    3 周前By尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.如果您听不到任何人,如何修复音频
    3 周前By尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25:如何解锁Myrise中的所有内容
    3 周前By尊渡假赌尊渡假赌尊渡假赌

    热工具

    MinGW - 适用于 Windows 的极简 GNU

    MinGW - 适用于 Windows 的极简 GNU

    这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

    PhpStorm Mac 版本

    PhpStorm Mac 版本

    最新(2018.2.1 )专业的PHP集成开发工具

    SublimeText3 Linux新版

    SublimeText3 Linux新版

    SublimeText3 Linux最新版

    SecLists

    SecLists

    SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

    Atom编辑器mac版下载

    Atom编辑器mac版下载

    最流行的的开源编辑器