Counting
The "count_characters" modifiers prints the number of non-whitespace characters in a value. If passed a parameter of true, it will count all characters, including whitespace. The "count_paragraphs" modifier prints the number of paragraphs in a value. The "count_sentences" modifier prints the number of sentences in a value. The "count_words" modifier prints the number of words in a value.
Formatting Dates with Smarty Date Modifier
The "date_format" modifier can be applied to timestamps to format a date:
date_format:"%m/%d/%y"
The formatting parameters are the same as those in PHP's strftime() function.
Without providing the formatting parameters, the date will be formatted as Mon D, YYYY (the short version of the month, the day as one or two numbers, and the year in four digits).
The current timestamp can be represented using the special $smarty variable: {$smarty.now}. To insert the current date in the default format, you would place this in your template (see the image at right):
{$smarty.now|date_format:"%m/%d/%y"}
Assigning Default Values
You can provide a default value for a variable by using the "default" modifier:
{$var|default:'some value'}
If a value is provided for $placeholder, it will be used, otherwise the default will be used in the generated page.
With PHP's error reporting set to E_ALL or E_STRICT, this will create an "undefined variable" error. The solution is to either adjust the level of error reporting in the script or to use a more formal IF-ELSE conditional:
{if $var eq ''}
some value
{else}
{$var}
{/if}
A default value can be provided for a variable for the whole template by using the assign function:
{assign var='var_name' value=$var_name|default:'some value'}
This line would be needed prior to any reference to $var_name in the template.
|