方法:
$this->assign('style',$style);//变量 $this->display();//模板 <?php /*配制*/ $config=array( /* 数据库设置 */ 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 'DB_NAME' => 'php', // 数据库名 'DB_USER' => 'root', // 用户名 'DB_PWD' => '123', // 密码 'DB_PREFIX' => 'jiaodu_', // 数据库表前缀 'DB_CHARSET' => 'utf8', // 数据库编码默认采用utf8 /* SESSION设置 */ 'SESSION_START' => 'user', //session方式,文件方式:file, 数据库设置为user /* 模板引擎设置 */ 'TMPL_ADMIN_PATH' =>'admin',//后台目录名称 'TMPL_COMPILE_PATH' =>'/Runtime',//读写目录 'TMPL_PATH' =>'/template',//模板路径 'TMPL_TEMPLATE_SUFFIX' => 'html', // 默认模板文件后缀 'TMPL_L_DELIM' => '{', // 模板引擎普通标签开始标记 'TMPL_R_DELIM' => '}', // 模板引擎普通标签结束标记 'TMPL_STRIP_SPACE' => true, // 是否去除模板文件里面的html空格与换行 'TMPL_CACHE_ON' => true, // 是否开启模板编译缓存,设为false则每次都会重新编译,一般用于模板调试 /* URL设置 */ 'URL_HTML_SUFFIX' => 'html', // URL伪静态后缀设置 'URL_PATHINFO_MODEL' => 2, // URL模式,1不隐藏、2隐藏入口文件[需要规则支持] /*其它设置*/ 'PASS_ENCRYPT' =>'www.php.cn',//加密因子 );
[PHP]代码
<?php /** * 模板解析类 * @author 角度 QQ:1286522207 * */ class template extends Action{ private $config; private $CompileDir;//编译目录 private $templateDir;//模板目录 private $templateFile; private $debuy=1;//是否调试 private $assign;//变量 public function __construct($templateFile){ $this->config(); $this->templateFile=$templateFile; } private function config(){ global $config; $this->config=$config; $this->CompileDir=$this->config['TMPL_COMPILE_PATH'].'/Compile'; $this->templateDir=$this->config['TMPL_PATH']; $this->debuy=$this->config['TMPL_CACHE_ON']; } /** * 检查编译目录 */ public function is_CompileDir(){ $dir=APP_PATH.$this->CompileDir; if (!is_dir($dir)){ if (!mkdir($dir)){ die('编译目录自动创建失败,请手动创建'); } } if (!is_writeable($dir)){ die('编译目录没有写入权'); } } /** * 注入变量 */ public function assign($assign) { $this->assign=$assign; } /** * 输出模板 */ public function display(){ $this->is_CompileDir(); $this->CompileCheck(); } /** * 检查编译 */ private function CompileCheck(){ $this->is_CompileDir(); $filename=APP_PATH.$this->CompileDir.'/'.md5($this->templateFile).'.php'; if ($this->debuy || !is_file($filename)){ $this->tmplstrtpspace($filename); } foreach ($this->assign as $key=>$row){ $$key=$row; } include $filename; } /** * 格式化模板并写入编译 */ private function tmplstrtpspace($filename){ if ($this->config['TMPL_STRIP_SPACE']){ $find = array("~>\s+<~","~>(\s+\n|\r)~"); $replace = array("><",">"); $tmplContent = preg_replace($find, $replace,$this->templateCheck()); }else { $tmplContent = $this->templateCheck(); } if (file_put_contents($filename,trim($tmplContent))){ return true; }else { die('编译写入失败'); } } /** * 检查模板 */ private function templateCheck(){ $PATH=APP_PATH.$this->templateDir.'/'.$this->templateFile.'.html'; if (is_file($PATH)){ return $this->template_compile(file_get_contents ( $PATH )); }else { die('模板:'.$this->templateFile.'.html 不存在'); } } /** * 编译模板 */ private function template_compile($template_Conver){ if (empty($template_Conver)){ return $template_Conver; }else { $_Left= $this->config['TMPL_L_DELIM']; $_Right= $this->config['TMPL_R_DELIM']; $template_Preg [] = '/<\?(=|php|)(.+?)\?>/is'; $template_Preg [] = '/' . $_Left . '(else if|elseif) (.*?)' . $_Right . '/i'; $template_Preg [] = '/' . $_Left . 'for (.*?)' . $_Right . '/i'; $template_Preg [] = '/' . $_Left . 'while (.*?)' . $_Right . '/i'; $template_Preg [] = '/' . $_Left . '(loop|foreach) (.*?)' . $_Right . '/i'; $template_Preg [] = '/' . $_Left . 'if (.*?)' . $_Right . '/i'; $template_Preg [] = '/' . $_Left . 'else' . $_Right . '/i'; $template_Preg [] = '/' . $_Left . "(eval|_)( |[\r\n])(.*?)" . $_Right . '/is'; $template_Preg [] = '/' . $_Left . '_e (.*?)' . $_Right . '/is'; $template_Preg [] = '/' . $_Left . '_p (.*?)' . $_Right . '/i'; $template_Preg [] = '/' . $_Left . '\/(if|for|loop|foreach|eval|while)' . $_Right . '/i'; $template_Preg [] = '/' . $_Left . '((( *(\+\+|--) *)*?(([_a-zA-Z][\w]*\(.*?\))|\$((\w+)((\[|\()(\'|")?\$*\w*(\'|")?(\)|\]))*((->)?\$?(\w*)(\((\'|")?(.*?)(\'|")?\)|))){0,})( *\.?[^ \.]*? *)*?){1,})' . $_Right . '/i'; $template_Preg [] = "/( | ){0,}(\r\n){1,}\";/"; $template_Preg [] = '/' . $_Left . '(\#|\*)(.*?)(\#|\*)' . $_Right . '/'; $template_Preg [] = '/' . $_Left . '\%([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)' . $_Right . '/'; $template_Replace [] = '<?\\1\\2?>'; $template_Replace [] = '<?php }else if (\\2){ ?>'; $template_Replace [] = '<?php for (\\1) { ?>'; $template_Replace [] = '<?php while (\\1) { ?>'; $template_Replace [] = '<?php foreach ((array)\\2) { $__i++; ?>'; $template_Replace [] = '<?php if (\\1){ ?>'; $template_Replace [] = '<?php }else{ ?>'; $template_Replace [] = '<?php \\3; ?>'; $template_Replace [] = '<?php echo \\1; ?>'; $template_Replace [] = '<?php print_r(\\1); ?>'; $template_Replace [] = '<?php } ?>'; $template_Replace [] = '<?php echo \\1;?>'; $template_Replace [] = ''; $template_Replace [] = ''; $template_Replace [] = '<?php echo $this->lang_array[\'\\1\'];?>'; return preg_replace ( $template_Preg, $template_Replace, $template_Conver ); } } }
성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

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

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

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

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사
어 ass 신 크리드 그림자 : 조개 수수께끼 솔루션
4 몇 주 전ByDDD
Windows 11 KB5054979의 새로운 기능 및 업데이트 문제를 해결하는 방법
3 몇 주 전ByDDD
Atomfall에서 크레인 제어 키 카드를 찾을 수 있습니다
4 몇 주 전ByDDD
<s> : 데드 레일 - 모든 도전을 완료하는 방법
1 몇 달 전ByDDD
Atomfall Guide : 항목 위치, 퀘스트 가이드 및 팁
1 몇 달 전ByDDD

뜨거운 도구

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

Atom Editor Mac 버전 다운로드
가장 인기 있는 오픈 소스 편집기
