Adding foreach-like features to the Smarty automatically load data method

Source: Internet
Author: User

The first step: add in the _compile_tag function of Smarty_Compiler.class.php:

Copy the code code as follows:

Start tag for loading data
Case ' Load ':
$this->_push_tag (' Load ');
return $this->_complie_load_start ($tag _args);
Break
End tag for loading data
Case '/load ':
$this->_pop_tag (' Load ');
return "<php Endforeach; endif Unset (/$_from);?> ";
Break
Step Two: Add a method:

Copy the code code 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)));
Defining 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 handler to process
return $this->_compile_foreach_start ($tag _args);
}
This allows you to use the load tag in the template. Usage Example:

Copy the code code as follows:

{Load table= "test" where= "' id ' <100" limit=10 Item=rec}
...
{/load}

$i = 0;
$t = Microtime (true);
for (; $i <1000; $i + +)
{
$STR = STRTR (MD5 ($i), $p 2);
}
Var_dump (Microtime (True)-$t); 0.085476875305176
$t = Microtime (true);
for (; $i <2000; $i + +)
{
$str = Preg_replace ($p, ", MD5 ($i));
}
Var_dump (Microtime (True)-$t); 0.09863805770874
The results showed that the efficiency of STRTR was about 15% higher than that of preg_replace.
Taking advantage of the weekend, check out the strtr PHP Source:

Copy the code code as follows:

Php_function (STRTR)
{
Zval **str, **from, **to;
int ac = Zend_num_args ();
Parameter checking (the ZEND_GET_PARAMETERS_EX function is defined in the Zend_api.c file)
if (AC < 2 | | AC > 3 | | zend_get_parameters_ex (AC, &str, &from, &to) = = FAILURE) {
Wrong_param_count;
}
Parameter check
if (ac = = 2 && z_type_pp (from)! = Is_array) {
Php_error_docref (NULL tsrmls_cc, E_warning, "the second argument is not an array.");
Return_false;
}
CONVERT_TO_STRING_EX (str);
/* Shortcut for empty string */
Macro Z_strlen_pp defined in Zend_operators.h
if (z_strlen_pp (str) = = 0) {
Return_empty_string ();
}
if (ac = = 2) {
Php_strtr_array (Return_value, z_strval_pp (str), Z_STRLEN_PP (str), hash_of (*from));
} else {
CONVERT_TO_STRING_EX (from);
CONVERT_TO_STRING_EX (to);
Zval_stringl (Return_value, z_strval_pp (str), Z_STRLEN_PP (str), 1);
PHP_STRTR (Z_strval_p (Return_value),
Z_strlen_p (Return_value),
Z_strval_pp (from),
Z_STRVAL_PP (To),
MIN (Z_strlen_pp (from),
Z_STRLEN_PP (to));
}
}

Adding foreach-like features to the Smarty automatically load data method

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.