简体   繁体   English

php 模板用于不同页面的不同 css 和 mysql

[英]php template for different pages with different css and mysql

Hi I'm trying to figure out how to do a PHP template system with a class, i dont like header.php footer.php or smarty or whatever blabla, just want the template.html, the class.php and page1.php, page2.php etc... with my own php code, and I've found a lot of website of people teaching how to do this but i still have lots of questions. Hi I'm trying to figure out how to do a PHP template system with a class, i dont like header.php footer.php or smarty or whatever blabla, just want the template.html, the class.php and page1.php, page2.php 等... 使用我自己的 php 代码,我发现很多网站有人教如何做到这一点,但我仍然有很多问题。

1) i want to add EXTRA css to some pages 1)我想在某些页面上添加额外的 css

2) SOME pages has php code like mysql queries and stuff like that 2)某些页面有 php 代码,如 mysql 查询和类似的东西

3) the CONTENT which would be a variable where ever i want in the template is not only words, instead is a large amount of divs and stuff, also in some pages the CONTENT variable has queries inside, like fillin a (eg)dropdown menu. 3)我想要在模板中作为变量的 CONTENT 不仅是单词,而是大量的 div 和东西,在某些页面中 CONTENT 变量内部也有查询,比如填写(例如)下拉菜单.

Hope somebody can guide me in this, i actually have my tempalte for explame, (the tags are just random)希望有人可以指导我,我实际上有我的模板来解释,(标签只是随机的)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<title>CompanyName | ##TITLE##</title>

<link href="layout.css" rel="stylesheet" type="text/css" />
<link href="styles.css" rel="stylesheet" type="text/css" />
##EXTRA_CSS##
<link rel="shortcut icon" href="/favicon.ico" />

<!--[if IE 6]>
    <script src="DD_belatedPNG_0.0.8a-min.js"></script>
    <script>
            DD_belatedPNG.fix('img, div');
    </script>
<![endif]--> 

##EXTA_JS##

</head>

<body>

    <div id="main">  
        <div id="container_black">
            <div id="container_white">

                <div id="container_header">

                    <div id="logo_top"></div>           
                    <div id="lineas_verticales_top">
                        <div class="volver_portada"><a href="index.shtml">Volver a portada</a></div>
                        <div class="english_spanish"><u>Español</u> | <a href="../en/index.shtml" class="english_spanish">English</a></div>
                    </div>
                    <div id="nav_bar_black"><div id="nav_bar_red"><div id="nav_bar_yel">

                        <ul class="menuholder"> 
                            <li class="menu_principal"><a href="#" title="Principal">Principal</a></li> 
                            <li class="menu_empresa"><a href="#" title="Empresa">Empresa</a></li> 
                            <li class="menu_productos"><a href="#" title="Productos">Productos</a></li> 
                            <li class="menu_clientes"><a href="#" title="Clientes">Clientes</a></li> 
                            <li class="menu_recetas"><a href="#" title="Recetas">Recetas</a></li> 
                            <li class="menu_contacto"><a href="#" title="Contacto">Contacto</a></li> 
                        </ul> 

                    </div></div></div>
                    <div id="topbg_degr"></div>

                </div>

                <div id="container_left">
                    <div id="conmargen_left_top"></div>
                    <div id="container_conmargen_left_middle">

                        ##CONTENT##

                    </div>
                    <div id="conmargen_left_bottom"></div>

                    <!--[IF INDEX]
                    <div id="fono"></div>
                    <div id="dir"></div>
                    -->

                </div>

                <!--[IF INDEX]
                <div id="nav"></div>
                -->

                <div id="container_right">
                    <div id="conmargen_right_top"></div>
                    <div id="container_conmargen_right_middle">

                        <!--[IF PAGE OR PAGE OR PAGE]
                        <a href="#" id="recetas_poster" title="recetas"></a>
                        [ELSE IF]
                        <a href="#" id="clientes_poster" title="recetas"></a>
                        [ELSE IF]
                        <a href="#" id="productos_poster" title="recetas"></a>

                        -->

                    </div>
                    <div id="conmargen_right_bottom"></div>
                </div>

                <!--[IF INDEX]
                <div id="frame_facebook">
                <a href="#" class="facebook" title="CompanyName"><span>CompanyName</span> en Facebook</a>
                <div class="breakL"></div>
                <fb:like href="#" layout="button_count" show_faces="true" width="100" font="tahoma"></fb:like>
                </div>
                -->

                <br/>   

            </div> <!-- cierre del container white -->
        </div> <!-- cierre del container black -->

        <div id="footer">   
                <div class="footer_comment">
                    CompanyName Todos los derechos reservados 2011
                </div>
        </div>

    </div> <!-- cierre del main -->
<br/>

</body>

</html>

for instance contact.php, the content would be a form, and on the top of the page i have this huge php code, where i validate and all.例如contact.php,内容将是一个表格,在页面顶部我有这个巨大的php代码,我在这里验证和所有。

I'd really appreciate if somebody would put me in the right path to do this.如果有人能让我走上正确的道路,我将不胜感激。 thank you in advance.先感谢您。

There are probably a hundred different ways you could approach this, each with their own advantages and disadvantages - using databases and/or ajax calls and/or headers and footers as you already said.可能有一百种不同的方法可以解决这个问题,每种方法都有自己的优点和缺点 - 正如你已经说过的那样,使用数据库和/或 ajax 调用和/或页眉和页脚。 You really have to work out which way works best for your particular project or style of coding, or both.您确实必须确定哪种方式最适合您的特定项目或编码风格,或两者兼而有之。

However - if you really just want 'template.html', but have some pages with PHP, javascript, MySQL or whatever else in them - then I would suggest.但是 - 如果你真的只是想要'template.html',但有一些页面包含 PHP、javascript、MySQL 或其他任何内容 - 那么我会建议。

Create template.html with placeholders such as {PAGE_TITLE}, {MAIN_CONTENT} or whatever you need.使用 {PAGE_TITLE}、{MAIN_CONTENT} 或任何您需要的占位符创建 template.html。

Create page1.php/page2.php etc and do any server side work, generate variables to match your placeholders.创建 page1.php/page2.php 等并执行任何服务器端工作,生成变量以匹配您的占位符。 It might be handy to store them as an array, ie:将它们存储为数组可能很方便,即:

PAGE_VARS array( 
                 ['TITLE'] => My Page
                 ['CONTENT'] => This is my page content
               )

At the end of your 'page' script, load the entire contents of template.html into a string在“页面”脚本的末尾,将 template.html 的全部内容加载到字符串中

$template = file_get_contents('template.html')

Then, build up your template with the replaced variables using either a basic loop:然后,使用基本循环使用替换变量构建模板:

foreach ($PAGE_VARS as $KEY=>$VALUE) {       
   $template = str_replace("{".$KEY."}",$VALUE,$template)
}

(Feel free to get clever and probably more efficient with some regular expressions above, this is just a quick example.) (随意使用上面的一些正则表达式变得聪明并且可能更有效,这只是一个简单的例子。)

Output your template. Output 您的模板。

echo $template;

You can create a quick class (class.php) like this that will be your 'template engine':您可以像这样创建一个快速的 class (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;
    }
}

In your template.html file, add special tags where you want to put your content, like this:在您的 template.html 文件中,在您想要放置内容的位置添加特殊标签,如下所示:

<html><head></head>
<body>
<div id="id1"><page_title></div>
</body>
</html>

... ...

Then create a function to write inside the tags (one per zone):然后创建一个 function 在标签内写入(每个区域一个):

function writetitle($s) {
    $GLOBALS['writes']++;
    $GLOBALS['page_title'] .= $s;
    return;
}

finally, in your page.php, call the class, write your content, and generate the page:最后,在你的page.php中,调用class,写入你的内容,生成页面:

require_once('class.php');
//Load Class
$template = new Template;
$template->load("template.html");

//Some query :
$query = mysql_query('SELECT...');
$res = mysql_num_rows($query);

//write your content :
writetitle('my title, '.$res.'');

//Generate the page :
$template->show();

Doing this you can create as many zones as you want.这样做,您可以创建任意数量的区域。 writetitle act like echo , so you can make queries and everything you want. writetitle 的行为类似于echo ,因此您可以进行查询和任何您想要的事情。

I hope it helps.我希望它有帮助。

Insetad of having PHP parsing your template files I would suggest writing the templates using PHP and not your own pseudo code.插入 PHP 解析您的模板文件我建议使用 PHP 而不是您自己的伪代码编写模板。

It's perfectly valid to write写是完全有效的

...
<body>
<?php if($fooBar == "hahahaha"):?>
  <a href="www.foobar.com">The cool foobar link</a>
<?php endif;?>
</body>
...

I would then create a layout file with all common html etc. Then using small template snippets to be inserted into the layout where the main content goes.然后,我将使用所有常见的 html 等创建一个layout文件。然后使用小的模板片段插入到主要内容所在的布局中。 By having actual PHP code in your templates the PHP enginge can handle them and you don't have to write your own logic to handle loops, if statements etc.通过在您的模板中包含实际的 PHP 代码,PHP 引擎可以处理它们,您不必编写自己的逻辑来处理循环、if 语句等。

Take a look at Zend Framework and you'll get a good idea of how you can write your template engine.看看 Zend Framework,您就会对如何编写模板引擎有一个很好的了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM