Personal tools
Views

User Handbook/Developers Guide/Smarty Tips

From CMSMS

Up: User Handbook/Developers Guide

Note: This page needs cleanup.

Contents

READ THIS FIRST: The Smarty Manual

HTML Entities with Smarty Modifiers

{$variable|html_entities}

Accessing CMSMS Variables from Smarty

{$gCms} is available so {$gCms.config.root_url}

PHP within Smarty

See: PHP Smarty Functions

  • calguy1000: you can call php from smarty
  • elijahlofgren: turned off by default though, right?
  • calguy1000: no
  • calguy1000: empty($variable) is a php function
    • I'd even try html_entities($variable) and see if that worked
    • i.e:{","|implode:$some_array} works
  • elijahlofgren: {if htmlentities("<test>")}
  • elijahlofgren: You're right, it works{/if}
  • elijahlofgren: ah, modifier you meant :)
  • calguy1000: to call a php function the rule is.... if the function only needs one argument, just call it... if it needs two or more treat as a modifier (I think)
  • that's nice because it allows you to do so much more for customizing the display of your page
  • but the rule should be, seperate logic into 'php' files... and display into .tpl files
  • so when coding, you just gotta remember

Debugging Smarty

Use {debug} when debugging smarty templates!!! Very useful!! See: http://smarty.php.net/manual/en/language.function.debug.php

Dumping variables

To get a var dump of a smarty object or variable, do something like this: {$var|print_r}

Showing Code Samples (preventing HTML and Smarty from being interpreted as such)

{capture assign='code_sample'}
{literal}
From: #cms on irc.freenode.net on June 21, 2008
(03:19:01 PM) elijahlofgren: calguy1000: e.g. News title in page title? 
(03:19:11 PM) calguy1000: elijahlofgren: 1.4 makes that easy
(03:19:35 PM) calguy1000: when processing a page template now we split apart the head from the body.... push the body through smarty first, then the head then output $head.$body
(03:19:49 PM) calguy1000: so you can set any smarty variable in any detail template, and use it in the head
(03:20:05 PM) calguy1000: or in content, or in a summar template... blah blah
(03:22:06 PM) calguy1000: so now in a news detail template you can simply do a {assign var='mytitle' value=$entry->title}
(03:22:44 PM) calguy1000: then in your head.... {if isset($mytitle)}<title>{$mytitle}</title>{else}<title>{title} -  {sitename}</title>{/if}
(03:25:43 PM) calguy1000: and module template modification times are cached in memory now.... so it's alot faster to load  module templates
{/literal}
{/capture}
{$code_sample|htmlentities}