BETA8 things to pay attention to
The Smarty3 API has been refactored to better structure-oriented and grammatical consistency. But the Smarty2 API is still supported, but it prompts.
Of course, you can also manually disable off this hint, but it is highly recommended that you upgrade your grammar to fit the SMARTY3 syntax
All method names in Smarty3 are "Foobarbaz", and all Smarty properties contain getters and setters, for example:
Set the cache path in the old version
$smarty->cache_dir
This can now be done:
$smarty->setcachedir (' foo/')
And can be obtained by using the following methods:
$smarty->getcachedir ()
Some smarty3 APIs, such as the beginning of the isxx, have been canceled because there are now similar GETXX methods for implementing the same functionality instead
The following is a simple list of APIs
$smarty->fetch ($template, $cache _id = null, $compile _id = null, $parent = NULL)
$smarty->display ($template, $cache _id = null, $compile _id = null, $parent = NULL)
$smarty->iscached ($template, $cache _id = null, $compile _id = null)
$smarty->createdata ($parent = null)
$smarty->createtemplate ($template, $cache _id = null, $compile _id = null, $parent = NULL)
$smarty->enablesecurity ()
$smarty->disablesecurity ()
$smarty->settemplatedir ($template _dir)
$smarty->addtemplatedir ($template _dir)
$smarty->templateexists ($resource _name)
$smarty->loadplugin ($plugin _name, $check = True)
$smarty->loadfilter ($type, $name)
$smarty->setexceptionhandler ($handler)
$smarty->addpluginsdir ($plugins _dir)
$smarty->getglobal ($varname = null)
$smarty->getregisteredobject ($name)
$smarty->getdebugtemplate ()
$smarty->setdebugtemplate ($tpl _name)
$smarty->assign ($tpl _var, $value = null, $nocache = False, $scope = Smarty_local_scope)
$smarty->assignglobal ($varname, $value = null, $nocache = False)
$smarty->assignbyref ($tpl _var, & $value, $nocache = False, $scope = Smarty_local_scope)
$smarty->append ($tpl _var, $value = null, $merge = False, $nocache = False, $scope = Smarty_local_scope)
$smarty->appendbyref ($tpl _var, & $value, $merge = False)
$smarty->clearassign ($tpl _var)
$smarty->clearallassign ()
$smarty->configload ($config _file, $sections = null)
$smarty->getvariable ($variable, $_ptr = null, $search _parents = True, $error _enable = True)
$smarty->getconfigvariable ($variable)
$smarty->getstreamvariable ($variable)
$smarty->getconfigvars ($varname = null)
$smarty->clearconfig ($varname = null)
$smarty->gettemplatevars ($varname = null, $_ptr = NULL, $search _parents = True)
Some API calls are done through its own objects
$smarty->cache->loadresource ($type = null)
$smarty->cache->clearall ($exp _time = null, $type = NULL)
$smarty->cache->clear ($template _name, $cache _id = null, $compile _id = null, $exp _time = null, $type = NULL)
$smarty->register->block ($block _tag, $block _impl, $cacheable = True, $cache _attr = Array ())
$smarty-> Register->compilerfunction ($compiler _tag, $compiler _impl, $cacheable = True)
$smarty->register-> Templatefunction ($function _tag, $function _impl, $cacheable = True, $cache _attr = Array ())
$smarty->register- >modifier ($modifier _name, $modifier _impl)
$smarty->register->templateobject ($object _name, $object _ Impl, $allowed = Array (), $smarty _args = True, $block _methods = Array ())
$smarty->register->outputfilter ($ function_name)
$smarty->register->postfilter ($function _name)
$smarty->register->prefilter ($ function_name)
$smarty->register->resource ($resource _type, $function _names)
$smarty->register- >variablefilter ($function _name)
$smarty->register->defaultpluginhandler ($function _name)
$ Smarty->register->defaulttemplatehandler ($function _name)
$smarty->unregister->block ($block _tag)
$smarty->unregister->compilerfunction ($compiler _tag)
$smarty->unregister->templatefunction ($function _tag)
$smarty->unregister->modifier ($modifier)
$smarty->unregister->templateobject ($object _name)
$smarty->unregister->outputfilter ($function _name)
$smarty->unregister->postfilter ($function _name)
$smarty->unregister->prefilter ($function _name)
$smarty->unregister->resource ($resource _type)
$smarty->unregister->variablefilter ($function _name)
$smarty->utility->compilealltemplates ($extention = '. TPL ', $force _compile = False, $time _limit = 0, $max _errors = n ULL)
$smarty->utility->clearcompiledtemplate ($resource _name = null, $compile _id = null, $exp _time = null)
$smarty->utility->testinstall ()
Then all the getters and setters can be used to get and set all the properties, and here are some examples:
$caching = $smarty->getcaching (); //Get $smarty->caching
$smarty-> Setcaching (TRUE); //Set $smarty-> Caching
$smarty->setdeprecationnotices (FALSE);//Set $smarty->deprecation_notices
$smarty-> Setcacheid ($id); //Set $smarty- >cache_id
$debugging = $smarty->getdebugging (); //Get $smarty->debuggin
directory structure
and SMARTY2 structure similar /p>
index.php
/libs/
Smarty.class.php #主文件
/libs/sysplugins/#内部plugin
Internal.
/plugins/#外部plugin, can be expanded freely
function.mailto.php
Modifier.escape.php2
/templates/#模板, can be a pure php or a traditional smarty template
Index.tpl
index_view.php
A lot of Smarty3 core functions are placed under the Sysplugins directory, and you don't need to modify any of the files.
Its plug-in file is placed under the/lib/plugins directory, where you can add your own plug-in files.
You still need to create your own cache/,templates/, template_c/, configs/directory, and make sure that the Cache/,template_c two directories have write permissions
Simple call
Require (' Smarty.class.php ');
$smarty = new Smarty;
$smarty->assign (' foo ', ' Bar ');
$smarty->display (' index.tpl ');)))
Difference
Although Smarty3 in the template is not the same as before, but in fact the internal logic is very different, but also can be compatible with 2
In addition to the following points
1.smarty3 can only run in PHP5 environments and no longer support PHP4
The 2.{php} label is turned off by default and can be opened by using the following method
$smarty->allow_php_tag=true
3. The template label will not support spaces, such as {$ABC} can be identified in the Smarty2, but 3 is not, it must be so {$ABC}, this is to be able to better support JavaScript and CSS, but you can still be set to support the original form
$smarty->auto_literal = false;
The API for 4.smarty3 is somewhat different, but it still supports Smarty2
Lexical attributes
Smarty3 uses a lexical analyzer for parsing and compiling templates that, in this way, can support some syntax extensions to make life better.
such as the mathematical calculation inside the template, intuitive, short function parameter options, and infinite function recursion, more accurate error handling and so on
New features
An expression
Support for more casual expressions
{$x + $y} Enter x and Y's and
{$foo = strlen ($bar)} variable supports PHP functions
{Assign Var=foo value= $x + $y} Property Support Expression
{$foo = Myfunct (($x + $y) *3)} function arguments support expressions
{$foo [$x +3]} The following table supports an expression
You can use a variable in quotation marks
{$foo = ' This was message {counter} '}
You can define an array inside a template
{Assign Var=foo value=[1,2,3]}
{Assign Var=foo value=[' y ' => ' yellow ', ' B ' => ' Blue ']}
{Assign Var=foo value=[1,[9,8],3]}
Simple variable Assignment
{$foo = $bar +2}
You can assign a value to the specified array element, and if the variable exists but is not an array, it converts the number of groups before assigning
{$foo [' Bar ']=1}
{$foo [' bar '] [' Blar ']=1}
Ditto, you can add values to an array
{$foo []=1}
property of the object supports the "." Operator
{$foo. A.B.C} => $foo [' a '] [' B '] [' C ']
{$foo. A. $b. c} => $foo [' a '] [$b] [' C ']
{$foo. A. {$b +4}.c} => $foo [' a '] [$b +4][' C ']
{$foo. A. {$b. C}} => $foo [' a '] [$b [' C ']]
Supporting variables in variable names
$foo a normal variable.
$foo _{$bar} variable names containing variables
$foo _{$x + $y} variable names can support an expression
$foo _{$bar}_buh_{$blar} variable name contains multiple variables
{$foo _{$x}} If $x is 1, output $foo_1
Supports object chains, which are sequential calls to object methods, much like jquery
{$object->method1 ($x)->method2 ($y)}
{for} tag supports loops like loop
{for $x =0, $y =count ($foo); $x < $y; $x + +} .... {/for}
In the For loop, you can qualify the location with the following special identifier:
$x @iteration The current number of cycles
Total cycle times of $x @total
$x @first Cycle for the first time
$x @last cycle The last time
The new foreach syntax
{foreach $myarray as $var} ... {/foreach}
The same is the special symbol in foreach, see it, do not translate ...
$var @key foreach $var array key
$var @iteration foreach Current iteration count (1,2,3 ...)
$var @index foreach Current index count (0,1,2 ...)
$var @total foreach $var array total
$var @first true on the iteration
$var @last True on the last iteration
Support While loop
{While $foo} ... {/while}
{while $x lt 10} ... {/while}
Functions that can be used directly with PHP
{Time ()}
Added a new label for {function} to define a block of functions to call (I like this feature, haha.) )
{function} ... {/function}
The label must have a Name property that is used to name the function, which is also required for the call.
Here is an example
/* Define a function */
{function Name=menu level=0}
<ul>
{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}
/* The parameters passed to the function * *
{$menu = [' item1 ', ' item2 ', ' item3 ' => [' item3-1 ', ' item3-2 ', ' item3-3 ' =>
[' Item3-3-1 ', ' item3-3-2 ']], ' ITEM4 ']}
/* Call that function * *
{Menu Data= $menu}
The {function} functional function must have a Name property and can have any number of other properties
Code blocks are not cached and can be closed by default by using the {NoCache} label
{NoCache} ... {/nocache}
can also be used as a property
{$foo Nocache=true}
{$foo NoCache}
{foo bar= "baz" nocache=true}
{foo bar= "baz" NoCache}
{Time () Nocache=true}
{Time () NoCache}
Returns the method of the current template
$smarty. cur_template
Variable scope and storage
In Smarty2, all the variables are stored in the Smarty object, so all the variables are available in all templates and sub methods
In Smarty3, you can define variables in the main Smarty object, or in the user-defined object, or even in the user's own template object.
And these objects can be connected by a chain string.
The object at the end of the chain can get all the variables stored in the object before the object chain.
The Smarty object must be the root object of the chain, but the object chain can exist independently of the Smarty object
All Smarty assignment methods can be used in either the data object or the template object
In addition to the above, global variables also have a special way of storing
A Smarty data object can be created in the following way
$data = $smarty->createdata (); To create a root data object
$data->assign (' foo ', ' Bar '); Assignment operations
$data->config_load (' my.conf '); Load configuration file
$data = $smarty->createdata ($smarty); Create a data object with Smarty as the parent object
$data 2= $smarty->createdata ($data); To create a data object Data2 as the parent object
Creating a Template object (template object) can be consistent by the Createtemplate method, its parameter passing, and the Fetch ()/display () method
How function is defined
function Createtemplate ($template, $cache _id = null, $compile _id = null, $parent = NULL)
Example
$TPL = $smarty->createtemplate (' Mytpl.tpl '); Create a template object with no parent object
$tpl->assign (' foo ', ' Bar '); Directly Assign variables
$tpl->config_load (' my.conf ');
$TPL = $smarty->createtemplate (' Mytpl.tpl ', $smarty); Create a template object with Smarty as the parent object
Fetch ()/display () Two methods will implicitly create a template object
If you do not specify a parent object, the default parent object will point to the Smarty object
If a template is called through include, the child template's parent object will point to the template object that references it
All the current template variables and the parent object's template variables are available, but if you are using {assign} or {$foo = ...} This method creates or modifies a variable
Then its scope will remain in the current template object only
In Smarty3, when assigning a variable, you can specify its scope, with 4 values Local,parent,root,global
{Assign Var=foo value= ' bar '}//No scope is specified, the default ' local '
{$foo = ' bar '}//Same, local scope
{Assign Var=foo value= ' bar ' scope= ' local '}//Same, local scope
{Assign Var=foo value= ' bar ' scope= ' parent '}//Values would be available to the parent object
{$foo = ' bar ' scope= ' parent '}//(normally the calling template)
{Assign Var=foo value= ' bar ' scope= ' root '}//Values would be exported up to the root object, so they can
{$foo = ' bar ' scope= ' root '}//is seen from all templates using the same root.
{Assign Var=foo value= ' bar ' scope= ' global '}//Values would be exported to global variable storage,
{$foo = ' bar ' scope= ' global '}
Extended
Smarty3 extensions are classes that inherit to Smarty–internal–pluginbase
All extensions contain a $this->smarty property of an Smarty object instance
Template inheritance
You can write {block} in the template ... {/block} fast, and these blocks can be overridden in a child template
PARENT.TPL:
<title>{block name= ' title '}my site name{/block}</title>
<body>
<div id= "Content" >
{Block name= ' content '}
Default Content
{/block}
</div>
</body>
CHILD.TPL:
{extends file= ' parent.tpl '}
{block name= ' title '}
Child title
{/block}
GRANDCHILD.TPL:
{extends file= ' child.tpl '}
{block name= ' title '} Home-{$smarty. Block.parent}{/block}
{block name= ' page-title '} My Home{/block}
{Block name= ' content '}
{foreach $images as $img}
{/foreach}
{/block}
The inherited template can be specified by the extends tag and overridden by overriding the parent template's name block in the child template
At the same time, you can get to the contents of the parent block through {$smarty. block,parent}
The GRANDCHILD.TPL above will produce the following
<title>home-child title</title>
<body>
<div id= "Content" >
</div>
</body>
Note that in the child template, all in the {block} ... Content other than {/block} will be ignored
This inheritance supports multiple files, multiple inheritance, which means you can inherit wirelessly
You can also insert the parent template structure by using the Append and Prepend properties of the {block}
PHP Streaming
To be supplemented ...
Variable Filter
To be supplemented ...
PHP templates
For those who want to write PHP purely in the template, Smarty offers a php option, pure PHP and a few different places:
The 1.PHP template will not be compiled and directly invoked by the engine
2.PHP templates will not have any security attributes
3.Smarty default does not open the PHP template, you can $smarty->allow_php_templates=true to open
If you want to use PHP template, just use PHP resource type
$smarty->display (' php:foo.php ');
You can also mix it in a template.
{include file= "php:foo.php"}