Smarty compilation process

Source: Internet
Author: User
(Based on v2.6.20)
1. Smarty. Class. php
// Constructor
Function smarty ()
{
$ This-> assign ('script _ name ',...);
}
// Assign Function
Function assign ($ tpl_var, $ value = NULL)
{
If (is_array ($ tpl_var) {// you can directly pass the Array
Foreach ($ tpl_var as $ key => $ Val ){
If ($ key! = ''){
$ This-> _ tpl_vars [$ key] = $ val;
}
}
} Else {
If ($ tpl_var! = '')
$ This-> _ tpl_vars [$ tpl_var] = $ value;
}
}
Important: $ this-> _ tpl_vars is used to save template variables.
// Display Method
Function display ($ resource_name, $ cache_id = NULL, $ compile_id = NULL)
{
$ This-> fetch ($ resource_name, $ cache_id, $ compile_id, true );
}
// The logic of this method has not been fully sorted
Function fetch ($ resource_name, $ cache_id = NULL, $ compile_id = NULL, $ display = false)
{
// Debug ....
// Cache ....
// Call method _ compile_resource to compile
}
//
Function _ compile_resource ($ resource_name, $ compile_path)
{
// Call method _ compile_source
}
/**
* Compile the Template File
*
* @ Param string $ resource_name
* @ Param string $ source_content
* @ Param string $ compiled_content
* @ Return Boolean
*/
Function _ compile_source ($ resource_name, & $ source_content, & $ compiled_content, $ cache_include_path = NULL)
{
If (file_exists (smarty_dir. $ this-> compiler_file )){
Require_once (smarty_dir. $ this-> compiler_file );
} Else {
// Use include_path
Require_once ($ this-> compiler_file );
}
// Instantiate the compilation object
$ Smarty_compiler = new $ this-> compiler_class; // smarty_compiler
// Initialize the compilation object (pass related variables, including the template variables above)
$ Smarty_compiler-> template_dir = $ this-> template_dir;
$ Smarty_compiler-> compile_dir = $ this-> compile_dir;
$ Smarty_compiler-> plugins_dir = $ this-> plugins_dir;
$ Smarty_compiler-> config_dir = $ this-> config_dir;
$ Smarty_compiler-> force_compile = $ this-> force_compile;
$ Smarty_compiler-> caching = $ this-> caching;
$ Smarty_compiler-> php_handling = $ this-> php_handling;
$ Smarty_compiler-> left_delimiter = $ this-> left_delimiter;
$ Smarty_compiler-> right_delimiter = $ this-> right_delimiter;
$ Smarty_compiler-> _ version = $ this-> _ version;
$ Smarty_compiler-> Security = $ this-> Security;
$ Smarty_compiler-> secure_dir = $ this-> secure_dir;
$ Smarty_compiler-> security_settings = $ this-> security_settings;
$ Smarty_compiler-> trusted_dir = $ this-> trusted_dir;
$ Smarty_compiler-> use_sub_dirs = $ this-> use_sub_dirs;
$ Smarty_compiler-> _ reg_objects = & $ this-> _ reg_objects;
$ Smarty_compiler-> _ plugins = & $ this-> _ plugins;
$ Smarty_compiler-> _ tpl_vars = & $ this-> _ tpl_vars; // template variable
$ Smarty_compiler-> default_modifiers = $ this-> default_modifiers;
$ Smarty_compiler-> compile_id = $ this-> _ compile_id;
$ Smarty_compiler-> _ Config = $ this-> _ config;
$ Smarty_compiler-> request_use_auto_globals = $ this-> request_use_auto_globals;
If (isset ($ cache_include_path) & isset ($ this-> _ cache_serials [$ cache_include_path]) {
$ Smarty_compiler-> _ cache_serial = $ this-> _ cache_serials [$ cache_include_path];
}
$ Smarty_compiler-> _ cache_include = $ cache_include_path;
// Compile
$ _ Results = $ smarty_compiler-> _ compile_file ($ resource_name, $ source_content, $ compiled_content );
If ($ smarty_compiler-> _ cache_serial ){
$ This-> _ cache_include_info = array (
'Cache _ serial '=> $ smarty_compiler-> _ cache_serial
, 'Ins INS _ Code' => $ smarty_compiler-> _ plugins_code
, 'Include _ file_path '=> $ cache_include_path );
} Else {
$ This-> _ cache_include_info = NULL;
}
Return $ _ results;
}
2, smarty_compiler.class.php
// Constructor
Function smarty_compiler (){
// Initialize Regular Expressions for various matches, such as $ this-> _ func_regexp = '[A-Za-Z _] \ W *' of the matching function *';
}
// Compile. use $ compiled_content to return the compiled PHP code.
/*
The Returned Code includes two parts:
1. the header is as follows:
<? PHP // smarty version 2.6.20, created on 2009-12-21 10:38:57 compiled from index. TPL
?>
<? PHP
Require_once (smarty_core_dir. 'core. load_plugins.php ');
// Load custom plug-ins, such as load_data
Smarty_core_load_plugins (Array ('ins ins' => array (Array ('function', 'Load _ data', 'index. TPL ', 20, false),), $ this);?>
<? PHP $ _ smarty_tpl_vars = $ this-> _ tpl_vars; // template variable assignment
// Load the header Template
$ This-> _ smarty_include (Array ('smarty _ include_tpl_file '=> 'header. TPL', 'smarty _ include_vars' => array ()));
$ This-> _ tpl_vars =$ _ smarty_tpl_vars;
Unset ($ _ smarty_tpl_vars );
?>
2. Template subject
*/
Function _ compile_file ($ resource_name, $ source_content, & $ compiled_content ){
// Process text blocks
// Compile all labels and call the _ compile_tag method (important)
}
Note:
1. The data smarty in the literal label will not be analyzed (it can be used to display JavaScript scripts that may contain characters such as braces)
2. You can use PHP labels to nest PHP code.

Reference: http://code.google.com/p/cyy0523xc/source/browse/trunk/php/Smarty%E7%BC%96%E8%AF%91%E8%BF%87%E7%A8%8B.txt

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.