Detailed introduction to writing the Smarty plug-in to directly load data in the template

Source: Internet
Author: User
This article describes how to compile the Smarty plug-in to load data directly in the template. For more information, see

This article describes how to compile the Smarty plug-in to load data directly in the template. For more information, see

Previously, when using smarty, it was usually used to read data (usually from the database), website space, and then assign the template variables in the php program to use the variable at the front end. This is not bad, but it is a little difficult to maintain the php code when there is a lot of data, especially when there is a lot of multi-mode board data.

So I wrote a plug-in. In the U.S. space, combined with the previous crud class implementation, some modular data can be loaded in the front-end template.

The 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 {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 value of data to the variable $ params ['assign'], so that the front-end can use this variable (for example, you can use foreach to output a list)
$ Smarty-> assign ($ params ['assign'], $ db-> get_block_list (array ($ params ['where']), $ params ['limit']);
}
}
?>


Writing a plug-in not only reduces the number of maintenance items, but also reduces the website space. A significant advantage is that the plug-in allows you to perform unified formatting and filtering operations on database queries.
In this way, the data can be loaded at the front end as follows:

The Code is as follows:


{Load_data assign = "list" table = "test" where = "'id' <100" limit = 10}
{Foreach from = $ list item = rec}
...
{/Foreach}

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.