2

In my HTML-prototype I have two ways of showing a module of employees. One version shows one employee, and the other shows two.

To build this in Craft I have setup a Matrix field (with a handle named 'contactMatrix'). It contains a headline, some text and an entry field to pick employees (with a limit of two).

To output this in my front end I need a way to check if the user has picked one or two employees. Something like:

if one employee within the matrix field is chosen do this html else if two employees within the matrix field is chosen do this slightly different html end if

Help, anyone?

4

I believe the code below should work. This code assumes you are on a single entry page or within a loop and your entry is available to you with the entry variable. You may need to adjust the block.type name and block.employees entries field name to match your proper handles. I've added comments inline to clarify each step:

{# Loop through your Matrix Field #}
{% for block in entry.contactMatrix %}

    {# Check to see which type of block you have.  You only want to check for the employees entry field if you are within the contact block #}
    {% if block.type == 'contact' %}

        {# See if your employees entries field has more than 1 entry #}
        {% if block.employees|length > 1 %}

          {# Output HTML for blocks with multiple employees #}

        {% else %}

          {# Output HTML for blocks with zero or one employee #}

        {% endif %}

    {% endif %}

{% endfor %}
2
  • 1
    That else is {# Output HTML for blocks with ZERO OR one employee #} – Marion Newlevant Jul 15 '14 at 15:48
  • 1
    @Oyvind could you please accept / update helpful answers to your questions? – carlcs Jul 16 '14 at 11:35

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.