<?php
/**
* PHPnow Template 模板引擎 1.0
* @copyright (C) 2011-2013 PHPnow
* @license http://www.phpnow.cn
* @author jiaodu QQ:1286522207
*/
namespace PHPnow;
/**
* 模板编译类
* @author sanliang
*/
class compile {
public function __construct(&$content, \PHPnow\template $template, $templateFile) {
$this->__source = $content;
$this->__template = $template;
$this->__templateFile = $templateFile;
$this->__ldel = preg_quote($this->__template->__leftDelimiter);
$this->__rdel = preg_quote($this->__template->__rightDelimiter);
$content = preg_replace("/<\?xml(.*?)\?>/s", "##XML\\1XML##", $content);
$this->parsePhp($content);
$content = preg_replace_callback("/##XML(.*?)XML##/s", array($this, 'xmlSubstitution'), $content);
$this->compileTemplate($content);
$content = "<?php class_exists('PHPnow')?:exit;?>" . $content;
$this->stripWhitespace($content);
}
private function parsePhp(&$content) {
if (!$this->__template->__phpOff)
$content = str_replace(array("<?", "?>"), array("<?", "?>"), $content);
else
$content = preg_replace('/(<\?(?!php|=|$))/i', '<?php echo \'\\1\'; ?>', $content); return $content;
}
private function xmlSubstitution($capture) {
return "<?php echo '<?xml " . stripslashes($capture[1]) . " ?>'; ?>";
}
protected function compileTemplate(&$content) {
$tagRegexp = str_replace(array("__PHPNOW", "PHPNOW__"), array($this->__ldel, $this->__rdel), $this->__tagList);
$tagRegexp = "/" . implode("|", $tagRegexp) . "/";
$content = preg_split($tagRegexp, $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$this->pathReplace($content);
$this->compileCode($content);
return $content;
}
private function reducePath(&$path) {
$path = str_replace("://", "@not_replace@", $path);
$path = str_replace("//", "/", $path);
$path = str_replace("@not_replace@", "://", $path);
$path = preg_replace('/\w+\/\.\.\//', '', $path);
}
private function pathReplace(&$content) {
if ($this->__template->__pathReplace) {
$path = $this->__template->__pathUrl;
$this->reducePath($path);
$exp = $sub = array();
if (in_array("img", $this->__template->__pathReplaceList)) {
$exp = array('/<img(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<img(.*?)src=(?:")([^"]+?)#(?:")/i', '/<img(.*?)src="(.*?)"/', '/<img(.*?)src=(?:\@)([^"]+?)(?:\@)/i');
$sub = array('<img$1src=@$2://$3@', '<img$1src=@$2@', '<img$1src="' . $path . '$2"', '<img$1src="$2"');
} if (in_array("script", $this->__template->__pathReplaceList)) {
$exp = array_merge($exp, array('/<script(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<script(.*?)src=(?:")([^"]+?)#(?:")/i', '/<script(.*?)src="(.*?)"/', '/<script(.*?)src=(?:\@)([^"]+?)(?:\@)/i'));
$sub = array_merge($sub, array('<script$1src=@$2://$3@', '<script$1src=@$2@', '<script$1src="' . $path . '$2"', '<script$1src="$2"'));
} if (in_array("link", $this->__template->__pathReplaceList)) {
$exp = array_merge($exp, array('/<link(.*?)href=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<link(.*?)href=(?:")([^"]+?)#(?:")/i', '/<link(.*?)href="(.*?)"/', '/<link(.*?)href=(?:\@)([^"]+?)(?:\@)/i'));
$sub = array_merge($sub, array('<link$1href=@$2://$3@', '<link$1href=@$2@', '<link$1href="' . $path . '$2"', '<link$1href="$2"'));
} if (in_array("a", $this->__template->__pathReplaceList)) {
$exp = array_merge($exp, array('/<a(.*?)href=(?:")(http\:\/\/|https\:\/\/|javascript:)([^"]+?)(?:")/i', '/<a(.*?)href="(.*?)"/', '/<a(.*?)href=(?:\@)([^"]+?)(?:\@)/i'));
$sub = array_merge($sub, array('<a$1href=@$2$3@', '<a$1href="' . $this->__template->__baseUrl . '$2"', '<a$1href="$2"'));
} if (in_array("input", $this->__template->__pathReplaceList)) {
$exp = array_merge($exp, array('/<input(.*?)src=(?:")(http|https)\:\/\/([^"]+?)(?:")/i', '/<input(.*?)src=(?:")([^"]+?)#(?:")/i', '/<input(.*?)src="(.*?)"/', '/<input(.*?)src=(?:\@)([^"]+?)(?:\@)/i'));
$sub = array_merge($sub, array('<input$1src=@$2://$3@', '<input$1src=@$2@', '<input$1src="' . $path . '$2"', '<input$1src="$2"'));
} $content = preg_replace($exp, $sub, $content);
}
}
private function compileCode(&$content) {
$compiledCode = $open_if = $comment_is_open = $ignore_is_open = null;
$loop_level = 0;
while ($html = array_shift($content)) {
if (!$comment_is_open && ( strpos($html, $this->__template->__leftDelimiter . '/ignore' . $this->__template->__rightDelimiter) !== false || strpos($html, '*' . $this->__template->__rightDelimiter) !== false ))
$ignore_is_open = false; elseif ($ignore_is_open) {
} elseif (strpos($html, $this->__template->__leftDelimiter . '/noparse' . $this->__template->__rightDelimiter) !== false)
$comment_is_open = false; elseif ($comment_is_open)
$compiledCode .= $html; elseif (strpos($html, $this->__template->__leftDelimiter . 'ignore' . $this->__template->__rightDelimiter) !== false || strpos($html, '{*') !== false)
$ignore_is_open = true; elseif (strpos($html, $this->__template->__leftDelimiter . 'noparse' . $this->__template->__rightDelimiter) !== false)
$comment_is_open = true; elseif (preg_match('/' . $this->__ldel . 'include="([^"]*)"(?: cache="([^"]*)"){0,1}' . $this->__rdel . '/', $html, $code)) {
$include_var = $this->varReplace($code[1], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".', $php_right_delimiter = '."', $loop_level);
$compiledCode .=isset($code[2]) ? '<?php $tpl = clone $this;' . '$tpl->__caching = true;' . '$tpl->__acheLifetime=' . $code[2] . ';' . '$tpl->display("' . $include_var . '");' . '?>' : '<?php $tpl = clone $this;' . '$tpl->display("' . $include_var . '");' . '?>';
} elseif (preg_match('/' . $this->__ldel . 'config="([^"]*)"(?: file="([^"]*)"){0,1}' . $this->__rdel . '/', $html, $code)) {
$include_var = $this->varReplace($code[1], $left_delimiter = null, $right_delimiter = null, $php_left_delimiter = '".', $php_right_delimiter = '."', $loop_level);
$compiledCode .=isset($code[2]) ? '<?php echo $this->getConfig("' . $include_var . '","' . $code[2] . '");' . '?>' : '<?php echo $this->getConfig("' . $include_var . '");' . '?>';
} elseif (preg_match('/' . $this->__ldel . 'loop(?: name){0,1}="\${0,1}([^"]*)"' . $this->__rdel . '/', $html, $code)) {
$loop_level++;
$var = $this->varReplace('$' . $code[1], $tag_left_delimiter = null, $tag_right_delimiter = null, $php_left_delimiter = null, $php_right_delimiter = null, $loop_level - 1);
$counter = "\$counter$loop_level";
$key = "\$key$loop_level";
$value = "\$value$loop_level";
$compiledCode .= "<?php $counter=-1; if( isset($var) && is_array($var) && sizeof($var) ) foreach( $var as $key => $value ){ $counter++; ?>";
} elseif (strpos($html, $this->__template->__leftDelimiter . '/loop' . $this->__template->__rightDelimiter) !== false) {
$counter = "\$counter$loop_level";
$loop_level--;
$compiledCode .= "<?php } ?>";
} elseif (preg_match('/' . $this->__ldel . 'if(?: condition){0,1}="([^"]*)"' . $this->__rdel . '/', $html, $code)) {
$open_if++;
$tag = $code[0];
$condition = $code[1];
$this->functionCheck($tag);
$parsed_condition = $this->varReplace($condition, $tag_left
没有合适的资源?快使用搜索试试~ 我知道了~
基于PHP的now template engine 模板引擎 v1.0.zip
共12个文件
php:5个
html:2个
png:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 143 浏览量
2023-07-22
00:11:19
上传
评论
收藏 38KB ZIP 举报
温馨提示
基于PHP的now template engine 模板引擎 v1.0.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于PHP的now template engine 模板引擎 v1.0.zip (12个子文件)
132692157582279243
template-1.0.0
LICENSE.md 11KB
PHPnow
PHPnow.class.php 339B
libs
compile.class.php 19KB
template.class.php 14KB
Lang
zh-cn.php 275B
logo.png 13KB
favicon.ico 5KB
index.php 156B
Config
global.ini 38B
Tpl
body.html 79B
index.html 222B
favicon.png 7KB
共 12 条
- 1
资源评论
助力毕业
- 粉丝: 2173
- 资源: 5122
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
- 基于MIC+NE555光敏电阻的声光控电路Multisim仿真原理图
- 基于JSP毕业设计-基于WEB操作系统课程教学网站的设计与实现(源代码+论文).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功