This is the celebrated journal of Mr. Simon Collison A.K.A Colly

More, more, more links in Expression Engine

1st November 2006

The split between intro and body is pretty common across blogs and news sites. On the home or news page you show the introductions to a number of articles or news stories, then you link to the full piece on it’s own page. If this is the intro, I’ll see you in the body.

Expression Engine gives us plenty of ways to accomplish this type of functionality. At the most basic level, we just need to setup custom fields for body and more then use something like the following: -

{if body}
<a href=”{title_permalink=“group/single_entry”}”>Read More</a>
{/if}

In simple terms, this chunk of code says: -

  1. If there’s any content in the body field
  2. Show a link to the single-entry page for this article
  3. That’s it

That’s a pretty basic and obvious example that works for most situations. But earlier today I needed to do something slightly more complex. My client wants to either use the body field to show the full news story on a subsequent page or fill in a link field in order for the Read More to link off-site.

Simple, I thought. I’ll just add a link_url field, then make a quick tweak to the example above: -


{if body}
<a href=”{title_permalink=“group/single_entry”}”>Read More</a>
{/if}

{if link}
<a href=”{link_url}”>Read More</a>
{/if}

Works fine, until the client enters both an external link in the link_url and some copy into the body field. We just know this will happen, and the result will be two clumsy links.

So, the more sophisticated version. It turned out to be slighly more complex than I first imagined, but easy to tackle with Expression Engine’s conditionals and operators. Here’s the logic: -

  • If the body field has content and the link field is empty, link to the single-entry page
  • If the body field is empty and the link field has content, link to the external url
  • If both fields have content, link to the single-entry page, and put the external url at the end of the article
  • If neither field has content, don’t do anything

Achieving this in EE is simple if we use elseif and re-arrange the logic a little. Here’s the code: -


{if body !=”” AND link !=”“}
<a href=”{title_permalink=“group/single_entry”}”>Read More</a>

{if:elseif body != “”}
<a href=”{title_permalink=“group/single_entry”}”>Read More</a>

{if:elseif link !=”“}
<a href=”{link_url}”>Read More</a>

{/if}

I think that’s quite neat and tidy: -

  • If the body field isn’t empty (has content) and the link field isn’t empty (has content), link to the single-entry page (and we’ll add the link at the end of the article
  • If that isn’t the situation, but the body field isn’t empty (has content), link to the single-entry page
  • If neither of the previous are the situation, and the link field isn’t empty (has content), link to the external url

Sure, it’s simple, but it shows how you can use Expression Engine to ensure your content works in a smart a way as possible.

Responses

# Adam Stacoviak responded on 2nd November 2006 with...

I’ve run into similar issues when building out EE for a client. This is a great, simple explanation of how to code a real world scenario using EE conditionals. One of the reasons we fell in love with EE was that you can do things like this without actually having to code in PHP, and the documentation to do so is very descriptive.

The only change that I opt for in this scenario is to use NULL in substitution of empty double quotes. It makes it easier to spot your NULL (empty) values, especially if you use Saft and TextMate (or another external editor).

{if body != NULL AND link != NULL}
Read More

{if:elseif body != NULL}
Read More

{if:elseif link != NULL}
Read More
{/if}

# Adam Stacoviak responded on 3rd November 2006 with...

Hmmm…I’m not sure I’ve run into that issue, I’ll have to keep an eye out.

Thanks for the heads up.

# Geof Harries responded on 3rd November 2006 with...

That’s also exactly what I love so much about EE. It permits me to do so much with so very little. EE is beyond elegant.

No messing around with PHP - I can get right to the desired functionality and have something operational within minutes. Such a powerful system.

# Simon Collison responded on 3rd November 2006 with...

One for the toolbox, methinks. Thank you Mr. Rudkin. Learning on my own blog yet again…

Responses are now disabled Your ability to respond is disabled automatically some 30 days after articles are published, or manually much sooner if spamming guttersnipes target a particular article.

Prev 655 Next

Superfluous Aside

Archived in ExpressionEngine, Design & Web

There are 5 responses

External References

Copyright © Mr. Simon Collison 2003-2017. Protected and licensed under a Creative Commons License. Grab the RSS feed

Engineered in Nottingham, scaffolded by ExpressionEngine, steam-pumped by United & kept alive with tea and hugs.