2

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?

  • 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. – Terry Upton Nov 8 '17 at 10:53
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.

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

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.