Compile the Smarty plug-in to load data directly in the template. Previously, when using smarty, it was usually to read data from the php program (generally from the database), and then assign the template variable to use this variable at the front end. In this case, when using smarty, it is usually necessary to read data from the php program (generally from the database) and then assign the assign to the template variable to use the variable at the front end. This is not bad, but it is a little difficult to maintain the php code when there is a lot of data, especially when there is a lot of multi-mode board data.
Therefore, I wrote a plug-in that combined with the previous crud class implementation, some modular data can be loaded in the front-end template.
The code is as follows:
/**
* Smarty plugin
* @ Package Smarty
* @ Subpackage plugins
*/
/**
* Smarty {load_data} function plugin
*
* Type: function
* Name: eval
* Purpose: evaluate a template variable as a template
* @ Link http://smarty.php.net/manual/en/language.function.eval.php {eval}
* @ Param array
* @ Param Smarty
*/
Function smarty_function_load_data ($ params, & $ smarty)
{
$ Class = (! Isset ($ params ['class']) | empty ($ params ['class'])? 'Cls _ crud ': trim ($ params ['class']);
(! Isset ($ params ['table']) | empty ($ params ['table']) & exit ('table' is empty! ');
$ Db = $ class: factory (array ('table' => $ params ['table']);
// Var_dump ($ params );
If (! Empty ($ params ['assign']) {
// Assign the value of data to the variable $ params ['assign'], so that the front-end can use this variable (for example, you can use foreach to output a list)
$ Smarty-> assign ($ params ['assign'], $ db-> get_block_list (array ($ params ['where']), $ params ['limit']);
}
}
?>
Writing a plug-in not only reduces a lot of maintenance, but also has a significant advantage in that it can perform unified formatting and filtering operations on the query database.
In this way, the data can be loaded at the front end as follows:
The code is as follows:
{Load_data assign = "list" table = "test" where = "'id' <100" limit = 10}
{Foreach from = $ list item = rec}
...
{/Foreach}
Prepare (usually from the database), and then assign to the template variable, you can use this variable at the front end. This is not bad...