深圳全飞鸿

标题: smarty3中block的研究 [打印本页]

作者: zhgc    时间: 2019-9-4 21:57
标题: smarty3中block的研究
本帖最后由 zhgc 于 2019-9-11 15:08 编辑

函数结构:
function smarty_block_translate($params, $content, &$smarty, &$repeat)    //smarty2
function smarty_block_translate($params, $content, Smarty_Internal_Template $template, &$repeat)   //smarty3
判断的官方方法:
  1.     // 只在结束标签时输出
  2.     if(!$repeat){
  3.         if (isset($content)) {
  4.             $lang = $params['lang'];
  5.             // 翻译$content的内容
  6.             return $translation;
  7.         }
  8.     }
复制代码

$repeat默认只有第一次为ture, $content默认第一次为null



1. 研究函数体内smarty的数据类型
加入以下代码,得到类名称
  1.         function smarty_block_nagomenu($params, $content, &$smarty, &$repeat) {
  2.                  // Smarty_Internal_Template
  3.                 trigger_error(get_class($smarty), E_USER_NOTICE);
  4.                  // MySmarty
  5.                 trigger_error(get_class($smarty->smarty), E_USER_NOTICE);
  6.         }
复制代码


2. 研究_tag_stack对象

加入以下代码
  1.         function smarty_block_syant($params, $content, &$smarty, &$repeat) {
  2.                 echo $content;
  3.                 var_dump($smarty->smarty->_tag_stack);
  4.         }
复制代码


模板内容:
{syant  aa=1 bb=2}
   {wj mm=9}{/wj}
{/syant}
得到的结构:
一层:
array(1) {
  [0]=> array(2) {
    [0]=> string(5) "syant"
    [1]=>  array(2) {   ["aa"]=> int(1)  ["bb"]=>  int(2) }
  }
}
二层:
array(2) {
  [0]=> array(2) {
    [0]=> string(5) "syant"
    [1]=> array(2) {  ["aa"]=> int(1)  ["bb"]=>   int(2) }
  }
  [1]=> array(2) {
    [0]=>  string(2) "wj"
    [1]=>  array(1) {   ["mm"]=>   int(9) }
  }
}

结论:
1. count来判断层级
$l = count($template->smarty->_tag_stack);

2. 0为名称,1为参数数据

作者: zhgc    时间: 2019-9-4 22:46
$template->smarty的属性如下:

auto_literal = 1
error_unassigned =
use_include_path =
_templateDirNormalized = 1
_joined_template_dir = C:\Apache24\htdocs\rb\templates\
_configDirNormalized =
_joined_config_dir =
default_template_handler_func =
default_config_handler_func =
default_plugin_handler_func =
_compileDirNormalized = 1
_pluginsDirNormalized =
_cacheDirNormalized =
force_compile =
use_sub_dirs =
allow_ambiguous_resources =
merge_compiled_includes =
extends_recursion = 1
force_cache =
left_delimiter = {
right_delimiter = }
literals =>  array  
security_class = Smarty_Security
security_policy =
php_handling = 0
allow_php_templates =
debugging =
debugging_ctrl = NONE
smarty_debug_id = SMARTY_DEBUG
debug_tpl =
error_reporting =
config_overwrite = 1
config_booleanize = 1
config_read_hidden =
compile_locking = 1
cache_locking =
locking_timeout = 10
default_resource_type = file
caching_type = file
default_config_type = file
cache_modified_check =
registered_plugins =>  array  
registered_objects =>  array  
registered_classes =>  array  
registered_filters =>  array  
registered_resources =>  array  
registered_cache_resources =>  array  
autoload_filters =>  array  
default_modifiers =>  array  
escape_html =
start_time = 1567607980.783
_current_file =
_parserdebug =
_objType = 1
_debug =
cache_id =
compile_id =
caching = 0
compile_check = 1
cache_lifetime = 3600
tplFunctions =>  array  
_cache =>  array  
template_class = Smarty_Internal_Template
tpl_vars =>  array  
parent =
config_vars =>  array  
ext => Smarty_Internal_Extension_Handler
作者: zhgc    时间: 2019-9-4 22:50
$template的属性如下:
  1. function smarty_block_syant($params, $content, &$template, &$repeat) {
  2.         foreach($template as $k=>$v){
  3.                 if(is_object($v)){
  4.                 echo $k." => ".get_class($v)."\r\n";}
  5.                 else if(is_array($v)){
  6.                         echo $k." => "." array  "."\r\n";
  7.                 }else{
  8.                         echo $k." = ".$v."\r\n";
  9.                 }       
  10.         }
  11. }
复制代码



_objType = 2
smarty => Smarty
source => Smarty_Template_Source
inheritance =
template_resource = t.tpl
mustCompile =
templateId = C:\Apache24\htdocs\rb\templates\#file:t.tpl###0
scope = 0
isRenderingCache =
startRenderCallbacks =>  array  
endRenderCallbacks =>  array  
cache_id =
compile_id =
caching = 0
compile_check = 1
cache_lifetime = 3600
tplFunctions =>  array  
_cache =>  array  
template_class = Smarty_Internal_Template
tpl_vars =>  array  
parent => Smarty
config_vars =>  array  
ext => Smarty_Internal_Extension_Handler
compiled => Smarty_Template_Compiled

作者: zhgc    时间: 2019-9-4 22:59
本帖最后由 zhgc 于 2019-9-4 23:04 编辑

$template->tpl_vars 和$template->smarty->tpl_vars两者分析!

$smarty->smarty->tpl_vars的内容:

array(2) {
  ["name"]=>
  object(Smarty_Variable)#4 (2) {
    ["value"]=>
    string(9) "刘二狗"
    ["nocache"]=>
    bool(false)
  }
  ["sex"]=>
  object(Smarty_Variable)#5 (2) {
    ["value"]=>
    string(3) "man"
    ["nocache"]=>
    bool(false)
  }
}


$smarty->tpl_vars的内容:

array(3) {
  ["SCRIPT_NAME"]=>
  object(Smarty_Variable)#3 (2) {
    ["value"]=>
    string(9) "/rb/t.php"
    ["nocache"]=>
    bool(false)
  }
  ["name"]=>
  object(Smarty_Variable)#4 (2) {
    ["value"]=>
    string(9) "刘二狗"
    ["nocache"]=>
    bool(false)
  }
  ["sex"]=>
  object(Smarty_Variable)#5 (2) {
    ["value"]=>
    string(3) "man"
    ["nocache"]=>
    bool(false)
  }
}

结论是两个的内容是不同的!





作者: zhgc    时间: 2019-9-5 14:32
$template->_cache和$template->smarty->_cache两者分析!

var_dump($smarty->_cache);
array(0) {
}


var_dump($smarty->smarty->_cache);
array(3) {
  ["resource_handlers"]=>
  array(1) {
    ["file"]=>
    object(Smarty_Internal_Resource_File)#8 (3) {
      ["uncompiled"]=>
      bool(false)
      ["recompiled"]=>
      bool(false)
      ["hasCompiledHandler"]=>
      bool(false)
    }
  }
  ["cacheresource_handlers"]=>
  array(1) {
    ["file"]=>
    object(Smarty_Internal_CacheResource_File)#11 (0) {
    }
  }
  ["_tag_stack"]=>
  array(1) {
    [0]=>
    array(2) {
      [0]=>
      string(5) "syant"
      [1]=>
      array(2) {
        ["aa"]=>
        int(1)
        ["bb"]=>
        int(2)
      }
    }
  }
}








欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/) Powered by Discuz! X3.2