2016-04-05 9 views
0

Im einschließlich einer link.twig in block.twig, und block.twig in page.twig. In meinen Set-Optionen gibt es eine Möglichkeit, wie ich den Link-Objekt-Namen in etwas wie HeroLink ändern kann?Setzen Sie ein Objekt mit Twig in eine Vorlage, die über eine andere Vorlage importiert wird?

Ich muss Optionen innerhalb page.twig festlegen. link.twig ist in anderen Templates enthalten, so dass ich es nicht ändern möchte (zB link.url zu heldLink.url ändern).

In meiner Seite:

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

In block.twig:

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

In link.twig:

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

Der Grund dafür ist, dass block.twig tatsächlich hat andere Links. link.twig kann mehrfach importiert werden. Da das Mock-Objekt in page.twig erstellt werden muss, macht etwas wie heldLink in diesem Kontext mehr Sinn.

+1

'{% "link.twig" mit { 'Stil gehören ':' primary ',' heroLink ': link,}%} '? – DarkBee

Antwort

0

In meiner Seite:

{% set options = { 
     action: { 
     text: 'Action', 
     url: "action.com" 
     } 
    } 
    %} 
    {% include "component.twig" with options %} 

In component.twig:

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

{{ mainLink.link(action.url, action.text) }} 

In link.twig

{% macro link(url, text) %} 
     <a href="{{ url }}">{{ text }}</a> 
    {% endmacro %} 
Verwandte Themen