如何将HTML模板转换为实时php可编辑模板?我希望能够设置标题,描述,添加图片和链接。(有点像behance上传系统)
有人能把我链接到教程吗?非常感谢!
要以简单的方式做到这一点,您应该遵循以下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;
}
- 最后,您所需要做的就是创建您的page.php。加载类,编写您想要的内容,并显示结果
类似于:
<?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:一个使用模板的示例页面。
希望它能有所帮助。)
- 将.html文件保存为.php
- 在你的PHP中,加载你想要填充模板的数据(这是一个巨大的主题,也是学习PHP的基础;我不能在SO上为你回答)
- 将加载的变量回显到模板中。初学者的方法是这样的。在"example.php"中:
<?php
//1. Populate the value from PHP somehow, such as from the GET variables in your HTTP request
$personsNameFromPHP = $_GET['personsNameFromGETVariables'];
//2. Echo the variable out in your markup like this:
?>
<div class="personsName">
<?php echo $personsNameFromPHP; ?>
</div>
W3学校是一个不错的起点。如果您已经了解PHP语法,请从学习MySQL数据库的工作方式以及PHP如何访问它们开始。如果你不懂PHP,W3Schools也有一些链接。
http://www.w3schools.com/PHP/php_mysql_intro.asp
HTH。