<p>I think your problem might be that you're overwriting the <code>metaDescribe</code> variable on each iteration of your <code>for</code> loop at the end of your first example. Given that you know you'll only be dealing with two blocks, you could try replacing the loop with something like this:</p>
<pre><code>{% set metaDescribe = artNameMat[0].artistName ~ ' ' ~ '(' ~ artNameMat[0].birthDeathLocationDate ~ ')' ~ ' ' ~ artNameMat[1].materials ~ ' ' ~  artNameMat[1].dimensions  %}
</code></pre>
<p>[Aside: it may also be worth revisiting your content model, as it sounds a little convoluted! Investigate the possibilities of moving some pieces of content out into dedicated sections or category groups and using relationships to tie everything together.]</p>

CC BY-SA 4.0
2020-10-01T15:23:48.703Z
That nailed it and served to show how much more I need to learn! Much appreciated. And yes, coming from EE I have yet to uncover the full depth of craft / twig, and rid myself of my old model of thinking. But as a first project... I'm bound to hit my head somewhere!
Seb
CC BY-SA 4.0
2020-10-01T14:29:29.763Z
2020-10-01T14:29:29.763Z
James Smith
<p>Newbie here, still grappling with basics, let alone elegance. My aim is to pull content from four different fields held in two different Matrix blocks and pass the output in a nice human readable format to populate the meta-description tag in my layouts and elsewhere later on the entry page.</p>
<p>I'm getting close but still no beer. I'm having trouble with merging everything. Only one or the other MatrixHandles outputs the correct content:</p>
<pre><code>{# Fetch MatrixBlock fields content for artistName and birthDeathLocationDate #}
{% set artistNameQuery = craft.matrixBlocks()
    .owner(entry)
    .field('artistNameBirthDeath')
    .type('artist') %}

{# Fetch MatrixBlock fields content for materials and dimensions #}
{% set artworkMaterialsQuery = craft.matrixBlocks()
    .owner(entry)
    .field('artworkDateMatDim')
    .type('artworkDetails') %}    

{# Try to merge the two above queries, with some merge voodoo #}
{% set artNameMat = artistNameQuery|merge(artworkMaterialsQuery) %}

{# Don't know why but this helps trigger the below, and sends it to my base.twig layout tmp #}
{% set metaDescribe = '' %} 

{# Print all of the above into a pretty, human/machine readable format for re-use in base.twig or elsewhere #}
{% for block in artNameMat %}
    {% set metaDescribe = block.artistName ~ ' ' ~ '(' ~ block.birthDeathLocationDate ~ ')' ~ ' ' ~ block.materials ~ ' ' ~  block.dimensions  %}
{% endfor %}
</code></pre>
<p>And then over in my base.twig template I've got:</p>
<pre><code>&lt;meta name=&quot;description&quot; content=&quot;
{% if metaDescribe is defined %}{{ metaDescribe }}{% else %} no meta desc {% endif %}&quot; /&gt;
</code></pre>
<p>So far it's outputting only the <code>artistNameQuery</code>. If I edit my not totally working merge line to just <code>{% set artNameMat = artworkMaterialsQuery %}</code>, it too sends the correct content for that Matrix handle/block to my base.twig.</p>
<p>Other notes. The above code is written within the template for displaying single artists' artwork (entry), outside of the <code>{% block main %}</code>. Running Craft 3.5.11.1.</p>
<p>Any pointers would be much appreciated!</p>
<p><strong>Update:</strong></p>
<p>I just tried making it all a lot more cleaner  / elegant replacing this:</p>
<pre><code>{% set artistNameQuery = craft.matrixBlocks()
    .owner(entry)
    .field('artistNameBirthDeath')
    .type('artist') %}

{% set artworkMaterialsQuery = craft.matrixBlocks()
    .owner(entry)
    .field('artworkDateMatDim')
    .type('artworkDetails') %} 
</code></pre>
<p>With this:</p>
<pre><code>{% set artNameMat = entry.artistNameBirthDeath.type('artist').all()
    |merge(entry.artworkDateMatDim.type('artworkDetails').all())
%}
</code></pre>
<p>But I'm still getting only &quot;one half&quot; of the Matrix fields that I'm after.</p>

<matrix>
Set fields from two different Matrix blocks, merge them and then set the output in a pretty way
# Set fields from two different Matrix blocks, merge them and then set the output in a pretty way

(Asked by: username_0 on 2020-10-01)

Newbie here, still grappling with basics, let alone elegance. My aim is to pull content from four different fields held in two different Matrix blocks and pass the output in a nice human readable format to populate the meta-description tag in my layouts and elsewhere later on the entry page.
I'm getting close but still no beer. I'm having trouble with merging everything. Only one or the other MatrixHandles outputs the correct content:
{# Fetch MatrixBlock fields content for artistName and birthDeathLocationDate #}
{% set artistNameQuery = craft.matrixBlocks()
    .owner(entry)
    .field('artistNameBirthDeath')
    .type('artist') %}

{# Fetch MatrixBlock fields content for materials and dimensions #}
{% set artworkMaterialsQuery = craft.matrixBlocks()
    .owner(entry)
    .field('artworkDateMatDim')
    .type('artworkDetails') %}    

{# Try to merge the two above queries, with some merge voodoo #}
{% set artNameMat = artistNameQuery|merge(artworkMaterialsQuery) %}

{# Don't know why but this helps trigger the below, and sends it to my base.twig layout tmp #}
{% set metaDescribe = '' %} 

{# Print all of the above into a pretty, human/machine readable format for re-use in base.twig or elsewhere #}
{% for block in artNameMat %}
    {% set metaDescribe = block.artistName ~ ' ' ~ '(' ~ block.birthDeathLocationDate ~ ')' ~ ' ' ~ block.materials ~ ' ' ~  block.dimensions  %}
{% endfor %}

And then over in my base.twig template I've got:


So far it's outputting only the artistNameQuery. If I edit my not totally working merge line to just {% set artNameMat = artworkMaterialsQuery %}, it too sends the correct content for that Matrix handle/block to my base.twig.
Other notes. The above code is written within the template for displaying single artists' artwork (entry), outside of the {% block main %}. Running Craft 3.5.11.1.
Any pointers would be much appreciated!
Update:
I just tried making it all a lot more cleaner  / elegant replacing this:
{% set artistNameQuery = craft.matrixBlocks()
    .owner(entry)
    .field('artistNameBirthDeath')
    .type('artist') %}

{% set artworkMaterialsQuery = craft.matrixBlocks()
    .owner(entry)
    .field('artworkDateMatDim')
    .type('artworkDetails') %} 

With this:
{% set artNameMat = entry.artistNameBirthDeath.type('artist').all()
    |merge(entry.artworkDateMatDim.type('artworkDetails').all())
%}

But I'm still getting only "one half" of the Matrix fields that I'm after.

**Tags:** matrix


---

## 1 answers

### Answer by username_1 on 2020-10-01. Score: 2 | Accepted answer

I think your problem might be that you're overwriting the metaDescribe variable on each iteration of your for loop at the end of your first example. Given that you know you'll only be dealing with two blocks, you could try replacing the loop with something like this:
{% set metaDescribe = artNameMat[0].artistName ~ ' ' ~ '(' ~ artNameMat[0].birthDeathLocationDate ~ ')' ~ ' ' ~ artNameMat[1].materials ~ ' ' ~  artNameMat[1].dimensions  %}

[Aside: it may also be worth revisiting your content model, as it sounds a little convoluted! Investigate the possibilities of moving some pieces of content out into dedicated sections or category groups and using relationships to tie everything together.]

> **Comments:**
> **username_0**: That nailed it and served to show how much more I need to learn! Much appreciated. And yes, coming from EE I have yet to uncover the full depth of craft / twig, and rid myself of my old model of thinking. But as a first project... I'm bound to hit my head somewhere!  

---

