Table of Contents
Index
{{ block('title') }}
Table Of Contents
Home php教程 php手册 Twig 的 tags学习(中文) 之二

Twig 的 tags学习(中文) 之二

Jun 13, 2016 am 10:46 AM
bar set twig Chinese host variable study Label use of Assignment

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的专栏
     

    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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌
    Will R.E.P.O. Have Crossplay?
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    How to set Chinese in Call of Duty: Warzone mobile game How to set Chinese in Call of Duty: Warzone mobile game Mar 22, 2024 am 08:41 AM

    Call of Duty Warzone is a newly launched mobile game. Many players are very curious about how to set the language of this game to Chinese. In fact, it is very simple. Players only need to download the Chinese language pack, and then You can modify it after using it. The detailed content can be learned in this Chinese setting method introduction. Let us take a look together. How to set the Chinese language for the mobile game Call of Duty: Warzone 1. First enter the game and click the settings icon in the upper right corner of the interface. 2. In the menu bar that appears, find the [Download] option and click it. 3. Select [SIMPLIFIEDCHINESE] (Simplified Chinese) on this page to download the Simplified Chinese installation package. 4. Return to the settings

    Setting up Chinese with VSCode: The Complete Guide Setting up Chinese with VSCode: The Complete Guide Mar 25, 2024 am 11:18 AM

    VSCode Setup in Chinese: A Complete Guide In software development, Visual Studio Code (VSCode for short) is a commonly used integrated development environment. For developers who use Chinese, setting VSCode to the Chinese interface can improve work efficiency. This article will provide you with a complete guide, detailing how to set VSCode to a Chinese interface and providing specific code examples. Step 1: Download and install the language pack. After opening VSCode, click on the left

    How to set Excel table to display Chinese? Excel switching Chinese operation tutorial How to set Excel table to display Chinese? Excel switching Chinese operation tutorial Mar 14, 2024 pm 03:28 PM

    Excel spreadsheet is one of the office software that many people are using now. Some users, because their computer is Win11 system, so the English interface is displayed. They want to switch to the Chinese interface, but they don’t know how to operate it. To solve this problem, this issue The editor is here to answer the questions for all users. Let’s take a look at the content shared in today’s software tutorial. Tutorial for switching Excel to Chinese: 1. Enter the software and click the "File" option on the left side of the toolbar at the top of the page. 2. Select "options" from the options given below. 3. After entering the new interface, click the “language” option on the left

    Tips for solving Chinese garbled characters when writing txt files with PHP Tips for solving Chinese garbled characters when writing txt files with PHP Mar 27, 2024 pm 01:18 PM

    Tips for solving Chinese garbled characters written by PHP into txt files. With the rapid development of the Internet, PHP, as a widely used programming language, is used by more and more developers. In PHP development, it is often necessary to read and write text files, including txt files that write Chinese content. However, due to encoding format problems, sometimes the written Chinese will appear garbled. This article will introduce some techniques to solve the problem of Chinese garbled characters written into txt files by PHP, and provide specific code examples. Problem analysis in PHP, text

    Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

    When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

    How to add tags on Douyin to attract traffic? Which tags on the platform are easiest to attract traffic to? How to add tags on Douyin to attract traffic? Which tags on the platform are easiest to attract traffic to? Mar 22, 2024 am 10:28 AM

    As a popular short video social platform, Douyin has a huge user base. For Douyin creators, using tags to attract traffic is an effective way to increase the exposure of content and attract attention. So, how does Douyin use tags to attract traffic? This article will answer this question in detail for you and introduce related techniques. 1. How to add tags on Douyin to attract traffic? When posting a video, make sure to choose tags that are relevant to the content. These tags should cover the topic and keywords of your video to make it easier for users to find your video through tags. Leveraging popular hashtags is an effective way to increase your video’s exposure. Research current popular tags and trends and incorporate them into your video descriptions and tags. These popular tags usually have higher visibility and can attract the attention of more viewers. 3. Label

    What is the clock behind the TikTok label? How to tag Douyin account? What is the clock behind the TikTok label? How to tag Douyin account? Mar 24, 2024 pm 03:46 PM

    When browsing Douyin works, we often see a clock icon behind the tag. So, what exactly is this clock? This article will focus on the discussion of "What is the clock behind the Douyin label", hoping to provide some useful reference for your use of Douyin. 1. What is the clock behind the Douyin label? Douyin will launch some hot topic challenges. When users participate, they will see a clock icon after the tag, which means that the work is participating in the topic challenge and displays the remaining time of the challenge. For some time-sensitive content, such as holidays, special events, etc., Douyin will attach a clock icon after the label to remind users of the validity period of the content. 3. Popular tags: When a tag becomes popular, Douyin will add a clock icon after the tag to indicate that the tag is

    Disable or enable automatic copy selection for copying in Terminal Disable or enable automatic copy selection for copying in Terminal Mar 24, 2024 am 09:46 AM

    This article will show you how to enable or disable automatic copying of selections to the clipboard in Windows Terminal. Windows Terminal is a multi-tab terminal emulator developed by Microsoft specifically for Windows 11/10, replacing the traditional command prompt. It supports running applications such as Command Prompt, PowerShell, WSL, Azure, etc. Often when working in the terminal, users need to copy commands and output, however the terminal does not support copying selection operations by default. Keep reading this article to learn how to fix this issue. How to enable or disable automatic copying of selections to cache in Terminal? Here's how you can enable or disable automatic copying of selections to the Terminal clipboard: Open the Terminal application and click above

    See all articles