Before using smarty, you usually read the data from the PHP program (typically from the database) and then assign the variables to the template before you can use the variable on the front end. This is not bad, but the data when the PHP side of the code to maintain a bit of trouble, especially when there are a lot of template block data.
So write a plugin, combining the previous CRUD classes implemented in the front-end template can load some modular data.
Copy Code code as follows:
<?php
/**
* Smarty Plugin
* @package Smarty
* @subpackage Plugins
*/
/**
* Smarty {load_data} function plugin
*
* type:function<br>
* name:eval<br>
* Purpose:evaluate A template variable as a template<br>
* @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 data to the variable $params[' assign ' so that the front end can use the variable (for example, you can output a list with foreach, etc.)
$smarty->assign ($params [' Assign '], $db->get_block_list (Array ($params [' where ']), $params [' limit ']);
}
}
?>
As a plug-in, in addition to reducing a lot of maintenance, there is a significant advantage is that the query database in this plug-in to the operation of a unified format and filtering operations.
This allows the data to be loaded on the front end:
Copy Code code as follows:
{load_data assign= ' list ' table= ' test ' where= ' id ' <100 ' limit=10}
{foreach from= $list Item=rec}
...
{/foreach}