<table cellspacing="0" cellpadding="0" border="0" width="100%" summary="Header navigation table"><tbody><tr><th align="center" colspan="3">Smarty - the compiling PHPtemplate engine</th> </tr><tr><td valign="bottom" align="left" width="25%"><a accesskey="P" href="api.createdata.html">Prev</a></td> <td valign="bottom" align="center" width="50%">Chapter 13. Smarty Class Methods()</td> <td valign="bottom" align="right" width="25%"><a accesskey="N" href="api.disableSecurity.html">Next</a></td> </tr></tbody></table> # createTemplate()[建立模板对象] Name createTemplate() ― returns a template object 返回一个模板对象 Description string createTemplate(string template,object parent); string createTemplate(string template, array data); string createTemplate(string template, string cache_id, string compile_id, object parent); string createTemplate(string template, string cache_id, string compile_id, array data); This creates a template object which later can be rendered by the display or fetch method. It uses the following parameters: template must be a valid template resource type and path. cache_id is an optional parameter. You can also set the $cache_id variable once instead of passing this to each call to this function. It is used in the event that you want to cache different content of the same template, such as pages for displaying different products. See also the caching section for more information. compile_id is an optional parameter. You can also set the $compile_id variable once instead of passing this to each call to this function. It is used in the event that you want to compile different versions of the same template, such as having separate templates compiled for different languages. parent is an optional parameter. It is an uplink to the main Smarty object, a user-created data object or to another user-created template object. These objects can be chained. The template can access only variables assigned to any of the objects in the parent chain. data is an optional parameter. It is an associative array containing the name/value pairs of variables which get assigned to the object. 本函数创建一个模板对象,该对象随后可被[display()](#)或[fetch()](#)渲染。其参数如下: “template”必须是个有效的模板资源类型和路径; “cache_id”是个可选参数,可以预先设置$cache_id变量,这样就不必每次调用本函数时传递具体参数。它在同一模板需要缓存不同内容情况下使用,例如,分页显示不同的产品。更多信息参见[缓存](#)章节; “compile_id”是个可选参数。与“cache_id”一样,可以预先将参数值分配给$compile_id变量,而不必每次调用本函数时传递具体参数。它在同一模板需要编译不同版情况下使用,例如,将模板分开不同的语言(编码)编译时; “parent”是个可选参数。它负责上行链接至主Smarty对象、一个用户自建数据对象或另一个用户自建模板对象。这些对象支持链式,模板可以访问赋值给其父链对象的变量。 “data”是个可选参数。它是个关联数组,包含赋值给对象的变量组成的名称/数值对。 <table class="EXAMPLE" cellspacing="0" cellpadding="0" border="0" width="100%"><tbody><tr><td> <div class="EXAMPLE"><strong><a name="AEN31315" id="AEN31315"/><span class="PROGRAMLISTING">Example 13.15. createTemplate()</span></strong><b><br/></b><table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td> <pre class="PROGRAMLISTING">&lt;?phpinclude('Smarty.class.php');$smarty = new Smarty;// create template object with its private variable scope$tpl = $smarty-&gt;createTemplate('index.tpl');// assign variable to template scope$tpl-&gt;assign('foo','bar');// display the template$tpl-&gt;display();?&gt;</pre></td> </tr></tbody></table><p><strong>译者例:parent参数使用</strong></p> <table width="100%" bgcolor="#e0e0e0" border="0"><tbody><tr><td>test.php:<br/>include_once('../libs/Smarty.class.php');<br/>$smarty = new Smarty;<br/>$smarty-&gt;assign('abc','hello!');<br/> $tpl= $smarty-&gt;createTemplate('test.html',$smarty); //这里‘parent’为Smarty对象$smarty <br/>$tpl-&gt;display(); <br/><br/>test.html:<br/>{$abc}</td> </tr></tbody></table><p> 参见<a href="api.display.html">display()</a>和<a href="api.template.exists.html">templateExists()</a>。</p> </div> </td> </tr></tbody></table> <table cellspacing="0" cellpadding="0" border="0" width="100%" summary="Footer navigation table"><tbody><tr><td valign="top" align="left" width="33%"><a accesskey="P" href="api.createdata.html">Prev</a></td> <td valign="top" align="center" width="34%"><a accesskey="H" href="index.html">Home</a></td> <td valign="top" align="right" width="33%"><a accesskey="N" href="api.disableSecurity.html">Next</a></td> </tr><tr><td valign="top" align="left" width="33%">createData()<br/> 建立数据</td> <td valign="top" align="center" width="34%"><a accesskey="U" href="api.functions.html">Up</a></td> <td valign="top" align="right" width="33%">disableSecurity()<br/> 关闭安全检查</td> </tr></tbody></table>