Smarty 3 overview


Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Smarty 3 overview

  1. 1. Smarty 3 overview to overcome the present difficulties while upgrading to smarty3reference url1. {$array|mod} and {$array|@mod} behave identical2. Smarty 3 is PHP 5 only. It will not work with PHP 43. The use of {php} tags is deprecated. It can be enabled with $smarty->allow_php_tag=true.4. == Delimiters and whitespace ==Delimiters surrounded by whitespace are no longer treated as Smarty tags.Therefore, { foo } will not compile as a tag, you must use {foo}. This changeMakes Javascript/CSS easier to work with, eliminating the need for {literal}.This can be disabled by setting $smarty->auto_literal = false;5. == Unquoted Strings ==Smarty 2 was a bit more forgiving (and ambiguous) when it comes to unquotedstringsin parameters. Smarty3 is more restrictive. You can still pass stringswithout quotesso long as they contain no special characters. (anything outside of A-Za-z0-9_)For example filename strings must be quoted<source lang="smarty">{include file=path/foo.tpl}</source>6.== Extending the Smarty class ==If you are extendingthe Smarty class, its constructor is not called implicitly if the your childclass definesits own constructor. In order to run Smartys constructor, a call toparent::__construct()within your child constructor is required.<source lang="php">class MySmarty extends Smarty { function __construct() { parent::__construct(); // your initialization code goes here }}</source>
  2. 2. 7.== Scope of Special Smarty Variables ==In Smarty 2 the special Smarty variables $smarty.section... and$smarty.foreach...had global scope. If you had loops with the same name in subtemplates youcould accidentallyoverwrite values of parent template.In Smarty 3 these special Smarty variable have only local scope in thetemplate whichis defining the loop. If you need their value in a subtemplate you have topass themas parameter.<source lang="smarty">{include file=path/foo.tpl index=$}</source>8. == SMARTY_RESOURCE_CHAR_SET ==Smarty 3 sets the constant SMARTY_RESOURCE_CHAR_SET to utf-8 as defaulttemplate charset.This is now used also on modifiers like escape as default charset. If yourtemplates useother charsets make sure that you define the constant accordingly. Otherwiseyou may notget any output.9. == trigger_error() ==The API function trigger_error() has been removed because it did just map toPHP trigger_error.However its still included in the Smarty2 API wrapper10. == Smarty constants ==The constantsSMARTY_PHP_PASSTHRUSMARTY_PHP_QUOTESMARTY_PHP_REMOVESMARTY_PHP_ALLOWhave been replaced with class constantsSmarty::PHP_PASSTHRUSmarty::PHP_QUOTESmarty::PHP_REMOVESmarty::PHP_ALLOWNeed to understand more11. == newline at {if} tags ==A n was added to the compiled code of the {if},{else},{elseif},{/if} tags toget output of newlines as expected by the template source.
  3. 3. If one of the {if} tags is at the line end you will now get a newline in theHTML output.12. == Autoloader ==Smarty 3 does register its own autoloader with spl_autoload_register. If yourcode hasan existing __autoload function then this function must be explicitlyregistered onthe __autoload stack. See further details.13.== Plugin Filenames ==Smarty 3 optionally supports the PHP spl_autoloader. The autoloader requiresfilenamesto be lower case. Because of this, Smarty plugin file names must also belowercase.In Smarty 2, mixed case file names did feature examples Rewritten for PHP 52. New Lexer/Parser:Smarty has a new template parser, a real syntax lexer. This gives Smarty much finer control over itstemplate syntax. Things like in-template math, line-precision error messages and recursive templatefunctions are now possible.3. Template Objects can now make template objects and execute them independently. Example:$tpl = $smarty->createTemplate(my.tpl);$tpl->assign(foo,bar);$smarty->display($tpl); // or $tpl->display();Data Objects:-The variables assigned to a template can now be managed independently as a Smarty_Data object.Example:
  4. 4. $data = new Smarty_Data;$data->assign(foo,bar);$smarty->display(my.tpl,$data);$tpl = $smarty->createTemplate(my.tpl,$data);Template InheritanceTemplates can now inherit from each other. You mark blocks of a template with {blockname=foo}{/block} tags, a template can inherit with the {extend file="my.tpl"} tag, and then you canreplace, change or append the blocked contents from the inheriting template(s).Example:parent .tpl<html> <head> <title>{block name=title}default title{/block}<title> </head> <body> {block name=body}default body{/block} </body></html>child .tpl{extends file="parent.tpl"}{block name=title}My Child Title{/block}{block name=body}My Child Body{/block}output of $smarty->display(child.tpl);<html> <head> <title>My Child Title<title> </head> <body> My Child Body </body></html>
  5. 5. In-Template Function Definitions:-Smarty{* define the function *}{function name=menu level=0} <ul class="level{$level}"> {foreach $data as $entry} {if is_array($entry)} <li>{$entry@key}</li> {menu data=$entry level=$level+1} {else} <li>{$entry}</li> {/if} {/foreach} </ul>{/function}{* create an array to demonstrate *}{$menu = [item1,item2,item3 => [item3-1,item3-2,item3-3 =>[item3-3-1,item3-3-2]],item4]}{* run the array through the function *}{menu data=$menu}Output* item1* item2* item3 o item3-1 o item3-2 o item3-3 + item3-3-1 + item3-3-2* item4