1

I've searched around and other solutions don't seem to be working I must be missing something obvious

Here's what i'm doing

{% set myVar = '' %}
{% for block in entry.matrixComponent %}
     {% if block.type == "matixBlockType" %}
          {% set myVar = block.matrixBlockField.ids() %}
     {% endif %}
{% endfor %}

This seems to work when I dump myVar I get

array(1) {
  [0]=>
  string(5) "10032"

which is indeed the id I'm looking for. I then

{% if entry.id not in myVar %}
    <div> {{ entry.id }} {{ Title }} not in myVar </div>
{% else %}
    <div> {{ entry.id }} {{ Title }} is in myVar {{ myVar }} </div>
{% endif %}

It lists out my posts in the matrix component, and where the entry.id matches myVar it still says not in myVar even though it would seem to me that it should have excluded it.

I'm not sure why it is listing the entry that matches myVar's Id

0

If I do a similar test:

{% set myVar = ['10032'] %}

{{ dump(myVar) }}

That gives:

array (size=1)
  0 => string '10032' (length=5)

Just like yours.

{% set entryId = 10032 %}

{% if entryId not in myVar %}
    {{ entryId }} not in myVar
{% else %}
    {{ entryId }} is in myVar
{% endif %}

Outputs:

10032 is in myVar

As expected. Changing entryId to something other than 10032 gives the expected "is not in myVar".

It should be noted that this line:

is in myVar {{ myVar }}

Will cause an array to string conversion error because you're telling Twig to output an array to the browser and it doesn't know how.

|improve this answer|||||
  • How would I set the string to myVar instead of the whole array? at {% set myVar = block.matrixBlockField.ids() %} Since I won't know the field id upfront normally. That way I can compare the strings later on. – Andrew Jan 22 '18 at 16:03
  • changing to {% if entry.id not in myVar[0] %} gets me a templating error. – Andrew Jan 22 '18 at 16:19
  • Sorry, I'm not following... in both your example and mine, myVar is set to an array with one string item in it set to "10032". – Brad Bell Jan 23 '18 at 0:31
  • It turns out that your answer correct. We had a plugin that would essentially iterate over it setting the id back to null. – Andrew Feb 16 '18 at 19:35
  • Glad you're all sorted! – Brad Bell Feb 17 '18 at 0:40

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.