I have studied smarty over the past two days and started to use it to develop a message board after I became familiar with some of its basic syntaxes. (In real development, no programmer will use smarty to develop a message board, how can it be used ?), It is mainly used to connect the learned knowledge points. During the development process, I encountered a problem, such as the question. I defined a function in the PHP file. How can I call it in the. TPL template? By searching for some information on the Internet, the following provides a specific solution:
As we all know, smarty provides many regulators used in templates, but in many cases, these regulators cannot provide the functions we need. At this time, we need to define our own PHP functions, then, it is called in the template.
First, let's talk about the solution in smarty2:
In smarty2, a function, register_function, is provided. Its prototype is
Void register_function (string name,
Mixed impl,
Bool cacheable,
Mixed cache_attrs );
We can use this function to dynamically register the UDF used in the template. Its first parameter is the function name that we will use in the template, the second parameter is the PHP function name we define. This parameter can be either a string containing the function name or an array (& $ object, $ method) array, & $ object indicates the object reference, and $ method indicates the method of the object. The third and fourth parameters can be omitted in most cases. Its usage is simple: a simple example is provided and explained below:
//. Php <br/> function htmlcode ($ paras) {<br/> extract ($ paras); <br/> return str_replace ('/n/R ', '<br>', str_replace ('','', $ cont); <br/>}< br/> $ smarty = new smarty (); <br/> $ smarty> register_function ('format _ content', 'htmlcode'); <br/> $ smarty-> display ("list.html ");
// List.html <br/> <Table width = "500px" border = "0" cellspacing = "5" cellpadding = "1" bgcolor = "# add3ef"> <br/> {section name = reply loop = $ content} <br/> <tr bgcolor = "# eff3ff"> <br/> <TD> title: {$ content [reply]. title} User: {$ content [reply]. user }</TD> <br/> </tr> <br/> <tr bgcolor = "# ffffff"> <br/> <TD> content: {format_content cont = $ content [reply]. content} </TD> <br/> </tr> <br/>{/ section} <br/> </table>
On the server side, that is, in the PHP file, I defined the htmlcode function that I needed, and then registered it to format_content using the register_function function, format_content is the function name used in the template. In the template, we use {format_content parameter 1 parameter 2 ......} In this example ,, the cont parameter is combined into an array of only one element and passed into the htmlcode function for calling. That is to say, the htmlcode parameter $ paras is an array, each element of it corresponds to a parameter that we pass to it in the template.
Here, we should know how to use it. Well, let's talk about the solution in smarty3.
In smarty3, I tried to use the register_function function. The function call 'register _ function' is unknown or deprecated error occurs, so I ran away from the official documentation, it is found that the register_function function has been eliminated in smarty3. Instead, it is a more powerful function named registerplugin. The prototype of this function is
Void registerplugin (string type,
String name,
Mixed callback,
Bool cacheable,
Mixed cache_attrs );
We can see that this function only has the first parameter, string type, more than register_function. What is the purpose of this parameter? This is defined in the document:
typeDefines the type of the plugin. Valid values are "function", "Block", "compiler" and "modifier ".
This parameter defines the plug-in type. The valid value is one of "function", "Block", "complier", and "modifier". You should understand it here, in register_function, only the function type can be registered, and this function can register four types. The remaining usage is basically the same as register_function.
In list.html, we don't need to change it, but just need to make some changes in the PHP file:
Function htmlcode ($ paras) {<br/> extract ($ paras); <br/> return str_replace ('/n/R',' <br> ', str_replace ('','', $ cont); <br/>}< br/> $ smarty = new smarty (); <br/> $ smarty-> registerplugin ('function', 'format _ content', 'htmlcode'); <br/> $ smarty-> display ("list.html ");
OK, all done!