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: -
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: -
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: -
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.
# 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.
Archived in ExpressionEngine, Design & Web
This post's Short URL is http://col.ly/s/655
There are 5 responses
Copyright © Mr. Simon Collison 2003-2012. 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 roll-ups.