Twig 将带有细枝的对象设置为通过其他模板导入的模板?

Twig 将带有细枝的对象设置为通过其他模板导入的模板?,twig,Twig,我在block.twig中包含link.twig,在page.twig中包含block.twig。在“我的设置选项”中,是否有一种方法可以将链接对象名称更改为类似heroLink的名称 我需要在page.twig中设置选项。link.twig包含在其他模板中,所以我不想更改它(例如将link.url更改为heroLink.url) 在我的页面中: {% set options = { title: 'my title', link: { text: 'Search',

我在block.twig中包含link.twig,在page.twig中包含block.twig。在“我的设置选项”中,是否有一种方法可以将链接对象名称更改为类似heroLink的名称

我需要在page.twig中设置选项。link.twig包含在其他模板中,所以我不想更改它(例如将link.url更改为heroLink.url)

在我的页面中:

{% set options = {
    title: 'my title',
    link: {
      text: 'Search',
      url: "www.google.com"
    }
}
%}
{% include "block.twig" with options %}
  {% set options = {
      action: {
        text: 'Action',
        url: "action.com"
      }
  }
  %}
  {% include "component.twig" with options %}
在block.twig中:

<div class="something">
    <h2>{{ title }}</h2>
    <div class="hero">
    {% include "link.twig" with {'style': 'primary'} %}
    </div>
</div>

{{title}}
{%包括带有{'style':'primary'}%}的“link.twig”
在link.twig中:

<a href="{{ link.url }}" class="link-class-{ style }}">{{ link.text }}</a>

原因是block.twig实际上还有其他链接。link.twig可以多次导入。因为模拟对象需要在page.twig中创建,所以类似于heroLink的东西在这种上下文中更有意义

在我的页面中:

{% set options = {
    title: 'my title',
    link: {
      text: 'Search',
      url: "www.google.com"
    }
}
%}
{% include "block.twig" with options %}
  {% set options = {
      action: {
        text: 'Action',
        url: "action.com"
      }
  }
  %}
  {% include "component.twig" with options %}
在component.twig中:

{% import "link.twig" as mainLink %}

{{ mainLink.link(action.url, action.text) }}
在link.twig中

    {% macro link(url, text) %}
      <a href="{{ url }}">{{ text }}</a>
    {% endmacro %}
{%macro链接(url,文本)%}
{%endmacro%}
{%包括带有{'style':'primary','heroLink':link,}%}的“link.twig”