简体   繁体   中英

What is PHP's equivalent of an ASP.NET MasterPage?

I am new to PHP. How can I create a base (like an asp.net masterpage) and have all other pages inherit from the base page or designate that I want pages to inherit from a certain base page, so I don't have to recreate things like headers, navigation, footers, etc...

Is it just more or do ASP.NET Masterpages seem to slow things down and add unnecessary clutter?

There is no such thing in php; however, there are 2 functions you can use to simulate that.

include() and require().

Like this

top.php

  <html>
<head>
<title>google<title>
<!--css and script includes-->
</head>
        <body>
<div id="top">
    <ul id="menu">
      <li><a>link</a></li>
    </ul>
</div>

Other.php

random text

The two constructs are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error.

Php doesn't have masterpages.

You can however create functionality for them.
Its not exactly a master template tho

Here is a simple example:

header.php

<!doctype><html><head></head><body>

index.php

<?php include 'header.php'; ?> // more html here // <?php include 'footer.php'; ?>

footer.php

</body></html>

I'd suggest you to use a template engine which supports features like inheritance. Using plain PHP for templates is a PITA.

Twig looks pretty nice and supports template inheritance: http://www.twig-project.org/

I don't know, what a masterpage should be - but normally your start with a single index.php, which includes all necessary libraries, files and also templates, which are used to format the output. To request a single page, normally you use a request-parameter on that index.php.

There is a VERY nice way using functions:

ob_start();
ob_get_contents();
ob_end_clean();

See an example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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