一 smarty 是什么
Smarty是一个PHP的模板引擎。更明确来说,它可以帮助开发者更好地 分离程序逻辑和页面显示。最好的例子,是当程序员和模板设计师是不同的两个角色的情况,而且 大部分时候都不是同一个人的情况。smarty 从开发层面上保证了程序员的"单一职责性Single"和"接口隔离 Interface Separate",PHP程序员更集中于逻辑处理,前端程序员更集中于页面展示.程序员修改业务逻辑不会影响前台显示,页面工程师修改页面不会影响逻辑.
二 smarty原理
tpl文件编译成php文件,smarty变量用php变量替换掉,然后输出到浏览器.贴上部分代码.
1 class Smarty extends Smarty_Internal_TemplateBase 2 { 3 const SMARTY_VERSION = '3.1.27'; 4 ... 5 const PLUGIN_MODIFIERCOMPILER = 'modifiercompiler'; 6 public static $global_tpl_vars = array(); 7 Smarty::muteExpectedErrors() 8 public static $_previous_error_handler = null; 9 public static $_muted_directories = array('./templates_c/' => null, './cache/' => null); 10 11 public static $_MBSTRING = SMARTY_MBSTRING; 12 public static $_CHARSET = SMARTY_RESOURCE_CHAR_SET; 13 public static $_DATE_FORMAT = SMARTY_RESOURCE_DATE_FORMAT; 14 public static $_UTF8_MODIFIER = 'u'; 15 public static $_IS_WINDOWS = false; 16 private $template_dir = array('./templates/'); 17 public $joined_template_dir = './templates/'; 18 public $joined_config_dir = './configs/'; 19 private $compile_dir = './templates_c/'; 20 private $plugins_dir = null; 21 private $cache_dir = './cache/'; 22 private $config_dir = array('./configs/'); 23 public $force_compile = false; 24 public $merge_compiled_includes = false; 25 public $inheritance_merge_compiled_includes = true; 26 public $force_cache = false; 27 public $left_delimiter = "{"; 28 public $right_delimiter = "}"; 29 ... 30 /** 31 * Initialize new Smarty object 32 */ 33 public function __construct() 34 { 35 if (is_callable('mb_internal_encoding')) { 36 mb_internal_encoding(Smarty::$_CHARSET); 37 } 38 $this->start_time = microtime(true); 39 // check default dirs for overloading 40 if ($this->template_dir[0] !== './templates/' || isset($this->template_dir[1])) { 41 $this->setTemplateDir($this->template_dir); 42 } 43 if ($this->config_dir[0] !== './configs/' || isset($this->config_dir[1])) { 44 $this->setConfigDir($this->config_dir); 45 } 46 if ($this->compile_dir !== './templates_c/') { 47 unset(self::$_muted_directories['./templates_c/']); 48 $this->setCompileDir($this->compile_dir); 49 } 50 if ($this->cache_dir !== './cache/') { 51 unset(self::$_muted_directories['./cache/']); 52 $this->setCacheDir($this->cache_dir); 53 } 54 if (isset($this->plugins_dir)) { 55 $this->setPluginsDir($this->plugins_dir); 56 } else { 57 $this->setPluginsDir(SMARTY_PLUGINS_DIR); 58 } 59 if (isset($_SERVER['SCRIPT_NAME'])) { 60 Smarty::$global_tpl_vars['SCRIPT_NAME'] = new Smarty_Variable($_SERVER['SCRIPT_NAME']); 61 } 62 63 // Check if we're running on windows 64 Smarty::$_IS_WINDOWS = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; 65 66 // let PCRE (preg_*) treat strings as ISO-8859-1 if we're not dealing with UTF-8 67 if (Smarty::$_CHARSET !== 'UTF-8') { 68 Smarty::$_UTF8_MODIFIER = ''; 69 } 70 } 71 72 /** 73 * fetches a rendered Smarty template 74 * 75 */ 76 public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false) 77 { 78 if ($cache_id !== null && is_object($cache_id)) { 79 $parent = $cache_id; 80 $cache_id = null; 81 } 82 if ($parent === null) { 83 $parent = $this; 84 } 85 // get template object 86 $_template = is_object($template) ? $template : $this->createTemplate($template, $cache_id, $compile_id, $parent, false); 87 // set caching in template object 88 $_template->caching = $this->caching; 89 // fetch template content 90 return $_template->render(true, false, $display); 91 } 92 93 /** 94 * displays a Smarty template 95 * 96 */ 97 public function display($template = null, $cache_id = null, $compile_id = null, $parent = null) 98 { 99 // display template 100 $this->fetch($template, $cache_id, $compile_id, $parent, true); 101 } 102 103 /** 104 * Check if a template resource exists 105 */ 106 public function templateExists($resource_name) 107 { 108 $save = $this->template_objects; 109 $tpl = new $this->template_class($resource_name, $this); 110 $result = $tpl->source->exists; 111 $this->template_objects = $save; 112 113 return $result; 114 } 115 116 public function __get($name) 117 { 118 $allowed = array('template_dir' => 'getTemplateDir', 'config_dir' => 'getConfigDir', 119 'plugins_dir' => 'getPluginsDir', 'compile_dir' => 'getCompileDir', 120 'cache_dir' => 'getCacheDir',); 121 122 if (isset($allowed[$name])) { 123 return $this->{$allowed[$name]}(); 124 } else { 125 trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE); 126 } 127 } 128 129 /** 130 * <<magic>> Generic setter. 131 */ 132 public function __set($name, $value) 133 { 134 $allowed = array('template_dir' => 'setTemplateDir', 'config_dir' => 'setConfigDir', 135 'plugins_dir' => 'setPluginsDir', 'compile_dir' => 'setCompileDir', 136 'cache_dir' => 'setCacheDir',); 137 138 if (isset($allowed[$name])) { 139 $this->{$allowed[$name]}($value); 140 } else { 141 trigger_error('Undefined property: ' . get_class($this) . '::$' . $name, E_USER_NOTICE); 142 } 143 }
实现思想不算难,但是第一:已经有成熟的工具smarty了,所以不必重复开发,而且smarty实现的功能足够强大;第二:smarty 的受众足够广,学习成本低.
所以,不再开发,整合smarty到框架里面吧.
三 整合
1.下载smarty :
1 https://github.com/phpseasa/smarty
2.整合进框架,源码上面已经分析过,大部分的配置在构造方法里.
所以直接引入.奉上代码.
1 /** 2 * @ file smarty 入口文件 3 * @ author yuxing@sina.book.com 4 */ 5 6 //define('SMARTY_PATH',dirname(__FILE__).'/'); 7 //echo SMARTY_PATH.'Smarty.class'.EXT;exit; 8 //require_once(SMARTY_PATH.'Smarty.class'.EXT); 9 10 class Mysmarty extends Smarty{ 11 public function __construct() 12 { 13 parent::__construct(); 14 $this->template_dir = APP_PATH."views"; 15 $this->compile_dir = APP_PATH."templates_c"; 16 $this->cache_dir = APP_PATH."cache"; 17 $this->caching = 0; 18 $this->debugging = true; 19 $this->compile_check = true; 20 $this->left_delimiter = "{"; 21 $this->right_delimiter = "}"; 22 } 23 }
如此配置完毕,
1 require_once(SYS_PATH.'smarty'.DIRECTORY_SEPARATOR.'smarty'.EXT); 2 class Controller { 3 private static $instance; 4 /** 5 * Constructor 6 */ 7 public function __construct() 8 { 9 self::$instance =& $this; 10 $this->smarty = new Mysmarty()
在控制器中调用,
1 public function __construct(){ 2 parent::__construct(); 3 } 4 public function test(){ 5 $smarty=$this->smarty; 6 // var_dump($this->smarty); 7 $smarty->assign('test','111'); 8 $smarty->display('test.tpl'); 9 }
写模板文件
1 this is a test framework ,there are so many things not going well!!! 2 3 {$test}
页面触发
同时弹出了smarty的debug页面.
well ,done!!
本站所有博客皆是原创,如果转载请标明出处.小三爷在此多谢了!~~