ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

EE v2.7.0 Relationships and Conditionals

September 15, 2013 12:21am

Subscribe [1]
  • #1 / Sep 15, 2013 12:21am

    sebastianjt

    17 posts

    Hello,

    I am using ExpressionEngine v2.7.0 and using the new Relationships Field for the first time. I am trying to conditionally render a block of code if the relationship indeed contains any related entries (as you would using Playa).

    I am doing a relationship between Projects and Collaborators. The relationship field lives in the Projects data channel.

    This is the code I would hope worked:

    {if project_collaborators}
    <h4>Collaborators</h4>
    <p><ul class="collaborators"><br />
         {project_collaborators}<br />
         <li>{project_collaborators:title}</li><br />
         {/project_collaborators}<br />
    </ul><br />
    <a href="/collaboration">Learn more</a><br />
    {/if}

    Instead I ended up coding it this way (which works but looks awful):

    {project_collaborators}
    {if project_collaborators:count == 1}
    <h4>Collaborators</h4>
    <p><ul class="collaborators"><br />
    {/if}<br />
        <li>{project_collaborators:title}</li><br />
    {if project_collaborators:count == project_collaborators:total_results}<br />
    </ul><br />
    <a href="/collaboration">Learn more</a><br />
    {/if}<br />
    {/project_collaborators}

    This last example isn’t that bad, BUT things get much messier in other more complex instances.

    I may be missing something simple so forgive my ignorance. If anyone could advice on how to achieve this it would be greatly appreciated.

    Thanks in advance.

    Sebastián Jaramillo.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases