Before using smarty, it is usually the PHP terminal to read the data (generally from the database), and then assign to the template variables, you can use this variable on the front end. This is not bad, but the data is more when the PHP side of the code to maintain a bit of trouble, especially when there are a lot of template chunks of data.
So write a plug-in, combined with the previous CRUD class implementation in the front-end template can load some of the modular data.
Copy CodeThe 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 data to the variable $params[' assign ', so that the front end can use the variable (for example, you can combine a foreach output with a list, 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 benefit is that the query database operation in this plug-in can be unified format and filtering operations.
This allows the data to be loaded in the front-end:
Copy CodeThe code is as follows:
{load_data assign= "list" table= "test" where= "' id ' <100" limit=10}
{foreach from= $list Item=rec}
...
{/foreach}
http://www.bkjia.com/PHPjc/327902.html www.bkjia.com true http://www.bkjia.com/PHPjc/327902.html techarticle before using smarty, it is usually the PHP terminal to read the data (generally from the database), and then assign to the template variables, you can use this variable on the front end. It's not bad ...