• Blog
  • Board
  • Chat
  • Store
  • Links
  • Translate
    • Chinese
    • French
    • German
    • Italian
    • Japanese
    • Korean
    • Spanish
  • Tools
    • Print
    • Normal
    • Mobile
    • Text Only
    • Increase Text Size
    • Decrease Text Size
  • Share
    • Email to a Friend
    • Blinklist
    • Blogger
    • Del.icio.us
    • Digg
    • Facebook
    • Google Bookmarks
    • Linked In
    • Myspace
    • Stumble Upon
    • Twitter
    • Yahoo Bookmarks
wesleygraham
  • About
    • Social Media
      • Facebook
      • Twitter
      • Linked In
      • Myspace
    • Resume
      • PDF
      • HTML
    • Contact
  • Portfolio
    • Design
      • Logos
      • Brochures
      • Posters
      • Websites
    • Photography
  • Resources
    • Smarty Quick Reference
    • CSS3 Quick Reference
    • CSS Hacks

Smarty Quick References

Below you will find some random useful smarty code.

  • Assign
  • Print Array
  • Count Array Objects
  • Get Current Date
  • Date Format
  • String Format
  • Currency
  • Character Count
  • Truncation
  • Concatenation
  • Include
  • Word Wrap

Examples

Assign Value

<{assign var='name' value='content'}>

Assign Array

<{assign var='array' value=','|explode:"content1,content2,content3"}>

Printing an array's values via smarty

<{$array_name|@debug_print_var}>

Counting array objects

Or if you just want a count of the number of objects, in order to, say, display an edit button if there is content in the array

<{$array_name|@count}>

An example of use:

<{if $options|@count neq '0'}><input type="button" value="Edit Selected"><{/if}>


Date Format

<{$smarty.now|date_format:"%A, %B %e, %Y"}>

Note: <{$smarty.now}> will give you the current date.

Other options:

  • %a - abbreviated weekday name according to the current locale
  • %A - full weekday name according to the current locale
  • %b - abbreviated month name according to the current locale
  • %B - full month name according to the current locale
  • %c - preferred date and time representation for the current locale
  • %C - century number (the year divided by 100 and truncated to an integer, range 00 to 99)
  • %d - day of the month as a decimal number (range 01 to 31)
  • %D - same as %m/%d/%y
  • %e - day of the month as a decimal number, a single digit is preceded by a space (range 1 to 31)
  • %g - Week-based year within century [00,99]
  • %G - Week-based year, including the century [0000,9999]
  • %h - same as %b
  • %H - hour as a decimal number using a 24-hour clock (range 00 to 23)
  • %I - hour as a decimal number using a 12-hour clock (range 01 to 12)
  • %j - day of the year as a decimal number (range 001 to 366)
  • %k - Hour (24-hour clock) single digits are preceded by a blank. (range 0 to 23)
  • %l - hour as a decimal number using a 12-hour clock, single digits preceeded by a space (range 1 to 12)
  • %m - month as a decimal number (range 01 to 12)
  • %M - minute as a decimal number
  • %n - newline character
  • %p - either `am' or `pm' according to the given time value, or the corresponding strings for the current locale
  • %r - time in a.m. and p.m. notation
  • %R - time in 24 hour notation
  • %S - second as a decimal number
  • %t - tab character
  • %T - current time, equal to %H:%M:%S
  • %u - weekday as a decimal number [1,7], with 1 representing Monday
  • %U - week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week
  • %V - The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week.
  • %w - day of the week as a decimal, Sunday being 0
  • %W - week number of the current year as a decimal number, starting with the first Monday as the first day of the first week
  • %x - preferred date representation for the current locale without the time
  • %X - preferred time representation for the current locale without the date
  • %y - year as a decimal number without a century (range 00 to 99)
  • %Y - year as a decimal number including the century
  • %Z - time zone or name or abbreviation
  • %% - a literal `%' character

String Format

Where <{$number}> eq 1023.5787446

<{$number|string_format:"%.2f"}>
<{$number|string_format:"%d"}>
						

Output: 1023.58
1023


Currency

Where <{$amount}> eq '23744623.5787446'

<{$amount|@currency'}>

Output: $23,744,623.58


Character Count

Using following paragraph as example text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam vel tortor non lorem blandit convallis suscipit vel leo. Duis nisl augue, semper non gravida nec, sodales sit amet lectus. Proin venenatis tristique felis. In felis sapien, semper vitae laoreet vitae, tincidunt eget risus. In nec mi justo, eu facilisis erat. Vivamus egestas pharetra sapien, eget elementum dolor laoreet eget. Proin eu magna quis nunc ultrices cursus. Aenean est ante, mattis sed dictum eu, feugiat non metus. Ut et libero eu orci accumsan ullamcorper. Quisque adipiscing lorem at leo gravida id pellentesque enim rutrum. Duis a massa aliquet nisl mattis consequat. Nam porttitor iaculis neque, at pharetra tellus eleifend at. Phasellus sit amet nunc nec neque semper aliquam.

<{$paragraph|count_characters}>
<{$paragraph|count_characters:true}>
						

Note:
true determines whether or not to include whitespace characters in the count.

Output: 641
757

Using character count is handy for if statements, especially in classing and applying styles.
ex: <{if ($paragraph|character_count) gt '40'}> class="tall"<{/if}>


Truncation

Using following paragraph as example text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam vel tortor non lorem blandit convallis suscipit vel leo. Duis nisl augue, semper non gravida nec, sodales sit amet lectus. Proin venenatis tristique felis. In felis sapien, semper vitae laoreet vitae, tincidunt eget risus. In nec mi justo, eu facilisis erat. Vivamus egestas pharetra sapien, eget elementum dolor laoreet eget. Proin eu magna quis nunc ultrices cursus. Aenean est ante, mattis sed dictum eu, feugiat non metus. Ut et libero eu orci accumsan ullamcorper. Quisque adipiscing lorem at leo gravida id pellentesque enim rutrum. Duis a massa aliquet nisl mattis consequat. Nam porttitor iaculis neque, at pharetra tellus eleifend at. Phasellus sit amet nunc nec neque semper aliquam.

<{$paragraph|truncate:80:"...":true:true}>

Note:
the first value determines the number of characters to truncate to,
the second is a string replace of the truncated text,
the third determines whether or not to truncate at a word boundary (False) or at an exact character (True), and
the forth determines whether the truncation happens at the end of the string (False) or in the middle of the string (True - Note word boundaries are ignored if set to True).


Concatenation

Where <{$articleTitle}> eq 'The world didn't end '

<{$articleTitle|cat:'Yesterday'}>

Output: The world didn't end Yesterday


Include

<{include file='PATH'}>

Word Wrap

This modifier(wordwrap) wraps a string to a column width. It has 3 parameters,

  1. Integer to specify the column width (default: 80)
  2. String of text to wrap the text to next line (default: '\n')
  3. Boolean value(true) to force to cut off at the exact character length (default: false / will try to wrap at a word boundary)

NOTE: Second parameter's default value ('\n') is suppose to work as a line break.

<{$variable|wordwrap:90:\n}>
 
wesleygraham
  • About
  • Portfolio
  • Resources