简体   繁体   English

包含模板时不存在Symfony3变量

[英]Symfony3 Variable does not exist in including template

I get the following error: Variable "file" does not exist. 我收到以下错误:变量“文件”不存在。

The included template Contractor:file.html.twig looks like this: 包含的模板Contractor:file.html.twig如下所示:

{{ dump(file) }}

The main template form.html.twig looks like this: 主模板form.html.twig如下所示:

{% extends '::base.html.twig' %}
{% form_theme form 'NaSoftEdmBundle:Form:fields.html.twig' %}

...

{% block files %}
  {% for file in form.files %}
     {{ include('NaSoftEdmBundle:Contractor:file.html.twig', {'file': file}) }}
  {% endfor %}
{% endblock %}

Controller: 控制器:

public function editAction(Request $request, Contractor $contractor)
{
    ...
    return $this->render('NaSoftEdmBundle:Contractor:form.html.twig', array(
        'form' => $form->createView(),
    ));
}

But when I try to display a variable inside the main template file (form.html.twig), the variable becomes available: 但是当我尝试在主模板文件(form.html.twig)中显示变量时,变量变为可用:

{% for file in form.files %}
   {{ dump(file) }} {# it work! #}
{% endfor %}


ContractorFile {#499 ▼
 -id: 154
 -uploadedFile: null
 -name: "57c6d217d9a92.jpg"
 -origName: "1471590585502.jpg"
 -path: "/contractor/docs/files/2/335"
 -size: 153536
 -mimeType: "image/jpeg"
 -updateDate: DateTime {#496 ▶}
 -contractor: Contractor {#370 ▶}
}

command php app/console cache:clear did not help 命令php app/console cache:clear没有帮助

The correct way to pass variables to a Twig include command is this: 将变量传递给Twig include命令的正确方法是:

{% include 'NaSoftEdmBundle:Contractor:file.html.twig' with {'file': file} %}

As @walther pointed out in comments, it is not actually required to pass variables since an included template get access to variables of the active context, ie you can simply write: 正如@walther在评论中指出的那样,实际上并不需要传递变量,因为包含的模板可以访问活动上下文的变量,即你可以简单地写:

{% include 'NaSoftEdmBundle:Contractor:file.html.twig' %}

and the file variable will be used. 并将使用file变量。

You still have to use the with syntax if you want to assign a new variable, eg 如果要分配新变量,仍然必须使用with语法,例如

{% include 'NaSoftEdmBundle:Contractor:files.html.twig' with {'files': form.files} %}

See http://twig.sensiolabs.org/doc/tags/include.html 请参见http://twig.sensiolabs.org/doc/tags/include.html

The problem was that I was before starting the cycle "for" included the same template for attribute data-prototype : 问题是我在开始循环之前“for”包含了属性data-prototype的相同模板:

data-prototype="{% filter escape %} 
     {% include 'NaSoftEdmBundle: Contractor: file.html.twig' with { '_form':form.files.vars.prototype} %}
{% endfilter %}"

I've created for the attribute "data-prototype" new template and it worked. 我已经为属性“data-prototype”创建了新模板并且它有效。

data-prototype="{% filter escape %} 
     {% include 'NaSoftEdmBundle: Contractor: filePrototype.html.twig' with { 'file':_form.files.vars.prototype} %}
{% endfilter %}"

This has been implemented to customize the template collection 已实现此操作以自定义模板集合

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

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