开发者

PHP equivalent to site.master?

开发者 https://www.devze.com 2023-03-03 14:40 出处:网络
One of the nice things about ASP.Net is the ability to set a site.master file that holds all the repeated HTML/code from a site开发者_如何学Python, and still be able to alter things on it from an indi

One of the nice things about ASP.Net is the ability to set a site.master file that holds all the repeated HTML/code from a site开发者_如何学Python, and still be able to alter things on it from an individual website page. (For example, changing the <title> tag for every page, or adding other things to the <head> of your document.)

In the past in PHP I've used Server Side Includes to remove duplicate HTML/code (such as the top of the document, main navigation, footer, etc.) but obviously you can't alter any of the contents from the page.

Is there any way to implement site.master type functionality in PHP? If not, what's a good way to remove repeated HTML/code while still being able to change things like the page's title, highlighted navigation, etc.

Thanks.


This is just called Template Engine and there is tons of engine for php.

Smarty is one of them


The simplest way of pulling in common fragments is to use require() which will allow you to include other files within the current file.

An example of how you might use it:

header.php - a reusable fragment

<div class="header">
    <h1><?= $pageTitle ?></h1>
</div>

pageContents.php

<body>
<?php
$pageTitle = 'Check it out, the Foobar page!';
require('header.php');
?>
blah blah
</body>


What you are looking for is a templating engine built for PHP. ASP is a framework which has templating built in which provides you the functionality that you are referring to (i..e templates, membership provider, etc.)

In order to get the same in PHP you will need to use either a framework or a templating engine.

Some samples:

  • http://www.webresourcesdepot.com/19-promising-php-template-engines/
  • http://www.phpframeworks.com/


there is no such functionality in PHP, you can move your components into separate files and include them in your main file using include or require functions.

Some frameworks do provide (simulate) such functionality though. e.g. In cakePHP you can have a layout file where you can output your pages.

0

精彩评论

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