Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php HTML临时模板系统_Php_Html - Fatal编程技术网

Php HTML临时模板系统

Php HTML临时模板系统,php,html,Php,Html,在我的网站上,我有许多名为新闻/博客/产品等的文件夹。。。所有这些部分实际上都是相同的设置,除了头部内部样式的小变化,而不是复制和粘贴相同的文件n次我希望有一个模板,我开始这样做: 每个部分都有一个名为(news.php/blog.php/products.php等)的“base.php”文件 每页有2个,包括: <?php include "../article_template/part1.html"; ?> // Each pages internal styli

在我的网站上,我有许多名为新闻/博客/产品等的文件夹。。。所有这些部分实际上都是相同的设置,除了头部内部样式的小变化,而不是复制和粘贴相同的文件n次我希望有一个模板,我开始这样做:

  • 每个部分都有一个名为(news.php/blog.php/products.php等)的“base.php”文件
  • 每页有2个,包括:

    <?php
         include "../article_template/part1.html";
    ?>
    
    // Each pages internal styling goes here
    
    <?php
        include "../article_template/part2.html";
    ?>
    
    
    //每个页面的内部样式都在这里
    
第1.html部分:

<?php
    require_once "directory/functions.php";
?><!DOCTYPE html>
<html><head>

<link rel="stylesheet" type="text/css" href="http://example.org/folder/style1.css">
<link rel="stylesheet" type="text/css" href="http://example.org/folder/style2.css">

<style type="text/css">

第2.html部分:

</style>

<title> <?php get_title(explode('/', $_GET['$url'])); ?></title>

</head><body>
<?php 
    include "../topborder.html";
    include "../banner.php";
    include "../navbar.html";
?>
<div id="container">
<div id = "article_container">
    <?php
        $uri = explode('/', $_GET['$url']);
        get_article($uri[0]);
    ?>
</div>
<?php include "../folder/sidepanel.html"; ?>
</div></body></html>


在第1部分和第2部分之间,我可以插入页面的内部样式。虽然这工作正常,但似乎有点不正常-我是否会在这样的设置中遇到许多性能或任何其他问题虽然这个设置对我来说比较容易,因为我只需要更改一个文件就可以影响所有区域,但我有点担心性能。有更好的方法吗?

这与WordPress页面使用的布局技术相同。你会没事的。如果你需要证明,下载一个免费的WordPress主题并查看一下

我有点担心演出。有没有更好的方法 这样做


为什么??在HTML之前的PHP标记意味着100%的无效。PHP将以PHP的形式运行&HTML
出现的第二次,它将以HTML的形式直接通过web服务器被吐出来。

重复自己在前面会花费更多的时间,如果你不得不返回并在多个地方进行相同的更改,那么以后可能会花费更多的时间。DRY(不要重复)减少了重复和随之而来的维护问题

在您的示例中,您包含不同的HTML文件,我们可以创建一个包含任何HTML模板的函数。优点:代码更短,不需要记住每个文件的路径,如果需要更改HTML文件,只需更改一次

<?php

    // Add this to functions

    function get_template($template = '') {

        $path = '/absolute/path'; // Absolute path to your project

        $templates = array(
            'topBorder' => '/views/topborder.html',
            'banner' => '/views/banner.html',
            'navBar' => '/views/navBar.html',
            'sidePanel' => '/views/sidepanel.html'
        );

        if(isset($templates[$template])) {
            $templateFile = $path . $templates[$template];
            if(file_exists($templateFile))
                include($templateFile);
        }

    }

    // Instead of including some static HTML
    // We only call the function with the corresponding template

    get_template('navBorder');
    get_template('banner');
    get_template('navBar');

Oo我从来没有使用过wordpress,很高兴知道有人在使用它,但是不是有一个问题,那就是下载一个php文件,其中包含所有内容—下载基本文件,然后下载两部分?它的工作量是原来的3倍?是的,但您是从同一台服务器下载的,维护性会得到回报。当然,在一个文件中速度会更快,但可维护性要差得多。这是真的,刚刚签出一个wordpress主题,我想我会使用这个系统:pConsider使用DRY原理(不要重复自己),而不是使用
include../navbar.html“
get\u navbar()之类的函数替换它
或可能是具有多个模板的函数,如
获取视图(“导航栏”)