2

Consider the following example:

master.twig

{% for module in entry.main_contents %}
  {% include '_modules/' ~ module.type with module only %}
{% endfor %}

my_awesome_module.twig

<h1>{{ title }}</h1
{{ content | raw }}

In my case obviously I get the twig error Argument 1 passed to Twig_Template::display() must be of the type array because I cannot pass objects to the include function. In your examples you use the following solution:

master.twig

{% for module in entry.main_contents %}
  {% include '_modules/' ~ module.type with {'data': module} only %}
{% endfor %}

my_awesome_module.twig

<h1>{{ data.title }}</h1
{{ data.content | raw }}

Isn't there a way to pass directly the matrix attributes without encapsulating them in an other array?

p.s.

I hooked in also a twig helper method that returns $model->getContent()->getAttributes() but it seems that the array returned looses all its nested properties ( like the assets list...)

2

The short answer is no.

The longer answer is you will probably get used to it. Craft twig templates in general look like {{entry.title}} (or {{data.title}}) rather than just {{title}}. I find it easier to read, though you may not.

1
  • I don't know if I must get used to it, but with this approach if you try to reuse multiple times in different contexts the same component you will end polluting your code just because of a template engine limitation. In many javascript template engines it's really common to pass data to the templates without using any context Feb 12 '15 at 15:59
2

It has nothing to do with the template include, if you were to implement this without the include you would still need to access the title and content through the 'module' variable.

{% for module in entry.main_contents %}
    <h1>{{ module.title }}</h1
    {{ module.content | raw }}
{% endfor %}

The with statement allows you explicitly pass variables using { key:value } syntax, where 'key' is a variable that you will be able to access by name in your included template. You could just as easily pass multiple variables using:

{% include '_modules/' ~ module.type with {'title': module.title, 'content':module.content} %}
4
  • There's actually no need to explicitly pass the block model. Included templates do have access to the variables as long as you do not add the only keyword to the tag.
    – carlcs
    Feb 12 '15 at 18:19
  • @carlcs Your right! Thanks. From the twig docs 'Included templates have access to the variables of the active context.' I don't know what I was thinking — I do this all the time and don't use 'with'. Haven't had my coffee yet. Feb 12 '15 at 18:36
  • Yep thought so, I also almost never use it with with. :)
    – carlcs
    Feb 12 '15 at 18:48
  • I don't know how you guys are used to build your app but these snippets in my case do not scale at all IMHO. Hoping to get a better solution Thanks anyway Feb 12 '15 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.