<p>The short answer is <em>no</em>. </p>

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

CC BY-SA 3.0
2015-02-12T15:59:00.887Z
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
Gianluca Guarini
CC BY-SA 3.0
2015-02-12T14:09:58.820Z
2015-02-12T14:09:58.820Z
Marion Newlevant
<p>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.</p>

<pre><code>{% for module in entry.main_contents %}
    &lt;h1&gt;{{ module.title }}&lt;/h1
    {{ module.content | raw }}
{% endfor %}
</code></pre>

<p>The <code>with</code> statement allows you explicitly pass variables using <code>{ key:value }</code> 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: </p>

<pre><code>{% include '_modules/' ~ module.type with {'title': module.title, 'content':module.content} %}
</code></pre>

CC BY-SA 3.0
2015-02-12T18:19:19.440Z
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
CC BY-SA 3.0
2015-02-12T18:36:54.000Z
@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.
Douglas McDonald
CC BY-SA 3.0
2015-02-12T18:48:23.527Z
Yep thought so, I also almost never use it with `with`. :)
carlcs
CC BY-SA 3.0
2015-02-12T20:32:36.843Z
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
Gianluca Guarini
CC BY-SA 3.0
2015-02-12T17:21:05.790Z
2015-02-12T18:50:55.713Z
2015-02-12T18:50:55.713Z
Douglas McDonald
<p>Consider the following example:</p>

<p><em>master.twig</em></p>

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

<p><em>my_awesome_module.twig</em></p>

<pre><code>&lt;h1&gt;{{ title }}&lt;/h1
{{ content | raw }}
</code></pre>

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

<p><em>master.twig</em></p>

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

<p><em>my_awesome_module.twig</em></p>

<pre><code>&lt;h1&gt;{{ data.title }}&lt;/h1
{{ data.content | raw }}
</code></pre>

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

<p>p.s.</p>

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

<templating><matrix>
How to pass the matrix attributes to views without too much ceremony
# How to pass the matrix attributes to views without too much ceremony

(Asked by: username_0 on 2015-02-12)

Consider the following example:
master.twig
{% for module in entry.main_contents %}
  {% include '_modules/' ~ module.type with module only %}
{% endfor %}

my_awesome_module.twig
{{ title }}
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
{{ data.title }}
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...)

**Tags:** templating, matrix

---

## 2 answers

### Answer by username_1 on 2015-02-12. Score: 2 | Accepted answer

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.

> **Comments:**
> **username_0**: 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  

---

### Answer by username_2 on 2015-02-12. Score: 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 %}
    {{ module.title }}
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} %}

> **Comments:**
> **username_3**: 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.  
> **username_2**: @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.  
> **username_3**: Yep thought so, I also almost never use it with `with`. :)  
> **username_0**: 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  

---

