简体   繁体   中英

What is the equivalent to Master Views from ASP.NET in PHP?

I'm used to working in ASP.NET / ASP.NET MVC and now for class I have to make a PHP website.

What is the equivalent to Master Views from ASP.NET in the PHP world?

Ideally I would like to be able to define a page layout with something like:

Master.php

<html>
    <head>
        <title>My WebSite</title>
        <?php headcontent?>
    </head>
    <body>
        <?php bodycontent?>
    </body>
</html>

and then have my other PHP pages inherit from Master, so I can insert into those predefined places.

Is this possible in PHP?

Right now I have the top half of my page defined as "Header.html" and the bottom half is "footer.html" and I include_once both of them on each page I create. However, this isn't ideal for when I want to be able to insert into multiple places on my master page such as being able to insert content into the head.

Can someone skilled in PHP point me in the right direction?

You can use Smarty or develop your own template engine. Basically you can define a simple layout like this:

layout.tpl:

<html>
    <head>
        <title>My WebSite</title>
        {$header}
    </head>
    <body>
        {$bodycontent}
    </body>
</html>

define your header:

header.tpl

<script type="text/javascript" src="js/myjs.js"></script>

and your content file:

home.tpl

<div>hello {$var}! </div>

And finally your controller:

index.php

<?php
$header = new Smarty();
$headerOutput = $header->fetch('header.tpl');

$content = new Smarty();
$content->assign('var', 'world');
$contentOutput = $content->fetch('home.tpl');

$layout = new Smarty();
$layout->display('header', $headerOutput);
$layout->display('bodycontent', $contentOutput);
$layout->display('layout.tpl');

?>

Since PHP is a programming language and not a framework, it doesn't have that functionality out of the box.

There are many solutions to this problem, probably the most complex one would be to actually use a framework. There are many PHP frameworks to choose from. Downside would be that you'd be learning a framework and not PHP.

A simpler solution is to use a Templating engine . You'd be closer to PHP but you'd still have to learn how to use such engine.

And you're already using the simplest solution: including files from a main file. You can also include PHP files that is going to get executed and not only static html.

If you don't want to use a Templating engine, one solution could be like this:

<html>
    <head>
        <title>My WebSite</title>
        <?php include('headcontent'); ?>
    </head>
    <body>
        <?php include('bodycontent'); ?>
    </body>
</html>

And in bodycontent :

<?php
switch ($_GET['page'];
    case 1:
        include('page1');
        break;
    case 2:
        include('page2');
        break;
}
?>

bodycontent will be like a very simple controller.

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