DolrPHP模板引擎DolrViews分享
【分享】DolrPHP模板引擎DolrViews分享
核心文件:DolrViews.class.php:
- /**
- * DolrPHP模板引擎
- * @author Joychao
- * @version 1.0 beta
- * @license http://www.Joychao.cc
- */
- defined('DOLRVIEWS') or define('DOLRVIEWS',true);
- defined('DIR_SEP') or define('DIR_SEP',DIRECTORY_SEPARATOR);
- class DolrViews
- {
- /**
- * 单例对象
- *
- * @static var
- * @var object DolrViews
- */
- protected static $_instance;
- /**
- * 模板变量
- *
- * @var array
- */
- protected $_tpl_vars=array();
- /**
- * 模板参数信息
- *
- * @var array
- */
- protected $_options=array(
- 'template_dir'=>'templates',
- 'compile_dir'=>'templates_c',
- 'cache_dir'=>'cache',
- 'caching'=>false,
- 'cache_lifetime'=>3600,
- 'plugins_dir'=>'plugins',
- 'tpl_suffix'=>'html',
- 'php_handing'=>false,
- );
- /**
- * 包含的插件
- * @var array
- */
- protected $includePlugins=array();
- /**
- * 主模板非包含模板
- * @var string
- */
- protected $mainTplFileName;
- /**
- * 单件模式调用方法
- *
- * @static
- * @return object DolrViews
- */
- public static function getInstance($options=array()) {
- if (!self :: $_instance instanceof self)
- self :: $_instance = new self($options);
- return self :: $_instance;
- }
- /**
- * 构造函数,初始化所有配置
- *
- * @param array $config=array(); 配置数组
- * @example:
- * $_options=array(
- * 'template_dir'=>'templates',
- * 'compile_dir'=>'templates_c',
- * 'cache_dir'=>'cache',
- * 'caching'=>false,
- * 'cache_lifetime'=>3600,
- * 'plugins_dir'=>'plugins',
- * 'php_handing'=>false,
- * );
- * $tpl=new DolrViews($_options);
- */
- public function __construct($options=array())
- {
- if (!empty($options))
- {
- $this->setOptions($options);
- }
- }
- /**
- * 分配变量到模板
- *
- * @param string $varName 变量名
- * @param mixed $varValue 变量值
- */
- public function assign($varName,$varValue)
- {
- $this->_tpl_vars[$varName]=&$varValue;
- }
- /**
- * 显示输出到页面
- *
- * @param string $tplFileName 模板文件名
- */
- public function display($tplFileName,$cacheId=null)
- {
- $cacheId=is_null($cacheId)?$_SERVER['REQUEST_URI']:$cacheId;
- $this->mainTplFileName=$tplFileName;
- $tplFilePath=$this->_getTplPath($tplFileName);
- extract($this->_tpl_vars);
- //内置变量
- extract($this->_getSystemVars($tplFileName));//系统变量
- //是否缓存
- if($this->_options['caching']){
- if($this->isCached($tplFileName,$cacheId)){
- include $this->_getCacheFile($tplFileName,$cacheId);
- }else{//否则缓存文件
- include $this->_writeCache($tplFileName,$this->_parseTpl($tplFileName,$cacheId),$cacheId);// 写入缓存
- }
- }else{//非缓存模式
- include $this->_parseTpl($tplFileName,$cacheId);//解析写入编译文件并包含
- }
- }
- /**
- * 配置模板选项
- *
- * @param array $options 配置数组
- */
- public function setOptions($options)
- {
- foreach ($options as $optionName => $optionValue) {
- $this->set($optionName,$optionValue);
- }
- }
- /**
- * 设置选项
- *
- * @param string $optionName 选项名称
- * @param mixed $optionValue 选项值
- */
- public function __set($optionName,$optionValue)
- {
- $this->set($optionName,$optionValue);
- }
- /**
- * 单独设置配置
- *
- * @param string $optionName 选项名称
- * @param mixed $optionValue 选项值
- */
- public function set($optionName,$optionValue)
- {
- switch (strtolower($optionName)) {
- case 'template_dir':
- $optionValue=$this->_trimPath($optionValue).DIR_SEP;
- if(!file_exists($optionValue))
- $this->_throwException('未找到指定的模板目录"'.$optionValue.'"');
- $this->_options['template_dir']=$optionValue;
- break;
- case 'compile_dir':
- $optionValue=$this->_trimPath($optionValue).DIR_SEP;
- if(!file_exists($optionValue))
- $this->_throwException('未找到指定的编译目录"'.$optionValue.'"');
- $this->_options['compile_dir']=$optionValue;
- break;
- case 'cache_dir':
- $optionValue=$this->_trimPath($optionValue).DIR_SEP;
- if(!file_exists($optionValue))
- $this->_throwException('未找到指定的缓存目录"'.$optionValue.'"');
- $this->_options['cache_dir']=$optionValue;
- break;
- case 'plugins_dir':
- $optionValue=$this->_trimPath($optionValue).DIR_SEP;
- if(!file_exists($optionValue))
- $this->_throwException('未找到指定的缓存目录"'.$optionValue.'"');
- $this->_options['plugins_dir']=$optionValue;
- break;
- case 'caching':
- $this->_options['caching']=(boolean)$optionValue;
- break;
- case 'cache_lifetime':
- $this->_options['cache_lifetime']=(float)$optionValue;
- break;
- case 'php_handing':
- $this->_options['php_handing']=(boolean)$optionValue;
- break;
- case 'tpl_suffix':
- $this->_options['tpl_suffix']=trim($optionValue);
- break;
- default:
- $this -> _throwException("未知的模板配置选项 \"$optionName\"");
- }
- }
- /**
- * 清除模板缓存
- *
- * @param string $tplFileName='' 模板文件名,不传入则删除所有缓存
- * @return boolean 成功或者失败
- */
- public function clearCache($tplFileName='',$cacheId=null)
- {
- $cacheId=is_null($cacheId)?$_SERVER['REQUEST_URI']:$cacheId;
- if(!empty($tplFileName)){
- $cacheFile=$this->_getCacheFile($tplFileName,$cacheId);
- if(file_exists($cacheFile)){
- chmod($cacheFile, 0777);
- @unlink($cacheFile);
- }
- }else{//删除所有缓存文件
- foreach (glob($this->_options['cache_dir'].'*') as $cacheFile) {
- chmod($cacheFile, 0777);
- @unlink($cacheFile);
- }
- }
- }
- /**
- * 检测是否缓存了指定模板文件
- *
- * @param string $tplFileName 模板文件名
- * @return boolean
- */
- public function isCached( $tplFileName,$cacheId=null)
- {
- $tplFilePath=$this->_getTplPath($tplFileName);
- $cacheId=is_null($cacheId)?$_SERVER['REQUEST_URI']:$cacheId;
- $cacheFile=$this->_getCacheFile($tplFileName,$cacheId);
- if(file_exists($cacheFile) //存在
- and filemtime($cacheFile)+$this->_options['cache_lifetime']>time()//未过期
- and filemtime($cacheFile)>filemtime($tplFilePath) //模板没有改动
- ){//存在并没过期
- return true;
- }else{
- return false;
- }
- }
- /**
- * 获取内置变量
- * @return array
- */
- protected function _getSystemVars($tplFileName){
- //内置变量
- $_sysVars=array();
- $_sysVars['dolr_now']=time();
- $_sysVars['dolr_get']=$_GET;
- $_sysVars['dolr_post']=$_POST;
- $_sysVars['dolr_request']=$_REQUEST;
- $_sysVars['dolr_cookie']=isset($_COOKIE)?$_COOKIE:null;
- $_sysVars['dolr_session']=isset($_SESSION)?$_SESSION:null;
- $_sysVars['dolr_template']=basename($tplFileName);
- $const=get_defined_constants(true);
- $_sysVars['dolr_const']=$const['user'];
- $_sysVars['dolr_url']="http://".$_SERVER ['HTTP_HOST'].$_SERVER['PHP_SELF'];
- if(!empty($_SERVER['QUERY_STRING'])){
- $_sysVars['dolr_url'].='?'.$_SERVER['QUERY_STRING'];
- }
- return $_sysVars;
- }
- /**
- * 获取模板文件路径
- *
- * @param string $tplFileName 模板文件
- * @return string 文件名
- */
- protected function _getTplPath($tplFileName)
- {
- return $this->_options['template_dir'].$this->_trimPath($tplFileName);
- }
- /**
- * 获取缓存的文件
- * @param string $tplFileName 模板文件
- * @return string 文件名
- */
- protected function _getCacheFile($tplFileName, $cacheId)
- {
- return $this->_options['cache_dir'].$tplFileName.'.cache.'.md5($cacheId).'.php';
- }
- /**
- * 获取编译的文件名
- *
- * @param string $tplFileName 模板文件
- * @return string 文件名
- */
- protected function _getCompileFile( $tplFileName, $cacheId)
- {
- return $this->_options['compile_dir'].$tplFileName.'.compile.php';
- }
- /**
- * 解析模板
- *
- * @param string $tplFileName 模板文件
- * @return string 解析后的内容
- */
- protected function _parseTpl($tplFileName,$cacheId)
- {
- $tplFilePath=$this->_getTplPath($tplFileName);
- if (!file_exists($tplFilePath) or !is_readable($tplFilePath)) {
- $this->_throwException('不能打开指定的模板文件"'.$tplFileName.'"');
- }
- $content=file_get_contents($tplFilePath);
- //检测包含,检测所有的include 'xxx.html'
- preg_match_all('/[\n\r\t]*[\n\r\t]*/s', $content, $matches);
- if(!empty($matches[1])){
- foreach ($matches[1] as $key=>$fileName) {
- $includeFilePath=$this->_getTplPath($fileName);
- if (!file_exists($includeFilePath) or !is_readable($includeFilePath)) {
- $this->_throwException('不能打开指定的模板文件"'.$includeFilePath.'"');
- }
- $includeCompilePath=$this->_getCompileFile($fileName,$cacheId);//得到编译文件名
- $this->_parseTpl($fileName,$cacheId);
- $content=str_replace($matches[0][$key], '', $content);
- }
- }
- //规则
- $pattern=array(
- '/ \?\>[\n\r]*\
- '/(]+)\s*>)/es',//直接输出变量内容
- '//s',//包含模板
- '/(.+?)/ies',//if /if
- '//ies',//elseif
- '//is',//else
- '/(.+?)
(.+?)/ies',//loop $array $v - '/(.+?)/ies',//loop $array $v
- '/(.+?)
(.+?)/ies',//loop $array $k $v loopelse - '/(.+?)/ies',//loop $array $k $v
- '//',//
- );
- //PHP格式
- $php=array(
- ' ',
- "\$this->_parseVar('\\1')",
- '_options['template_dir']."\\1';?>",
- "\$this->_stripvtags('','\\2');",
- "\$this->_stripvtags('','');",
- '',
- "\$this->_stripvtags('','\\3\\4');",
- "\$this->_stripvtags('','\\3\n');",
- "\$this->_stripvtags(' \\3) { ?>','\\4\\5');",
- "\$this->_stripvtags(' \\3) { ?>','\\4');",
- "",
- );
- $content=preg_replace($pattern, $php, $content);
- //包含插件
- $pluginsString='';
- if($tplFileName==$this->mainTplFileName){//如果为主模板则放入include文件
- foreach ($this->includePlugins as $plugin) {
- $pluginsString.="include \$this->_options['plugins_dir'].'{$plugin}';\n";
- }
- }
- $header=
- /**
- * {$tplFileName}
- * DolrViews 模板编译文件
- * @package {$this->mainTplFileName}
- */
- defined('DOLRVIEWS') or exit('Access denied');
- $pluginsString
- ?>\n
- DOLRVIEWS;
- $content=$header.$content;
- $compileFilePath=$this->_writeCompile($tplFileName,$content,$cacheId);//写入编译文件
- return $compileFilePath;
- }
- /**
- * 缓存模板文件
- *
- * @param string $tplFileName 模板文件
- * @param string $cacheId 缓存ID
- */
- protected function _writeCache($tplFileName,$cacheId)
- {
- $tplFilePath=$this->_getTplPath($tplFileName);
- $cacheFilePath=$this->_getCacheFile($tplFileName,$cacheId);//保存文件名
- $compileFilePath=$this->_getCompileFile($tplFileName,$cacheId);
- ini_set('error_reporting','off');//不缓存错误提示
- extract($this->_tpl_vars);//模板变量
- extract($this->_getSystemVars($tplFileName));//系统变量
- ob_start();
- if(file_exists($compileFilePath) and filemtime($compileFilePath)>filemtime($tplFilePath))//模板没有改动
- {
- include $compileFilePath;
- }else{
- include $this->_parseTpl($tplFileName,$cacheId);//解析并写入编译文件
- }
- $html=ob_get_contents();
- ob_clean();
- file_put_contents($cacheFilePath, $html);
- return $cacheFilePath;
- }
- /**
- * 写入编译文件
- *
- * @param string $tplFileName 模板文件
- * @param string $cacheId 缓存ID
- * @param string $content 网页
- */
- protected function _writeCompile($tplFileName,$content,$cacheId)
- {
- $compileFilePath=$this->_getCompileFile($tplFileName,$cacheId);//保存文件名
- if(!file_exists(dirname($compileFilePath))){
- $this->_makeDir(dirname($compileFilePath));
- }
- file_put_contents($compileFilePath,$content);
- return $compileFilePath;
- }
- /**
- * 将路径修正为适合操作系统的形式
- *
- * @param string $path 路径名称
- * @return string
- */
- protected function _trimPath( $path)
- {
- return rtrim(str_replace(array('/', '\\', '//', '\\\\'),DIR_SEP, $path),DIR_SEP);
- }
- /**
- * 根据指定的路径创建不存在的文件夹
- *
- * @param string $path 路径/文件夹名称
- * @return string
- */
- protected function _makeDir( $path)
- {
- $dirs = explode(DIR_SEP, $this ->_trimPath($path));
- $tmp = '';
- foreach ($dirs as $dir)
- {
- $tmp .= $dir . DIR_SEP;
- if (!file_exists($tmp) && !@mkdir($tmp, 0777))
- return $tmp;
- }
- return true;
- }
- /**
- * 变量处理
- *
- * @param string $string 目标字符串
- * @return string
- */
- protected function _parseVar($string){
- $pattern=array(
- '/^',
- '/>$/',
- '/(\$\w+\|[^>\s]+)/e',//$title|striptags|html2text
- '/(\$[\w]+\.[\w]+)/e',
- );
- $replacement=array(
- "
- ' ?>',
- "\$this->_parseModifier('\\1');",
- "\$this->_parsePoint('\\1');",
- );
- return stripslashes(preg_replace($pattern, $replacement, $string));
- }
- /**
- * 变量调节器的处理
- *
- * @param string $string 模板中匹配到的变量
- * @return string 处理后的字符串
- */
- protected function _parseModifier($string)
- {
- $arr=explode('|', trim($string,';'));
- $tmp=array_shift($arr);
- foreach($arr as $value)
- {
- $tmpArr=explode(':',$value);//html2text
- $funcName=array_shift($tmpArr);//html2text
- $args=count($tmpArr)>0?','.join(',',$tmpArr):'';//参数用,号链接 arg1,arg2,arg3
- if(!function_exists($funcName)){//如果不是PHP内置函数则包含插件
- if(!file_exists($this->_options['plugins_dir'].'modifier_'.$funcName.'.php')){
- $this->_throwException('插件"'.$funcName.'"不存在');
- }
- $pluginFileName='modifier_'.$funcName.'.php';
- if(!in_array($pluginFileName, $this->includePlugins)){
- $this->includePlugins[]=$pluginFileName;//添加include插件
- }
- $tmp="dolr_modifier_{$funcName}($tmp{$args})";
- }else{
- $tmp="{$funcName}($tmp{$args})";
- }
- }
- return stripslashes($tmp.';');
- }
- /**
- * 数组操作的点支持
- *
- * @param string $string 目标字符串
- * @return string
- */
- protected function _parsePoint($string)
- {
- $arr=explode('.',$string);//$a.b.c.f
- $varName=array_shift($arr);//$a
- return $varName.'[\''.join('\'][\'',$arr).'\']';//$a['b']['c']['f']
- }
- /**
- * 去掉自定义标签
- *
- * @param string $expr 源文
- * @param string $statement 替换目标
- * @return string
- */
- protected function _stripvtags($expr, $statement)
- {
- $expr = str_replace("\\\"", "\"", preg_replace("/\/s", "\\1", $expr));
- $statement = str_replace("\\\"", "\"", $statement);
- return $expr . $statement;
- }
- /**
- * 抛出一个错误信息
- *
- * @param string $message
- * @return void
- */
- protected function _throwException($message)
- {
- trigger_error('DolrViews错误:'.$message);
- exit;
- }
- }
- /* vim: set expandtab: */
使用范例:
PHP:
- include 'DolrViews.class.php';
- $tpl=DolrViews::getInstance();//单例
- $_options=array(
- 'template_dir'=>'templates',//模板目录
- 'compile_dir'=>'templates_c',//编译目录
- 'cache_dir'=>'cache',//缓存目录
- 'caching'=>false,//是否缓存
- 'cache_lifetime'=>3600,//缓存有效期
- 'plugins_dir'=>'plugins',//插件目录
- 'tpl_suffix'=>'html',//模板后缀
- );
- $tpl->setOptions($_options);//保存如上设置
- $title="标题测试啊";
- $val=3;
- $array=array(
- array('username'=>'Joychao','password'=>'nowpass'),
- array('username'=>'DolrPHP','password'=>'password'),
- );
- $var='这里是带标签的HTML';
- //分配变量
- $tpl->assign('val',$val);
- $tpl->assign('title',$title);
- $tpl->assign('array',$array);
- //显示
- $tpl->display('index.html');
- --包含文件----------------------------
- ------if else------------------------
- do anything
- do another
- Do not
- ---------------------------------
- ------loop------------------------
循环内容
- 交互式显示:
-
循环中的变量:
循环中的索引$dolr_index:
ccc - 没有内容
- ---------------------------------
- --包含文件----------------------------
- ------------------------------
目录范例:
DolrViews/
|--cache //缓存目录
|--plugins //插件目录
|--templates //模板目录
|--templates_c //编译文件目录
|--DolrViews.classs.php //引擎主文件
|--index.php //测试文件
目前插件只写了一个测试,插件规范是modifier_函数名.php 内容函数名:dolr_modifier_函数名($第一个参数必须[,其它参数])。范例:
- function dolr_modifier_html2text($string)
- {
- return strip_tags($string);
- }
今天刚写的,欢迎大家拍砖啊! 另外欢迎 @安正超 !

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











많은 사용자들이 스마트 시계를 선택할 때 Huawei 브랜드를 선택하게 됩니다. 그 중 Huawei GT3pro와 GT4가 가장 인기 있는 선택입니다. 두 제품의 차이점을 궁금해하는 사용자가 많습니다. Huawei GT3pro와 GT4의 차이점은 무엇입니까? 1. 외관 GT4: 46mm와 41mm, 재질은 유리 거울 + 스테인레스 스틸 본체 + 고해상도 섬유 후면 쉘입니다. GT3pro: 46.6mm 및 42.9mm, 재질은 사파이어 유리 + 티타늄 본체/세라믹 본체 + 세라믹 백 쉘입니다. 2. 건강한 GT4: 최신 Huawei Truseen5.5+ 알고리즘을 사용하면 결과가 더 정확해집니다. GT3pro: ECG 심전도, 혈관 및 안전성 추가

Java의 String.valueOf() 함수를 사용하여 기본 데이터 유형을 문자열로 변환 Java 개발에서 기본 데이터 유형을 문자열로 변환해야 할 때 일반적인 방법은 String 클래스의 valueOf() 함수를 사용하는 것입니다. 이 함수는 기본 데이터 유형의 매개변수를 허용하고 해당 문자열 표현을 반환할 수 있습니다. 이 기사에서는 기본 데이터 유형 변환을 위해 String.valueOf() 함수를 사용하는 방법을 살펴보고 다음과 같은 몇 가지 코드 예제를 제공합니다.

char 배열을 문자열로 변환하는 방법: 할당을 통해 달성할 수 있습니다. char 배열이 문자열에 직접 값을 할당하고 실행하도록 하려면 {char a[]=" abc d\0efg ";string s=a;} 구문을 사용합니다. 변환을 완료하는 코드입니다.

Windows 11에서 캡처 도구가 작동하지 않는 이유 문제의 근본 원인을 이해하면 올바른 솔루션을 찾는 데 도움이 될 수 있습니다. 캡처 도구가 제대로 작동하지 않는 주요 이유는 다음과 같습니다. 초점 도우미가 켜져 있습니다. 이렇게 하면 캡처 도구가 열리지 않습니다. 손상된 응용 프로그램: 캡처 도구가 실행 시 충돌하는 경우 응용 프로그램이 손상되었을 수 있습니다. 오래된 그래픽 드라이버: 호환되지 않는 드라이버가 캡처 도구를 방해할 수 있습니다. 다른 응용 프로그램의 간섭: 실행 중인 다른 응용 프로그램이 캡처 도구와 충돌할 수 있습니다. 인증서가 만료되었습니다. 업그레이드 프로세스 중 오류로 인해 이 문제가 발생할 수 있습니다. 이 문제는 대부분의 사용자에게 적합하며 특별한 기술 지식이 필요하지 않습니다. 1. Windows 및 Microsoft Store 앱 업데이트

Java의 String.replace() 함수를 사용하여 문자열의 문자(문자열) 바꾸기 Java에서 문자열은 불변 객체입니다. 즉, 문자열 객체가 생성되면 해당 값을 수정할 수 없습니다. 그러나 문자열에서 특정 문자나 문자열을 바꿔야 하는 상황이 발생할 수 있습니다. 이때 Java의 String 클래스에 있는 replacement() 메소드를 사용하여 문자열 교체를 구현할 수 있습니다. String 클래스의 replacement() 메소드에는 두 가지 유형이 있습니다.

안녕하세요 여러분, 오늘은 Java: String에 대한 기본 지식을 여러분과 공유하겠습니다. String 클래스의 중요성은 말할 필요도 없이 우리 백엔드 개발에서 가장 많이 사용되는 클래스라고 할 수 있으므로 이에 대해 이야기할 필요가 있다.

문자열의 길이를 얻으려면 Java의 String.length() 함수를 사용하십시오. Java 프로그래밍에서 문자열은 문자열의 길이, 즉 문자열의 문자 수를 가져와야 하는 경우가 많습니다. Java에서는 String 클래스의 length() 함수를 사용하여 문자열의 길이를 얻을 수 있습니다. 다음은 간단한 예제 코드입니다: publicclassStringLengthExample{publ

1. String1. JDK의 문자열 이해 먼저 JDK의 String 클래스 소스 코드를 살펴보면 String 클래스가 final로 수정되는 것을 볼 수 있습니다. String 클래스의 하위 클래스가 없으므로 JDK를 사용하는 모든 사람들이 동일한 String 클래스를 사용합니다. String 상속이 허용되면 모든 사람이 사용하는 String 클래스가 동일한 버전이 아닙니다. 서로 다른 두 사람이 동일한 메서드를 사용하면 결과가 달라져 코드 개발이 불가능해집니다. 상속과 메서드 재정의는 유연성을 제공할 뿐만 아니라 많은 하위 클래스가 다르게 동작하게 만듭니다.
