views:

130

answers:

3

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?

A: 

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');

?>
inakiabt
Just say no to smarty! PHP is already a templating language, why would you add another system to learn on top of that!
notJim
you say no to Smarty, I'm very confortable with it.
inakiabt
Smarty is a big time saver - quite easy to learn and a lot of people are spending their time adding new time saving features and closing security holes without you having to do anything.Smarty also allows you to have an open source templating base that you can use across multiple projects.I've been using it for about five years now and it's made a big difference on every project by saving development time, and making logic/presentation separation quite easy.
Aligma
A: 

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.

Marko
A: 

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.

luichanfe