Added compiler function support.

This commit is contained in:
andrey
2001-04-24 16:43:05 +00:00
parent 75f7c18b68
commit d0a91394b1
7 changed files with 136 additions and 50 deletions

20
NEWS
View File

@@ -1,21 +1,25 @@
Version 1.4.0
-------------
- added custom compiler functions support, register_compiler_function() and
unregister_compiler_function() API functions. (Andrei, Ivo Jansch).
- updated GLOBAL_ASSIGN to take SCRIPT_NAME from HTTP_SERVER_VARS
instead of global variable. You can also assign several variables
in one shot with an array. (Monte)
- added template filters (Monte)
in one shot with an array. (Monte, Roman Neuhauser)
- added template filters, register_filer() and unregister_filter() API
functions. (Monte)
- added RELEASE_NOTES file to distribution (Monte)
- moved CREDITS out of manual into its own file (Monte)
- added register_resource() and unregister_resource() functions (Monte)
- changed syntax of variables called within section loops,
supplied fix_vars.php script to fix old syntax (Andrei)
- added $insert_tag_check to speed up cached pages if
{insert ...} is not used (Monte)
- added register_resource() and unregister_resource() API functions. (Monte)
- changed the syntax of indexing template variables, thus supporting
structures of arbitrary complexity; supplied fix_vars.php script to fix
old syntax. (Andrei)
- added $insert_tag_check to speed up cached pages if {insert ...} is not
used (Monte)
- added $compiler_class variable to allow specifying a different compiler
class. (Andrei)
- changed Smarty to compile templates at runtime, allowing for arbitrary
template resources. (Monte)
- added fix for LOCK_EX under windows and changed a couple file
- added fix for LOCK_EX under Windows and changed a couple of file
permissions for security. (Monte)
- allow arbitrary date strings to date_format, html_select_date and
html_select_time (Monte)

View File

@@ -103,6 +103,9 @@ class Smarty
var $left_delimiter = '{'; // template tag delimiters.
var $right_delimiter = '}';
var $compiler_funcs = array(
);
var $custom_funcs = array( 'html_options' => 'smarty_func_html_options',
'html_select_date' => 'smarty_func_html_select_date',
'html_select_time' => 'smarty_func_html_select_time',
@@ -132,9 +135,9 @@ class Smarty
var $compiler_class = 'Smarty_Compiler'; // the compiler class used by
// Smarty to compile templates
var $resource_functions = array(); // what functions resource handlers are mapped to
var $filter_functions = array(); // what functions templates are filtered through
// before being compiled
var $resource_funcs = array(); // what functions resource handlers are mapped to
var $filter_funcs = array(); // what functions templates are filtered through
// before being compiled
/**************************************************************************/
/* END SMARTY CONFIGURATION SECTION */
@@ -246,6 +249,23 @@ class Smarty
unset($this->custom_funcs[$function]);
}
/*======================================================================*\
Function: register_compiler_function
Purpose: Registers compiler function
\*======================================================================*/
function register_compiler_function($function, $function_impl)
{
$this->compiler_funcs[$function] = $function_impl;
}
/*======================================================================*\
Function: unregister_compiler_function
Purpose: Unregisters compiler function
\*======================================================================*/
function unregister_compiler_function($function)
{
unset($this->compiler_funcs[$function]);
}
/*======================================================================*\
Function: register_modifier
@@ -271,7 +291,7 @@ class Smarty
\*======================================================================*/
function register_resource($name, $function_name)
{
$this->resource_functions[$name] = $function_name;
$this->resource_funcs[$name] = $function_name;
}
/*======================================================================*\
@@ -280,7 +300,7 @@ class Smarty
\*======================================================================*/
function unregister_resource($name)
{
unset($this->resource_functions[$name]);
unset($this->resource_funcs[$name]);
}
/*======================================================================*\
@@ -290,7 +310,7 @@ class Smarty
\*======================================================================*/
function register_filter($function_name)
{
$this->filter_functions[] = $function_name;
$this->filter_funcs[] = $function_name;
}
/*======================================================================*\
@@ -300,12 +320,12 @@ class Smarty
function unregister_filter($function_name)
{
$tmp_array = array();
foreach($this->filter_functions as $curr_func) {
foreach($this->filter_funcs as $curr_func) {
if($curr_func != $function_name) {
$tmp_array[] = $curr_func;
}
}
$this->filter_functions = $tmp_array;
$this->filter_funcs = $tmp_array;
}
/*======================================================================*\
@@ -587,8 +607,8 @@ class Smarty
}
break;
default:
if(isset($this->resource_functions[$resource_type])) {
$funcname = $this->resource_functions[$resource_type];
if(isset($this->resource_funcs[$resource_type])) {
$funcname = $this->resource_funcs[$resource_type];
if(function_exists($funcname)) {
// call the function to fetch the template
$funcname($resource_name,$template_source,$template_timestamp);
@@ -629,7 +649,8 @@ class Smarty
$smarty_compiler->custom_funcs = $this->custom_funcs;
$smarty_compiler->custom_mods = $this->custom_mods;
$smarty_compiler->version = $this->version;
$smarty_compiler->filter_functions = $this->filter_functions;
$smarty_compiler->filter_funcs = $this->filter_funcs;
$smarty_compiler->compiler_funcs = $this->compiler_funcs;
if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled))
return true;

View File

@@ -54,15 +54,15 @@ class Smarty_Compiler extends Smarty {
function _compile_file($tpl_file, $template_source, &$template_compiled)
{
// run template source through functions registered in filter_functions
if(is_array($this->filter_functions) && count($this->filter_functions) > 0) {
foreach($this->filter_functions as $curr_func) {
// run template source through functions registered in filter_funcs
if(is_array($this->filter_funcs) && count($this->filter_funcs) > 0) {
foreach($this->filter_funcs as $curr_func) {
if(function_exists($curr_func)) {
$template_source = $curr_func($template_source);
$template_source = $curr_func($template_source);
} else {
$this->_trigger_error_msg("filter function $curr_func does not exist.");
$this->_trigger_error_msg("filter function $curr_func does not exist.");
}
}
}
}
$this->_current_file = $tpl_file;
@@ -230,18 +230,37 @@ class Smarty_Compiler extends Smarty {
return $this->_compile_insert_tag($tag_args);
default:
if (isset($this->custom_funcs[$tag_command])) {
return $this->_compile_custom_tag($tag_command, $tag_args);
} else {
var_dump($tag_command);
if (isset($this->compiler_funcs[$tag_command])) {
return $this->_compile_compiler_tag($tag_command, $tag_args);
} else if (isset($this->custom_funcs[$tag_command])) {
return $this->_compile_custom_tag($tag_command, $tag_args);
} else {
$this->_syntax_error("unknown tag - '$tag_command'", E_USER_WARNING);
return;
}
}
}
/*======================================================================*\
Function: _compile_compiler_tag
Purpose: compile the custom compiler tag
\*======================================================================*/
function _compile_compiler_tag($tag_command, $tag_args)
{
$function = $this->compiler_funcs[$tag_command];
if (!function_exists($function)) {
$this->_syntax_error("compiler function '$tag_command' is not implemented", E_USER_WARNING);
return;
}
return '<?php' . $function($tag_args, $this) . ' ?>';
}
/*======================================================================*\
Function: _compile_custom_tag
Purpose: compile custom tag
Purpose: compile custom function tag
\*======================================================================*/
function _compile_custom_tag($tag_command, $tag_args)
{

View File

@@ -68,4 +68,5 @@ This is an example of the html_options function:
</select>
</form>
{myfunc attr1=5 attr2=$Name|upper}
{include file="footer.tpl"}

View File

@@ -103,6 +103,9 @@ class Smarty
var $left_delimiter = '{'; // template tag delimiters.
var $right_delimiter = '}';
var $compiler_funcs = array(
);
var $custom_funcs = array( 'html_options' => 'smarty_func_html_options',
'html_select_date' => 'smarty_func_html_select_date',
'html_select_time' => 'smarty_func_html_select_time',
@@ -132,9 +135,9 @@ class Smarty
var $compiler_class = 'Smarty_Compiler'; // the compiler class used by
// Smarty to compile templates
var $resource_functions = array(); // what functions resource handlers are mapped to
var $filter_functions = array(); // what functions templates are filtered through
// before being compiled
var $resource_funcs = array(); // what functions resource handlers are mapped to
var $filter_funcs = array(); // what functions templates are filtered through
// before being compiled
/**************************************************************************/
/* END SMARTY CONFIGURATION SECTION */
@@ -246,6 +249,23 @@ class Smarty
unset($this->custom_funcs[$function]);
}
/*======================================================================*\
Function: register_compiler_function
Purpose: Registers compiler function
\*======================================================================*/
function register_compiler_function($function, $function_impl)
{
$this->compiler_funcs[$function] = $function_impl;
}
/*======================================================================*\
Function: unregister_compiler_function
Purpose: Unregisters compiler function
\*======================================================================*/
function unregister_compiler_function($function)
{
unset($this->compiler_funcs[$function]);
}
/*======================================================================*\
Function: register_modifier
@@ -271,7 +291,7 @@ class Smarty
\*======================================================================*/
function register_resource($name, $function_name)
{
$this->resource_functions[$name] = $function_name;
$this->resource_funcs[$name] = $function_name;
}
/*======================================================================*\
@@ -280,7 +300,7 @@ class Smarty
\*======================================================================*/
function unregister_resource($name)
{
unset($this->resource_functions[$name]);
unset($this->resource_funcs[$name]);
}
/*======================================================================*\
@@ -290,7 +310,7 @@ class Smarty
\*======================================================================*/
function register_filter($function_name)
{
$this->filter_functions[] = $function_name;
$this->filter_funcs[] = $function_name;
}
/*======================================================================*\
@@ -300,12 +320,12 @@ class Smarty
function unregister_filter($function_name)
{
$tmp_array = array();
foreach($this->filter_functions as $curr_func) {
foreach($this->filter_funcs as $curr_func) {
if($curr_func != $function_name) {
$tmp_array[] = $curr_func;
}
}
$this->filter_functions = $tmp_array;
$this->filter_funcs = $tmp_array;
}
/*======================================================================*\
@@ -587,8 +607,8 @@ class Smarty
}
break;
default:
if(isset($this->resource_functions[$resource_type])) {
$funcname = $this->resource_functions[$resource_type];
if(isset($this->resource_funcs[$resource_type])) {
$funcname = $this->resource_funcs[$resource_type];
if(function_exists($funcname)) {
// call the function to fetch the template
$funcname($resource_name,$template_source,$template_timestamp);
@@ -629,7 +649,8 @@ class Smarty
$smarty_compiler->custom_funcs = $this->custom_funcs;
$smarty_compiler->custom_mods = $this->custom_mods;
$smarty_compiler->version = $this->version;
$smarty_compiler->filter_functions = $this->filter_functions;
$smarty_compiler->filter_funcs = $this->filter_funcs;
$smarty_compiler->compiler_funcs = $this->compiler_funcs;
if ($smarty_compiler->_compile_file($tpl_file, $template_source, $template_compiled))
return true;

View File

@@ -54,15 +54,15 @@ class Smarty_Compiler extends Smarty {
function _compile_file($tpl_file, $template_source, &$template_compiled)
{
// run template source through functions registered in filter_functions
if(is_array($this->filter_functions) && count($this->filter_functions) > 0) {
foreach($this->filter_functions as $curr_func) {
// run template source through functions registered in filter_funcs
if(is_array($this->filter_funcs) && count($this->filter_funcs) > 0) {
foreach($this->filter_funcs as $curr_func) {
if(function_exists($curr_func)) {
$template_source = $curr_func($template_source);
$template_source = $curr_func($template_source);
} else {
$this->_trigger_error_msg("filter function $curr_func does not exist.");
$this->_trigger_error_msg("filter function $curr_func does not exist.");
}
}
}
}
$this->_current_file = $tpl_file;
@@ -230,18 +230,37 @@ class Smarty_Compiler extends Smarty {
return $this->_compile_insert_tag($tag_args);
default:
if (isset($this->custom_funcs[$tag_command])) {
return $this->_compile_custom_tag($tag_command, $tag_args);
} else {
var_dump($tag_command);
if (isset($this->compiler_funcs[$tag_command])) {
return $this->_compile_compiler_tag($tag_command, $tag_args);
} else if (isset($this->custom_funcs[$tag_command])) {
return $this->_compile_custom_tag($tag_command, $tag_args);
} else {
$this->_syntax_error("unknown tag - '$tag_command'", E_USER_WARNING);
return;
}
}
}
/*======================================================================*\
Function: _compile_compiler_tag
Purpose: compile the custom compiler tag
\*======================================================================*/
function _compile_compiler_tag($tag_command, $tag_args)
{
$function = $this->compiler_funcs[$tag_command];
if (!function_exists($function)) {
$this->_syntax_error("compiler function '$tag_command' is not implemented", E_USER_WARNING);
return;
}
return '<?php' . $function($tag_args, $this) . ' ?>';
}
/*======================================================================*\
Function: _compile_custom_tag
Purpose: compile custom tag
Purpose: compile custom function tag
\*======================================================================*/
function _compile_custom_tag($tag_command, $tag_args)
{

View File

@@ -68,4 +68,5 @@ This is an example of the html_options function:
</select>
</form>
{myfunc attr1=5 attr2=$Name|upper}
{include file="footer.tpl"}