How to add a foreach-like function to smarty to automatically load data. Using custom plug-ins in smarty to load data (see the detailed introduction of writing a Smarty plug-in to directly load data in the template), it is not convenient to use it, smart phones use custom plug-ins in smarty to load data (see the detailed introduction of writing the Smarty plug-in to directly load data in the template). It is not convenient to use it, just like the foreach tag:
Step 1: Add the following in the _ compile_tag function of Smarty_Compiler.class.php:
The code is as follows:
// Start tag for loading data
Case 'load ':
$ This-> _ push_tag ('load ');
Return $ this-> _ complie_load_start ($ tag_args );
Break;
// Load the end tag of the data
Case '/load ':
$ This-> _ pop_tag ('load ');
Return" ";
Break;
Step 2: Add a method:
The code is as follows:
/**
* Load data
* @ Param $ tag_args
*/
Function _ complie_load_start ($ tag_args)
{
$ Key = substr (md5 ($ tag_args), 8, 16); // Generate a special variable name based on the parameter
$ Attrs = $ this-> _ parse_attrs ($ tag_args );
// More processing can be added here
$ 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 variables
$ This-> _ tpl_vars [$ key] = $ db-> get_block_list (array (substr ($ attrs ['where'], 1,-1 )), $ attrs ['limit']);
$ Tag_args = "from =/$ {$ key}". $ tag_args;
// Call the foreach tag processing function for processing
Return $ this-> _ compile_foreach_start ($ tag_args );
}
In this way, you can use the load label in the template. Example:
The code is as follows:
{Load table = "test" where = "'id' <100" limit = 10 item = rec}
...
{/Load}
Compile (see the detailed introduction of writing the Smarty plug-in to directly load data in the template). It is not convenient to use it...