假设使用标签形式作为模板语法
在解析环节我想到两种实现方式
1. 利用正则获取标签内容,然后将标签进行规则性的替换并使用eval进行解析,解析完成后再用正则进行替换
2. 利用正则获取标签内容,然后将标签进行规则性的替换,替换完成后使用cli执行脚本并返回内容
现在的问题是,不太清楚整个流程和这两种实现方式是否正确,或者有别的方式。
谢谢
回复内容:
假设使用标签形式作为模板语法
在解析环节我想到两种实现方式
1. 利用正则获取标签内容,然后将标签进行规则性的替换并使用eval进行解析,解析完成后再用正则进行替换
2. 利用正则获取标签内容,然后将标签进行规则性的替换,替换完成后使用cli执行脚本并返回内容
现在的问题是,不太清楚整个流程和这两种实现方式是否正确,或者有别的方式。
谢谢
简单理解一下,是这样的
<code><?php // file.php $var = 'text'; include 'file.html'; ``` ``` <!-- file.html --> =$var?> </code>
file.html就是file.php的模板
再换个思路是这样的
<code><?php // file.php $var = 'text'; #complie template if (!is_file('template_file.php')) { $contents = file_get_contents('file.html'); $contents = preg_replace(.....); file_put_contents('template_file.php',$contents); } include 'template_file.php'; </code></code>
<code><!-- file.html --> {{var}} </code>
摘自Slim:
<code> protected function render($template, $data = null) { $templatePathname = $this->getTemplatePathname($template); if (!is_file($templatePathname)) { throw new \RuntimeException("View cannot render `$template` because the template does not exist"); } $data = array_merge($this->data->all(), (array) $data); extract($data); ob_start(); require $templatePathname; return ob_get_clean(); } </code>
拿到数据之后要进一步处理还是display就看你自己了
举个简单的列子给你吧
<code><?php /**************** * 核心文件 * @discription: 编写简单的模板引擎 */ define('INVIEW', true); class view { var $tpl_dir = 'template'; var $cache_dir = 'cache'; var $tpl_ext = '.html'; var $var_left = '{'; var $var_right = '}'; function __construct($config=array()) { extract($config); if(isset($tpl_dir))$this->tpl_dir = $tpl_dir; if(isset($cache_dir))$this->cache_dir = $cache_dir; if(isset($tpl_ext))$this->tpl_ext = $tpl_ext; if(isset($var_left))$this->var_left = $var_left; if(isset($var_right))$this->var_right = $var_right; } function load($tplfilename) { $tplfile = $this->tpl_dir.'/'.$tplfilename.$this->tpl_ext; if(!file_exists($tplfile)) die('Template not found: '.$tplfile); return $this->cache($tplfilename, $tplfile); } //判断模板是否缓存,如模板文件有更改则重新编译 function cache($tplname, $tpl_file) { $cache_file = $this->cache_dir.'/'.md5($tplname).'.php'; if(!file_exists($cache_file) || filemtime($tpl_file)>filemtime($cache_file)) $this->compile($tpl_file, $cache_file); return $cache_file; } //编译模板内容到PHP格式,并写入缓存 function compile($tpl, $cache) { $body = file_get_contents($tpl); $vl = $this->var_left; $vr = $this->var_right; $patterns = array( "#$vl\s*include:(.+?)\s*$vr#i", "#$vl\s*if\s+(.+?)\s*$vr#i", "#$vl\s*else\s*$vr#i", "#$vl\s*elseif\s+(.+?)\s*$vr#i", "#$vl\s*endif\s*$vr#i", "#$vl\s*/if\s*$vr#i", "#$vl\s*foreach\s+(.+?):(.+?)\s*$vr#i", "#$vl\s*endforeach\s*$vr#i", "#$vl\s*/foreach\s*$vr#i", "#$vl([0-9a-zA-Z_]+?)\.([0-9a-zA-Z_]+?)\.([0-9a-zA-Z_]+?)$vr#i", "#$vl([0-9a-zA-Z_]+?)\.([0-9a-zA-Z_]+?)$vr#i", "#$vl([0-9a-zA-Z_\[\]\'\"]+?)$vr#i", "#$vl([0-9a-zA-Z_]+?):(.*?)$vr#i" ); $replacements = array( "<?php include show('\\1'); ?>", "<?php if(\\1): ?>", "<?php else: ?>", "<?php elseif(\\1): ?>", "<?php endif; ?>", "<?php endif; ?>", "<?php if(count($\\1)>0):\$autoindex=0;foreach($\\1 as \\2):\$autoindex++; ?>", "<?php endforeach;endif; ?>", "<?php endforeach;endif; ?>", "<?php echo $\\1['\\2']['\\3']; ?>", "<?php echo $\\1['\\2']; ?>", "<?php echo $\\1; ?>", "<?php echo \\1(\\2); ?>" ); $body = preg_replace($patterns, $replacements, $body); file_put_contents($cache, "<?php if(!defined('INVIEW'))die('YIQU'); ?>".$body); } } $view = new view(); function show($tpl) { global $view; return $view->load($tpl); } ?> </code>
上面就是一个模版引擎了,看看如何使用
php 使用示例
<code><br><?php include 'view.php'; $title = '文档标题'; $a=array('a'=>'rows','b'=>array('c'=>'inarray')); include show('index'); ?> </code>
模版使用示例
<code><br> <title>{title}</title> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> 你好,仅供参考<br> {a.a} {a.b.c}<br> {foreach a:$v} {autoindex}.{v}<br> {/foreach} {include:footer} </code>
PHP 本身就可看作一個「模板引擎」。
<code> <title>=$title?></title> <main>=$main?></main> </code>
只要:
<code>$title = 'test'; $main = '<input>'; require('template.inc.php'); </code>
即可。
如需要得到內容而非直接輸出,只要:
<code>ob_start(); $title = 'test'; $main = '<input>'; require('template.inc.php'); $content = ob_get_clean(); </code>
包裝成函數,從功能上看,算不算用 PHP 實現了模板引擎呢?
php模板一般是翻譯型,是把特定的文件翻譯成php或html,然後輸出的是那個文件的結果
php本身可以看作是C语言的模板。。。
楼主说的模板引擎,大抵都是用正则表达式实现的。
{$user_name}--->正则匹配--->
没什么太高深的东西在里面,如果你愿意,直接采用原生模式嵌入到html即可,毕竟正则效率也不是那么高
我很多年前写的c++版本,供参考,实现比较土,编译原理都还给老师了
https://github.com/pi1ot/webapplib/blob/master/waTemplate.h
https://github.com/pi1ot/webapplib/blob/master/waTemplate.cpp

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6
視覺化網頁開發工具

WebStorm Mac版
好用的JavaScript開發工具