php - 如何使用 PHP 将 HTML 转换为可实时编辑的模板?

标签 php html css templates system

<分区>

如何将 HTML 模板转换为实时的 php 可编辑模板?我希望能够设置标题、描述、添加图像和链接。 (类似于行为上传系统)

有人可以将我链接到教程吗? 非常感谢!

最佳答案

要以简单的方式做到这一点,您应该遵循以下 3 个步骤: 1- 在您的 HTML 模板中添加自定义标签 2- 创建一个类使您的 HTML 模板可写 3-加载类,编写模板,显示页面

  1. 首先,在您的 HTML 模板 (template.html) 中插入一些自定义标签,如下所示:

  2. 然后,创建一个快速类 (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;
}
  1. 最后,您需要做的就是创建您的 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 :使用您的模板的示例页面。

希望对你有帮助。 ;)

关于php - 如何使用 PHP 将 HTML 转换为可实时编辑的模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9004968/

相关文章:

javascript - Bootstrap 按钮-单选值未设置

css - Bootstrap 下拉菜单外观

javascript - 使用带有固定 Div 的 scrollTop 进行动画处理

html - 固定页眉、页脚和边栏,中间有滚动内容区域

php - CakePhp:读取 Controller 内的查看规则消息

PHP Sendmail 发送的电子邮件内容在包含 CSS 内联样式时不显示

javascript - json数组值没有传回jquery

php - 尝试将代理 Bootstrap 模板添加到 Laravel 5.4

javascript - AJAX 调用显示来自 html 中的 php 脚本的响应

javascript - 如何使用javascript获取方法参数的值?