开发者

PHP+CSS common code to multiple site

I'm learning to use PHP, CSS and XHTML. I would like to do a multiple site which have common style? How can I do some general background and how to import it to all my sites? I would like to have the following code in every page:

<div id="document">
<div class="nav">
<ul>
<li><a href="site/register">Register</a></li>
<li><a href="site/login">Log in</a></li>
</ul>开发者_C百科;
</div>
</div>
</ul>


Try using include: http://php.net/manual/en/function.include.php

<?php include("standardpage.php"); ?>

or across the web like this:

include('http://www.example.com/mymenu.html');

For CSS stylesheets you can use:

<LINK rel="stylesheet" href="http://www.example.com/sharedstylesheet.css" type="text/css" />


Utilizing a PHP framework or template language is a frequently employed idea. The concept of an HTML template in which you plug computed values or dynamic content into is an especially powerful one. This can be done with PHP itself (which is a template language) or with something like Smarty (which is a simplified template language written in PHP).

The typically accepted approach is to not go with a separate templating language (even though this is still common in certain applications), but to instead utilize a PHP based framework. Four of the most common ones include:

  • Zend Framework
  • Cake PHP
  • Code Igniter
  • Symfony

With that said, however, in some applications (such as developing a CMS meant for implementation by other developers, a mailing list tool which accepts common templates, or other similar ventures) a separate template language to expose a limited set of PHP functionality can be useful. In such cases there are a number of choices, but Smarty remains the most widely used:

  • Smarty

You can end up writing your own framework, many people do, the idea is to separate business logic from presentational logic. This can be achieved in an infinite number of ways, but existing well-supported frameworks are a strong choice over rolling your own if you are not a guru. The reason is that they typically enforce a solid Model-View-Controller schema and offer several useful abstractions which makes setting up templates and dynamic websites much simpler than simply in-lining a bunch of PHP and hoping for the best.

Mixing business and presentation logic is the easiest way to completely garble any intention and render your online application an intractable mess. It is also a common anti-pattern new PHP developers fall into because the language makes it so easy to do this (and many other things) wrong. It is important to be aware of this and make use of such abstractions you have available to minimize coupling.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜