$default_config_handler_func

提供一个回调函数,这个回调函数将在从资源里面无法获取到配置文件的时候调用。

Note

该设置一般在当配置文件是文件资源的时候起作用。 当找不到资源自身的时候,该设置不会调用,而是会抛出SmartyException异常。


Example 13.4. $default_config_handler_func

  1. <?php
  2.  
  3. $smarty = new Smarty();
  4. $smarty->default_config_handler_func = 'my_default_config_handler_func';
  5.  
  6. /**
  7. * 默认配置文件处理函数
  8. *
  9. * 当Smarty的文件资源无法载入需要的文件时调用。
  10. *
  11. * @param string $type 资源类型 (如: "file", "string", "eval", "resource")
  12. * @param string $name 资源名称 (如: "foo/bar.tpl")
  13. * @param string &$content 配置内容
  14. * @param integer &$modified 配置的修改时间
  15. * @param Smarty $smarty Smarty 实例
  16. * @return string|boolean 返回文件路径或布尔值,
  17. * 布尔值true表示$content 和 $modified已经赋值,
  18. * 布尔值false表示没有默认配置文件可供载入。
  19. */
  20. function my_default_config_handler_func($type, $name, &$content, &$modified, Smarty $smarty) {
  21. if (false) {
  22. // 返回文件路径
  23. return "/tmp/some/foobar.tpl";
  24. } elseif (false) {
  25. // 直接返回配置内容
  26. $content = 'someVar = "the config source"';
  27. $modified = time();
  28. return true;
  29. } else {
  30. // 告诉Smarty无法找到配置文件
  31. return false;
  32. }
  33. }
  34.  
  35. ?>
  36.  

原文: https://www.smarty.net/docs/zh_CN/variable.default.config.handler.func.tpl