是否可以在Twig中定义内联模板?

 李伟祥祥 发布于 2023-02-12 19:40

我想在Twig做类似的事情:

{% inlinetemplate input_wrapper %}

{% block controls %}{% endblock controls %}
{% endinlinetemplate %} {% extendinline input_wrapper %} {% block label %}Age {% endblock label %} {% block name %}age{% endblock name %} {% block controls %} {% endblock controls %} {% endextendline input_wrapper %}

这可能吗?

1 个回答
  • 快速解决方案

    基于您对您真正想要的内容的评论,我相信set标签的"块版本" 是您所需要的,即:

    {% set variableName %}
    <p>
        Content block with <i>line-breaks</i>
        and {{ whatever }} else you need.
    </p>
    {% endset %}
    

     

    然后,您需要的任何标记块都可以传递给宏(正如其他人建议的那样):

    {% macro input_wrapper(label, name, controls) %}
    <div class="control-group">
        <label class="control-label" for="{{ name }}">{{ label }}</label>
        <div class="controls">{{ controls }}</div>
    </div>
    {% endmacro %}
    
    {% set label, name = "Age", "age" %}
    {% set controls %}
    <select name="age">
        <option>...</option>
    </select>
    {% endset %}
    
    {% import _self as inline %}
    {{ inline.input_wrapper(label, name, controls) }}
    

     

    真正的解决方案

    至于原始问题,我做了一些研究,发现你可以使用set和verbatim标签以及template_from_string函数的组合来定义内联模板.

    但是,模板的内容取决于您希望如何使用它:

      如果您对使用块语法设置变量感到满意,请使用include 标记或函数.

      如果您需要使用,就像在原始示例中一样,您将必须使用embed标记.

     

    使用变量和包含标记的示例

    {# Defining the template #}
    {% set input_wrapper_string %}
    {% verbatim %}
    <div class="control-group">
        <label class="control-label" for="{{ name }}">{{ label }}</label>
        <div class="controls">{{ controls }}</div>
    </div>
    {% endverbatim %}
    {% endset %}
    {% set input_wrapper_tpl = template_from_string(input_wrapper_string) %}
    
    {# Setting the variables #}
    {% set label, name = "Age3", "age" %}
    {% set controls %}
    <select name="age">
        <option>...</option>
    </select>
    {% endset %}
    
    {# "Rendering" the template #}
    {% include input_wrapper_tpl %}
    

     

    使用块和嵌入标记的示例

    {# Defining the template #}
    {% set input_wrapper_string %}
    {% verbatim %}
    <div class="control-group">
        <label class="control-label" for="{% block name %}{% endblock %}">{% block label %}{% endblock %}</label>
        <div class="controls">{% block controls %}{% endblock %}</div>
    </div>
    {% endverbatim %}
    {% endset %}
    {% set input_wrapper_tpl = template_from_string(input_wrapper_string) %}
    
    {# "Rendering" the template and overriding the blocks #}
    {% embed input_wrapper_tpl %}
        {% block label %}Age{% endblock %}
        {% block name  %}age{% endblock %}
        {% block controls %}
        <select name="age">
            <option>...</option>
        </select>
        {% endblock %}
    {% endembed %}
    

    2023-02-12 19:43 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有