Creating content template tutorial

This is a step-by-step tutorial which will give you an overview about creating a content template in Saurus 4. We assume that you have previously worked through the Creating page template tutorial.

Creating template file

This is pretty much the same procedure as with the page templates, although the content- and page templates are stored in different sections in site Admin:

Go to Admin > Templates > Content templates. Click New and name your template „My content articles” for example.

Editing the template

The content template file is stored in the same file system folder as the page templates:

/classes/smarty/templates

Find your file my_content_articles.html and open it in the text editor program. If you do not have access to the filesystem, you can work with the popup window in site Admin to continue with the tutorial.

Article listing

Copy-paste the following code to your content template:

{init_articles name="article_list"}
{foreach from=$article_list item="article"}

<h1>{$article->buttons}<a href="{$article->href}">{$article->title}</a></h1>

{init_article name="article_details" id=$article->id}
{$article_details->body|truncate:60:".."}

{foreachelse}
{$article_list_newbutton}
{/foreach}

Here’s what it does:

  • The {init_articles} SAPI tag works the same way as {init_sections} and retrieves articles from Saurus CMS database. We do not have many parameters in our example because by default this function returns child objects of section which ID is currently in the URL – the current section.
  • So we loop through the articles and print out v-buttons and clickable title.
  • {init_article} inside the loop cycle creates a new variable and retrieves current article’s detailed fields. We hold article’s basic information like title, ID, href separated from details such as lead and body. Note the plural form of {init_articles} and singular form of {init_article}.
  • truncate:60:”..” is a Smarty string modifier which cuts the text from first 60 chars and adds two dots if the actual text is longer.

Create a new section or modify existing to apply your content template and check out about how it works.

About object’s detail view

So your template creates a listing of articles within the current section. If you click on any of the article titles, an article’s detail view is displayed. For that, we use object template defined in Admin > Templates > Object templates. This template is used whenever you request the article by its ID in the URL, for example from search results.

Normally you would have an object template for each content object type but sometimes it would be wise to change the detail view regarding to the content template assigned to the section.

Which template is currently used?

Saurus CMS would use the following algorithm to find current content template:

  • If the URL holds an ID of any other object type than section, the object template defined in Admin > Templates > Object templates would be used.
  • If the URL holds an ID of section, the content template set in section’s properties is used. If the user has not set any content template for section, master content template defined in Admin > Templates > Master templates is used.

For page templates, if there is no local page template set in section’s properties, the template defined in Admin > Templates > Master templates is used regardless of which object type’s ID is in the URL.

Template parameters

You can override the template usage chosen by Saurus CMS by adding tpl and c_tpl parameters to the URL which change page and content template accordingly.

If you still have the my_page_template active for the site, you can try adding &tpl=1043 to your site URL. This should force the site to use the page template which shipped with the product installation. The value 1043 is identifier number of the template in the database and is not connected to the content object ID-s. You can find out the indetifiers of each template in Admin > Templates > Page Templates by moving the mouse cursor over template links and waching the browser status bar.

Creating detail view

Let’s see what we can do with this knowledge. Change the following row in your my_content_articles content template.

<h1>{$article->buttons}<a href="{$article->href}&amp;c_tpl={$c_tpl}">{$article->title}</a></h1>

We have added c_tpl parameter to the URL which points to article’s detail view and added {$c_tpl} SAPI variable to it which returns the current content template number. So we are forcing Saurus to use our current content template.

When you test the code about how it works you will find that instead of article details, a „new article” button is displayed. What happened is that your template tries to find articles which are sub-items to the object currently in URL. Since you clicked on article, you are now able to create sub articles to the article object. While we do not usually recommend it, you can create a structure like that in Saurus CMS. Our task here was to create a custom detail view so we need to check in our template which kind of object is in the URL.

Replace all your content template code with the following:

{if $current_class == 'section'}

{init_articles name="article_list"}
{foreach from=$article_list item="article"}

<h1>{$article->buttons}<a href="{$article->href}&amp;c_tpl={$c_tpl}">{$article->title}</a></h1>

{init_article name="article_details" id=$article->id}
{$article_details->body|truncate:60:".."}

{foreachelse}
{$article_list_newbutton}
{/foreach}

{else}

{init_article name="article_details"}

<h1>{$article_details->buttons}{$article_details->title}</h1>
<p>{$article_details->date} by {$article_details->author}<p>
{if $article_details->lead}<p>{$article_details->lead}</p>{/if}
{if $article_details->body}<p>{$article_details->body}</p>{/if}

{/if}

What we did:

  • There is an {if $current_class ..} sentence which checks whether the object in URL is section. If yes, we use the same code as previously to display article listing.
  • If not, we assume that the object type is article and print out the details such as title, date and author.
  • Since the content editor can split the article contents between lead and body part we check whether one of these are defined and display them accordingly in HTML paragraph.