Home Backend Development PHP Tutorial Learning Twig's tags (Chinese) Part 2_PHP Tutorial

Learning Twig's tags (Chinese) Part 2_PHP Tutorial

Jul 13, 2016 pm 05:48 PM
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 %}

       


The {% block %} tag defines 4 blocks (block head, block title, block content, block footer), which allows sub-templates to fill in content. The function of block is to tell the template engine that the content inside can be overwritten by sub-templates.

A sub-template might look like this


{% 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 is very critical. It tells the template engine that this template inherits from another template (base.html). When the template engine parses this template, it will first load the parent template. The extends tag should be the first tag within the template.

If the child template does not define block footer, the parent template will use the default value instead.


Note: The name of the block tag cannot be repeated. If you want the same block to be printed multiple times. You can use the block function


{% block title %}{% endblock %}

{{ block('title') }}


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

{{ block('title') }}


{% block body %}{% endblock %}

Parent block
Maybe you will need the content of the parent block. You can use the parent function, which is useful when you want to add content to a block instead of overwriting it.


{% block sidebar %}

Table Of Contents


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

Table Of Contents


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


Name endblock
The template engine allows you to name the closing tag, which improves readability a lot. But personally I don’t think it’s useful.

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

Nested block

Allows you to nest generated blocks to form more complex blocks

{% for item in seq %}

  • {% block loop_item %}{{ item }}{% endblock %}

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

  • {% endfor %}

    Abbreviation block
    The following two writing methods are equivalent

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

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

    {% block title page_title|title %}

    Dynamic inheritance
    You can use a variable to inherit from different templates.

    {% extends some_var %}
    {% extends some_var %}
    This is also OK if the variable is a twig template object.


    $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));
    Updated in version 1.2 You can pass an array and twig will select the first existing template to inherit.


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


    Conditional inheritance
    This is very simple, just check it out yourself,


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

    block tag
    See extends tag

    include tag
    Loads a template and returns rendered content. The loaded template can use the variables of the current template {% include 'header.html' %}
    Body
    {% include 'footer.html' %}
    {% include 'header.html' %}
    Body
    {% include 'footer.html' %}

    You can add variables to templates


    {# 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 %}
    You can also use the only keyword to prevent the loaded template from using the variables of the current template. You can only use the variables in the include {# 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 %}

    The loaded template name can also be a twig expression


    {% include some_var %}
    {% include ajax ? 'ajax.html' : 'not_ajax.html' %}
    {% include some_var %}
    {% include ajax ? 'ajax.html' : 'not_ajax.html' %}
    You can also use twig template object


    $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));

    Newly added in version 1.2, you can add the ignore missing keyword to the template so that no error will occur when the template does not exist.

    {% 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 %} New content added in version 1.2, you can pass an array to include, and it will automatically load the first existing template {% include ['page_detailed.html', 'page .html'] %}
    {% include ['page_detailed.html', 'page.html'] %}

    import tag


    Twig allows you to put some commonly used code into macros, which are imported by different templates.

    There are two ways to import templates. You can import the entire template into a variable, or only import the few macros you need

    Suppose we have an assistant module to help us render the form (forms.html)


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

    {% endmacro %}

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

    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478460.htmlTechArticleset标签 主要是用来给变量赋值的。 {% set foo = foo %} {% set foo = [1, 2] %} {% set foo = {foo: bar} %} {% set foo = foo ~ bar %} {% set foo, bar = foo, bar %} {% set...
    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)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    4 weeks 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

    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

    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

    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

    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

    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

    See all articles