<分区>
如何将 HTML 模板转换为实时的 php 可编辑模板?我希望能够设置标题、描述、添加图像和链接。 (类似于行为上传系统)
有人可以将我链接到教程吗? 非常感谢!
<分区>
如何将 HTML 模板转换为实时的 php 可编辑模板?我希望能够设置标题、描述、添加图像和链接。 (类似于行为上传系统)
有人可以将我链接到教程吗? 非常感谢!
最佳答案
要以简单的方式做到这一点,您应该遵循以下 3 个步骤: 1- 在您的 HTML 模板中添加自定义标签 2- 创建一个类使您的 HTML 模板可写 3-加载类,编写模板,显示页面
首先,在您的 HTML 模板 (template.html) 中插入一些自定义标签,如下所示:
然后,创建一个快速类 (class.php) 使您的自定义标签可写:
class Template { var $contents;
function load($file) {
if ($fp = fopen($file, "r")) {
$this->contents = fread($fp, filesize($file));
fclose($fp);
}
}
function replace($str,$var) {
$this->contents = str_replace("<".$str.">",$var,$this->contents);
}
function show() {
$search = array(
'/\t/', //Remove Tabs
'/<!--[^\[-]+?-->/', //Remove Comments
'/\n\n/' //Remove empty lines
);
$replace = array(
'',
'',
''
);
$this->contents = preg_replace($search, $replace, $this->contents);
echo $this->contents;
}
}
一旦完成,您必须创建一个函数来写入您的标签。在我的示例中,能够编写您的 <page_title>
标记,将以下代码添加到您的 class.php 文件中:
function writetitle($s) {
$GLOBALS['writes']++;
$GLOBALS['page_title'] .= $s;
return;
}
类似的东西:
<?php
require_once('class.php'); //Load Class
$template = new Template;
$template->load("template.html"); //Load HTML template
//Some query :
$query = mysql_query('SELECT...');
$res = mysql_num_rows($query);
writetitle('my page title went live!, '.$res.''); //write your content
$template->show(); //Generate the page
?>
writetitle 现在充当 echo,因此您可以进行查询和任何您想要的。
最后你有 3 个文件: tempalte.html :你的模板 class.php :你的模板引擎 page.php :使用您的模板的示例页面。
希望对你有帮助。 ;)
关于php - 如何使用 PHP 将 HTML 转换为可实时编辑的模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9004968/