How to add a foreach-like function to smarty to automatically load data _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.