Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-12T15:59:00.887Z
Answers.Text: 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
Answers.UserDisplayName: Gianluca Guarini
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-12T14:09:58.820Z
Answers.LastActivityDate: 2015-02-12T14:09:58.820Z
Answers.UserDisplayName: Marion Newlevant
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-12T18:19:19.440Z
Answers.Text: 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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-12T18:36:54.000Z
Answers.Text: @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.
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-12T18:48:23.527Z
Answers.Text: Yep thought so, I also almost never use it with `with`. :)
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-12T20:32:36.843Z
Answers.Text: 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
Answers.UserDisplayName: Gianluca Guarini
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-12T17:21:05.790Z
Answers.LastActivityDate: 2015-02-12T18:50:55.713Z
Answers.LastEditDate: 2015-02-12T18:50:55.713Z
Answers.UserDisplayName: Douglas McDonald
Body: <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>
  
Tags: <templating><matrix>
Title: How to pass the matrix attributes to views without too much ceremony
ThreadText: # 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*
  
  ```
  <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...)
  
  **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 %}
      <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} %}
  
  ```
  
  > **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**: @username_3 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  
  
  ---
  
  