Use custom Plug-ins in Smarty to load data (see: Write a detailed description of the Smarty plug-in loading data directly in the template), or feel inconvenient when it is used, and brainwave want to write a label like foreach:
Step One: Add in the _compile_tag function of Smarty_Compiler.class.php:
Copy Code code as follows:
//Load start tag for data
case ' load ':
$this->_push_tag (' Load ');
return $this->_complie_load_start ($tag _args);
break;
//Load end tag for data
case '/load ':
$this->_pop_tag (' Load ');
return "<?php Endforeach; endif Unset (/$_from);?> ";
break;
Step Two: Add a method:
Copy Code code as follows:
/**
* Load Data
* @param $tag _args
*/
function _complie_load_start ($tag _args)
{
$key = substr (MD5 ($tag _args), 8, 16); Generates a special variable name based on the parameter
$attrs = $this->_parse_attrs ($tag _args);
//Here can add more processing
$class = (!isset ($attrs [' class ']) | | | empty ($attrs [' class ']))? ' Cls_crud ': Trim ($attrs [' class ']);
(!isset ($attrs [' table ']) | | empty ($attrs [' table '])] && exit (' table ' is empty! ');
$db = $class:: Factory (Array (' table ' => substr ($attrs [' table '], 1,-1));
//define new variable
$this->_tpl_vars[$key] = $db->get_block_list (Array (substr ($attrs [' where '], 1,-1)), $attrs [' limit ']);
$tag _args = "from=/${$key}". $tag _args;
//Invoke the Foreach tag handler function for processing
return $this->_compile_foreach_start ($tag _args);
}
This allows you to use the load tag in the template. Usage for example:
Copy Code code as follows:
{load table= ' test ' where= ' id ' <100 ' limit=10 Item=rec}
...
{/load}