Smarty Templating

Article templating can be more than just a question of copy/pasting data. Use logic to check if a field is empty or show a different picture if one was not uploaded. Change the article layout depending on a choice or limit the number of words..... this can all be done using Smarty Templating.

Validate for value (if/else)

You don't want to have any white space in your article because someone didn't fill out a field. So for a simple line of text between paragraph tags we don't want empty tags if the text field is empty.
Simple example: We have a text field called my_name and we only want to render it when there is a name in it.

{if $MY_NAME}<p>{$MY_NAME}</p>{/if}

Now let's say we want to give the name John Doe if no name is filled out.

{if $MY_NAME}<p>{$MY_NAME}</p>{else}<p>John Doe</p>{/if}

Smarty Modifiers (truncate/strip)

The data in the template parameters can be "modified" before it is pasted in to the Joomla article. In order to modify we use Smarty code snippets know as modifiers. It is even possible to create your own modifiers for Smarty as they are plugin based. Please refer to Smarty documentation for this.

Truncating example

Let's say you like to use the first 150 letters of a text area field as an introduction text. We would call this truncating the text.

<p>{$MY_TEXT_FIELD|truncate:150}</p>

But if you are using a HTML editor as input (multi-line text field with editor) it will be necessary to strip the HTML tags first.

<p>{$MY_TEXT_FIELD|strip_tags|truncate:150}</p>

You can find all Smarty options for truncating here:
https://www.smarty.net/docsv2/en/language.modifier.truncate.tpl
https://www.smarty.net/docsv2/en/language.modifier.strip.tags.tpl

Conditional statements (eq/elseif/==)

The single select list field type is available both in F2C Lite and Pro. The following example shows how you can change your article content based on the choice made by the submitter.

Single select list settings (field information)
When the single select list is used there are two columns in the field type settings. One is the 'option value', the other is the 'option display text'. The choice made can be rendered in two ways into the article.

Option value (latin characters, no spacing): {$FIELDNAME}
Option display text: {$FIELDNAME_TEXT}

Example: Gender choice determining article content
In this example we will assume there is a single select list field type with unique field name GENDER. The values are 1: Male, 2: Female. In our article we want our text to refer to 'him' or 'her' and 'his' or 'hers'.

Article text to create: "This box is his/hers, it therefore doesn't belong to him/her." The example will show the use of both the value and text option.

<p>This box
{if $GENDER eq '1'}
is his
{elseif $GENDER eq '2'}
is hers
{else}
is someones
{/if}
, it therefore doesn't belong to
{if $GENDER_TEXT == 'Male'}
her.
{elseif $GENDER_TEXT == 'Female'}
him.
{else}
you.
{/if}
</p>

The above code can render 3 options:

  1. This box is his, it therefore doesn't belong to her. (Male selected)
  2. This box is hers, it therefore doesn't belong to him. (Female selected)
  3. This box is someones, it therefore doesn't belong to you. (Nothing selected)

Of course there are many variants on display or hiding the entire option if nothing is selected. Extended Smarty documentation about conditions and possible syntax can be found here:http://www.smarty.net/manual/en/language.function.if.php

 

F2C Cheat-sheet

An overview of all template parameters (placeholders for your form data). These are used in your F2C intro and main template to parse the data from your submission forms into the Joomla article.

Download

Footer

Form2Content, a Joomla CCK Documentation © 2010 - 2015 Open Source Design | Powered by Form2Content