Expression Engine - entry tags

Imagine you had the power to control and manipulate the display of every item of content on your site, this is exactly what EE’s “weblog entries tag” allows you to do.

Introducing single variables

Expression Engine allows you to have any number of data fields which are referred to as Single variables. For this example we’ll assume you have two variables - Title, and Summary. In your templates the code for these fields would be shown as {title} and {summary}.

A single variable can be virtually any sort of data you want, a name, a paragraph of text, an image or file, a date, or URL.

The weblog entries tag

Wherever you place this tag in a template EE will output any content that matches the tag. Let’s start by looking at a basic tag which looks like this…

{exp:weblog:entries weblog="news"}
{title} 
{summary}<br />
{/exp:weblog:entries} 

That was easy wasn’t it! What this is saying is show all entries from the news section, and for each show the Title and Summary data. Under normal circumstances this would simply output everything you had in your news section which probably wouldn’t be very practical if you had thousands on news items!

Now lets add some parameters to define exactly what we want to output.

Parameters

There are dozens of available parameters so I’ll just quote a couple of the most common ones here.

Let’s say we want to restrict the number of news item to 10, all we do is add the parameter - limit=“10” - to the tag. Also we’ll say we want to show the most recent entries, we can do this by sorting the output using - orderby=“date”. Let’s see what our tag looks like now…

{exp:weblog:entries weblog="news" limit="10" orderby="date"}
{title} 
{summary}<br />
{/exp:weblog:entries} 

Conditional variables

These variables show or don’t show information based on a “what if” test. You can test for many things, such as whether a user is logged in (or not), or whether a particular item of data is present. In our example we’ll add a test to output some information if there are no news results available using the “if no_results” conditional test…

{exp:weblog:entries weblog="news" limit="10" orderby="date"}
{title} 
{summary}<br />
{if no_results}Sorry theres nothing here{/if}
{
/exp:weblog:entries} 

So here we’re saying output 10 of the latest news titles and summaries, but if there aren’t any, output a useful message, ie “Sorry there’s nothing here”.

Variable pairs

Finally we have matching pairs of variables that cover categories and dates. In this example I’ll use the “categories” pair to output the category the news item is assigned to. The “category_name” variable is used to show the category name!

{exp:weblog:entries weblog="news" limit="10" orderby="date"}
{categories}{category_name}{
/categories} {title} {summary}<br />
{if no_results}Sorry theres nothing here{/if}
{
/exp:weblog:entries} 

The results

Assuming everything is in order the output to your web page would look something like this…

Sport - Northampton win - Cobblers win 2-0 at home ground
Science - Life found on Mars! - NASA announced today it has found little green men
Arts - Local auction surprise - Local artist sells a painting for 1 million
Politics - Politician shock - A local politician gave a straight “yes” answer
Business - Acme post record results - Acme made more money last year than the year before
Sport - Saints triumph - Northampton saints win the Rugby World cup

I’ve literally only covered the tip of the iceberg with the weblog entries tag. There are dozens of parameters and variables that can be put together in any combination, and that allows you complete control over your content and how your present it. For a full list head on over to the Developer documentation and see what’s possible.

Share |

Related reading