DolrPHP テンプレート エンジン DolrViews 共有
【分享】DolrPHPモジュール板引擎DolrViews分享
核心文件:DolrViews.class.php:
- /**
- * DolrPHP テンプレート エンジン
- * @author Joychao
- * @version 1.0 beta
- * @license http: //www.Joychao.cc
- */
- define('DOLRVIEWS') または define('DOLRVIEWS',true);
- define ('DIR_SEP') またはdefine('DIR_SEP',DIRECTORY_SEPARATOR);
- class DolrViews
- {
- /**
- * シングルトン オブジェクト
- *
- * @static var
- * @var オブジェクト DolrViews
- */
- protected static $_instance;
- /**
- * テンプレート変数
- *
- * @var array
- */
- protected $_tpl_vars=array();
- /**
- * テンプレートパラメータ情報
- *
- * @var array
- */
- protected $_options=array(
- 'template_dir'=>'テンプレート',
- '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'=>'テンプレート',
- * 'compile_dir'=>'templates_c',
- * 'cache_dir'=>'cache',
- * 'caching'=>false,
- * 'cache_lifetime'=>3600,
- * ' plugins_dir '=>'プラグイン',
- * 'php_handing'=>false,
- * );
- * $tpl=new DolrViews($_options); 🎜> */
- パブリック関数 __construct($options=array())
- {
- if (!empty($options))
- {
- $this->setOptions($options);
- }
- }
- /**
- * 変数をテンプレートに割り当てます
- *
- * @param string $varName 変数名
- * @parammixed $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->gt;_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 オプション名
- * @parammixed $optionValue オプション値
- */
- パブリック関数 __set($optionName,$optionValue)
- {
- $this->set($optionName,$optionValue) ;
- }
- /**
- * 構成を個別に設定します
- *
- * @param string $optionName オプション名
- * @parammixed $optionValue オプション値
- */
- public function set($optionName,$optionValue)
- {
- switch (strto lower($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 'キャッシュ' :
- $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 ブール値の成功または失敗
- */
- 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
- */
- パブリック関数 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) / /
- と filemtime($cacheFile)+$this->_options['cache_lifetime']>time()//期限切れではありません
- と filemtime($cacheFile) >filemtime ($tplFilePath) //テンプレートは変更されていません
- ){//存在しており、期限切れになっていません
- return true;
- }else{
- return false;
- }
- }
- /**
- * 組み込み変数の取得
- * @return array
- */
- 保護された関数_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_dependent_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 template file
- * @return string ファイル名
- */
- 保護された関数 _getTplPath($tplFileName)
- {
- return $this->_options['template_dir'].$this->_trimPath($tplFileName);
- }
- /**
- * キャッシュされたファイルを取得します
- * @param string $tplFileName template file
- * @return string ファイル名
- */
- 保護された関数 _getCacheFile($tplFileName, $cacheId)
- {
- return $this->_options['cache_dir'].$tplFileName.'.cache.'.md5($cacheId).'.php';
- }
- /**
- * コンパイル済みファイル名を取得します
- *
- * @param string $tplFileName template file
- * @return string ファイル名
- */
- 保護された関数 _getCompileFile( $tplFileName, $cacheId)
- {
- return $this->_options['compile_dir'] .$tplFileName.'.compile.php';
- }
- /**
- * 解析されたテンプレート
- *
- * @param string $tplFileName テンプレート ファイル
- * @return string 解析されたコンテンツ
- */
- 保護された関数 _parseTpl($tplFileName ,$cacheId)
- {
- $tplFilePath=$this->gt;_getTplPath($tplFileName);
- if (!file_exists($tplFilePath) または !is_readable($ tplFilePath)) {
- $this->_throwException(' 指定されたモール板文件を打てません'.$tplFileName.'"');
- }
- $content =file_get_contents($tplFilePath);
- //检测包含、检测所有的 include 'xxx.html'
- preg_match_all('/[nrt]*
[nrt]*/s', $content, $matches); - if(!empty($matches[1])){
- foreach ($matches[1] as $key=>$fileName) {
- $includeFilePath=$this-> _getTplPath($fileName);
- if (!file_exists($includeFilePath) または !is_readable($includeFilePath)) {
- $this->_throwException(' 指定されたモジュールファイルを打てません" '.$includeFilePath.'"');
- }
- $includeCompilePath=$this->_getCompileFile($fileName,$cacheId);// 编译文件名 $this->_parseTpl($fileName,$cacheId);
- $content=str_replace($matches[0][$key], '', $content);
- }
- }
- //规则
- $pattern=array(
- '/ ?>[nr]* /s',//删除 PHP 代打断间多余空格及换行
- '/(
]+)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 - '/
/',//<サイクル 'a','b'> - );
- // PHP 格式
- $php=array(
- ' ',
- "$this->_parseVar('\1')",
- ' _options['template_dir']."\1';?>",
- "$this->_stripvtags('','\2');",
- "$this->_stripvtags('','');",
- '',
- "$this->_stripvtags('','\3\4');",
- "$this->_stripvtags('','\ 3n');",
- "$this->_stripvtags(' \3) { ?>','\4\ 5');",
- "$this->_stripvtags(' \3) { ?>','\4');",
- "",
- );
- $content=preg_replace($pattern, $php, $content);
- //プラグインを含める
- $pluginsString='';
- if($tplFileName==$this->mainTplFileName){// メイン テンプレートの場合は、インクルード ファイル
- foreach ($this->includePlugins as $plugin) {
- $pluginsString.="include $this->options['plugins_dir'].'{$plugin }';n ";
- }
- }
- $header=<<
- /* *
- * {$tplFileName}
- * DolrViews テンプレート コンパイル ファイル
- * @package {$this->mainTplFileName}
- */
- define('DOLRVIEWS') または exit('アクセス拒否');
- $pluginsString
- ?>n
- DOLRVIEWS;
- $content=$header.$content;
- $compileFilePath=$this->_writeCompile($tplFileName,$content,$cacheId);//コンパイルされたファイルを書き込みます
- return $compileFilePath;
- }
- /**
- * キャッシュテンプレートファイル
- *
- * @param string $tplFileName テンプレートファイル
- * @param string $cacheId キャッシュ ID
- */
- 保護関数 _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 ウェブページ
- */
- 保護された関数 _writeCompile($tplFileName,$content) ,$cacheId)
- {
- $compileFilePath=$this->gt;_getCompileFile($tplFileName,$cacheId);//ファイル名を保存
- if(!file_exists( dirname($compileFilePath))){
- $this->makeDir(dirname($compileFilePath));
- }
- file_put_contents($compileFilePath,$content);
- return $compileFilePath;
- }
- /**
- * オペレーティング システムに適した形式にパスを修正します
- *
- * @param string $path path name
- * @return string
- */
- 保護された関数 _trimPath( $path)
- {
- return rtrim(str_replace(array('/', '\', '//', '\\'),DIR_SEP, $path),DIR_SEP);
- }
- /**
- * 指定されたパスに基づいて存在しないフォルダーを作成します
- *
- * @param string $path パス/フォルダー名
- * @return文字列
- */
- 保護関数 _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 target string
- * @return string
- */
- 保護された関数 _parseVar($string ){
- $pattern=array(
- '/^',
- '/>$/',
- '/( $w+|[^>s]+)/e',//$title|striptags|html2text
- '/($[w]+.[w]+ )/e',
- );
- $replacement=array(
- "
- ' ?>',
- "$ this->_parseModifier('\1');",
- "$this->_parsePoint('\1');",
- );
- returntripslashes(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("/\<\?php echo \\\$.+? \?\>/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');
<$title|strip_tags:'hahah'|html2text:"nnn"> - --包含文件----------------------------
- ------if else------------------------
- do anything
- do another
- Do not
- ---------------------------------
- ------loop------------------------
循环内容<$dolr_index>
- 交互式显示:
- 循环中的变量:<$v.username>
ループ内のインデックス $dolr_index:
ccc - コンテンツなし
- ---------------- --- -------------
- --含まれるファイル--------------- --- -------
- ------------ --- ---------------
目次の例:
ドルビュー数/
|--cache //キャッシュディレクトリ
|--plugins //プラグインディレクトリ
|--templates //テンプレート ディレクトリ
|--templates_c //コンパイルファイルディレクトリ
|--DolrViews.classs.php //エンジンメインファイル
|--index.php //テストファイル
現在、プラグインは 1 つのテストのみを作成しています。プラグインの仕様は modifier_function name.php です。コンテンツ関数名: dolr_modifier_function 名 ($最初のパラメータは [, その他のパラメータ] である必要があります)。例:
- 関数 dolr_modifier_html2text($string)
- {
- returnstrip_tags($string);
- }
今日これを書きました。皆さんもぜひ投稿してください。 こちらも大歓迎です @安正超!

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック

多くのユーザーはスマートウォッチを選ぶときにファーウェイブランドを選択しますが、その中でもファーウェイ GT3pro と GT4 は非常に人気のある選択肢であり、多くのユーザーはファーウェイ 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配列をstringに変換する方法:代入によって実現できます {char a[]=" abc d\0efg ";string s=a;} 構文を使用して、char配列にstringに値を直接代入させて実行します変換を完了するためのコード。

Windows 11 で Snipping Tool が機能しない理由 問題の根本原因を理解すると、適切な解決策を見つけるのに役立ちます。 Snipping Tool が正しく動作しない主な理由は次のとおりです。 フォーカス アシスタントがオンになっている: これにより、Snipping Tool が開かなくなります。破損したアプリケーション: 起動時にスニッピング ツールがクラッシュする場合は、破損している可能性があります。古いグラフィック ドライバー: 互換性のないドライバーは、スニッピング ツールに干渉する可能性があります。他のアプリケーションからの干渉: 実行中の他のアプリケーションが Snipping Tool と競合する可能性があります。証明書の有効期限が切れています: アップグレード プロセス中のエラーにより、この問題が発生する可能性があります。これらの簡単な解決策は、ほとんどのユーザーに適しており、特別な技術知識は必要ありません。 1. Windows および Microsoft Store アプリを更新する

Java の String.replace() 関数を使用して文字列内の文字 (文字列) を置換する Java では、文字列は不変オブジェクトです。つまり、文字列オブジェクトが作成されると、その値は変更できません。ただし、文字列内の特定の文字または文字列を置換する必要がある状況が発生する場合があります。現時点では、Java の String クラスの replace() メソッドを使用して文字列置換を実装できます。 String クラスの replace() メソッドには 2 つのタイプがあります。

皆さんこんにちは。今日は Java の基本知識である String についてお話します。 String クラスの重要性は言うまでもなく、バックエンド開発で最もよく使用されるクラスであるため、説明する必要があります。

文字列の長さを取得するには、Java の String.length() 関数を使用します。Java プログラミングでは、文字列は非常に一般的なデータ型です。多くの場合、文字列の長さ、つまり文字列内の文字数を取得する必要があります。 Java では、String クラスの length() 関数を使用して文字列の長さを取得できます。簡単なコード例を次に示します。 publicclassStringLengthExample{publ

1. JDK の String1. String を理解する まず、JDK の String クラスのソース コードを見てみましょう. これには多くのインターフェイスが実装されています. String クラスは Final によって変更されていることがわかります. これは、String クラスができないことを意味しますString. クラスのサブクラスは継承されず、String. クラスのサブクラスは存在しないため、JDK を使用するすべての人が同じ String クラスを使用します。String の継承が許可されている場合、誰もが String を拡張できます。全員が異なるバージョンの String を使用し、2 人の異なる人が String. クラスを使用します。同じメソッドでも異なる結果が表示されるため、コードの開発が不可能になります。継承とメソッドのオーバーライドは柔軟性をもたらすだけでなく、多くのサブクラスの動作が異なる原因になります。
