简体   繁体   English

如何在Twig中将变量从父模板传递给子模板?

[英]How can I pass variable from parent template to child template in Twig?

I'm working with Symfony and Twig and I can't find solution for the next problem: in my parent template ( index.html.twig ) I have such code: 我正在使用Symfony和Twig,我找不到下一个问题的解决方案:在我的父模板( index.html.twig )中我有这样的代码:

<noscript>
    {% block noscript %}
    <div class="alert alert-warning">
        <strong>{% block notice %}{{ notice_js_disabled }}{% endblock %}&nbsp;</strong>
        {% block message %}{{ js_disabled }}{% endblock %}
    </div>
    {% endblock %}
</noscript>

I have child template ( category.html.twig ) which extends index.html.twig template. 我有子模板( category.html.twig ),它扩展了index.html.twig模板。

Can I pass value of {{ notice_js_disabled }} var from index template to category template? 我可以将{{ notice_js_disabled }} var的值从索引模板传递到类别模板吗?
notice_js_disabled is returned from my Symfony indexAction controller. notice_js_disabled从我的Symfony indexAction控制器返回。

UPD: The solution for my problem I founded, next: I have made base templae, called main.html.twig, where I'm rendering element from the controller: UPD:我创建的问题的解决方案,接下来:我已经创建了一个名为main.html.twig的基本模板,我从控制器渲染元素:

{% block header %}
   {{ render(controller('StoreBundle:Header:index')) }}
{% endblock %}

Then, on my index.html.twig file, I made next things: 然后,在我的index.html.twig文件中,我做了下面的事情:

{% extends 'Store/tpl/main.html.twig' %}
{% block title %}{{ title }}{% endblock %}
{% block content %}
    <p class="currentPage" hidden>home</p>
    {{ parent() }}
{% endblock %}

I'm not sure is it correct solution, but it's work:) 我不确定它是否正确解决方案,但它的工作:)

What you have to do is the following: -Create A variable in the parent template 您需要做的是: - 在父模板中创建一个变量

{% set notice_js_disabled = value_received_from_indexAction %}

After the template that you need that value 在您需要该值的模板之后

{{ notice_js_disabled }}

I hope it helps you 我希望它对你有所帮助

The only way in Twig you can pass some variable to a child template is by the include tag. 在Twig中,您可以将一些变量传递给子模板的唯一方法是使用include标记。

{# template.html will have access to the variables from the current context and the additional ones provided #}
{% include 'template.html' with {'foo': 'bar'} %}

{% set vars = {'foo': 'bar'} %}
{% include 'template.html' with vars %}

However in this way you are not extending some parent template from the child, but you are doing the other way around: you are placing a submodule into the current template. 但是,通过这种方式,您不会从子项扩展某些父模板,但是您正在执行相反的操作:您将子模块放入当前模板中。

There are only a couple of variables which are global in symfony You can set some custom global variables in configuration like described here 有一对夫妇只的是全球性的变量symfony中,您可以在配置中设置一些自定义的全局变量等中描述的在这里

Otherwise you can use some predefined global variables, the app variable. 否则,您可以使用一些预定义的全局变量app变量。 ( check it out here ) 在这里查看

My personal advice is that you should review your application template logic. 我个人的建议是,您应该检查您的应用程序模板逻辑。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM