Answers.Body: <p>From what I can see here, the only important object you care about in the include file is the video object. So what you could do is simply passing the video field in parameter directly, using, for the entry:</p>
  
  <pre><code>{% include '_includes/matrix/video' with { video : entry.video } only %}
  </code></pre>
  
  <p>...and for the matrix block:</p>
  
  <pre><code>{% include '_includes/matrix/video' with { video : block.video } only %}
  </code></pre>
  
  <p><strong>But</strong> then you would need to explicitly call each matrix block type with associated parameters.</p>
  
  <p>I would in any case add the <code>"only"</code> keyword after the parameters, as it guarantees that your template will be isolated from other variables already defined in the main template.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-08T10:58:16.540Z
Answers.LastActivityDate: 2017-11-08T10:58:16.540Z
Answers.UserDisplayName: kant312
Answers.Body: <p>Perhaps a better method is to include; <code>{% set object = block ?? entry %}</code> at the top of the <code>_includes/matrix/video.htm</code> template and this negates the need to use with <code>{ object : entry }</code> in the includes</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-08T10:54:08.650Z
Answers.LastActivityDate: 2017-11-08T10:54:08.650Z
Answers.UserDisplayName: Terry Upton
Body: <p>So I am trying to reuse some include files built for a matrix builder that are also in some instances used as stand alone fields (Video, Downloads etc). The way I have coded this is that the object/handle changes from using <code>block.handlename</code> to <code>entry.handlename</code> what would be the most efficient way to code this?</p>
  
  <p>Currently this is what I have done, but not sure it feels ‘right’?</p>
  
  <p>So taking the video as an example. I have an include file <code>_includes/matrix/video.html</code>
  I am then calling this from a Matrix Block like so;</p>
  
  <pre><code>{% for block in entry.mainContent.level(1) %}
  {% switch block.type %}
  {% case block.type %}
  {% include '_includes/matrix/' ~ block.type %}
  {% endswitch %}
  {% endfor %}
  </code></pre>
  
  <p>And also an entry template like so;</p>
  
  <pre><code>{% if entry.video|length %}
  &lt;div id="video" class="tab"&gt;
      &lt;div class="tab__content"&gt;
          {% include '_includes/matrix/video' with { object : entry } %}
      &lt;/div&gt;
  &lt;/div&gt;
  {% endif %}
  </code></pre>
  
  <p>In my video include file <code>_includes/matrix/video.html</code> I now have this;</p>
  
  <pre><code>{% set object = object ?? block %}
  {% set url = object.video %}
  
  &lt;div class="videoWrapper"&gt;
      {{ url|videoEmbed({ byline: 0, autoplay: 0, rel:0 }) }}
  &lt;/div&gt;
  </code></pre>
  
  <p>But this means i need to this each time I am calling an entry? <code>{% include '_includes/matrix/video</code> with <code>{ object : entry } %}</code> Is this the best approach?</p>
  
  <p>Any suggestions/recommendations?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-11-08T10:53:43.320Z
Comments.Text: Perhaps a better method is to include;
  `{% set object = block ?? entry %}` at the top of the `_includes/matrix/video.htm` template and this negates the need to use `with { object : entry }` in the includes.
Comments.UserDisplayName: Terry Upton
Tags: <templating><matrix><entry><include>
Title: Re-using includes when the object type changes (entry vs block) - what is the best method?
ThreadText: # Re-using includes when the object type changes (entry vs block) - what is the best method?
  
  (Asked by: username_0 on 2017-11-08)
  
  So I am trying to reuse some include files built for a matrix builder that are also in some instances used as stand alone fields (Video, Downloads etc). The way I have coded this is that the object/handle changes from using `block.handlename` to `entry.handlename` what would be the most efficient way to code this?
  
  Currently this is what I have done, but not sure it feels ‘right’?
  
  So taking the video as an example. I have an include file `_includes/matrix/video.html`
  I am then calling this from a Matrix Block like so;
  
  ```
  {% for block in entry.mainContent.level(1) %}
  {% switch block.type %}
  {% case block.type %}
  {% include '_includes/matrix/' ~ block.type %}
  {% endswitch %}
  {% endfor %}
  
  ```
  
  And also an entry template like so;
  
  ```
  {% if entry.video|length %}
  <div id="video" class="tab">
      <div class="tab__content">
          {% include '_includes/matrix/video' with { object : entry } %}
      </div>
  </div>
  {% endif %}
  
  ```
  
  In my video include file `_includes/matrix/video.html` I now have this;
  
  ```
  {% set object = object ?? block %}
  {% set url = object.video %}
  
  <div class="videoWrapper">
      {{ url|videoEmbed({ byline: 0, autoplay: 0, rel:0 }) }}
  </div>
  
  ```
  
  But this means i need to this each time I am calling an entry? `{% include '_includes/matrix/video` with `{ object : entry } %}` Is this the best approach?
  
  Any suggestions/recommendations?
  
  **Tags:** templating, matrix, entry, include
  
  > **Comments:**
  > **username_0**: Perhaps a better method is to include; `{% set object = block ?? entry %}` at the top of the `_includes/matrix/video.htm` template and this negates the need to use `with { object : entry }` in the includes.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2017-11-08. Score: 2
  
  From what I can see here, the only important object you care about in the include file is the video object. So what you could do is simply passing the video field in parameter directly, using, for the entry:
  
  ```
  {% include '_includes/matrix/video' with { video : entry.video } only %}
  
  ```
  
  ...and for the matrix block:
  
  ```
  {% include '_includes/matrix/video' with { video : block.video } only %}
  
  ```
  
  **But** then you would need to explicitly call each matrix block type with associated parameters.
  
  I would in any case add the `"only"` keyword after the parameters, as it guarantees that your template will be isolated from other variables already defined in the main template.
  
  
  ---
  
  ### Answer by username_0 on 2017-11-08. Score: 1
  
  Perhaps a better method is to include; `{% set object = block ?? entry %}` at the top of the `_includes/matrix/video.htm` template and this negates the need to use with `{ object : entry }` in the includes
  
  
  ---
  
  