Reputation: 2973
How to make master pages in php
? Like a Layout.cshtml
(and RenderBody()
) in ASP.NET MVC?
Thanks!
P.S. Maybe there's a third-party tool for the purpose?
EDIT
Ok. The thing is not about MVC architecture! Do look here: http://jsfiddle.net/challenger/8qn22/109/
I want the master page/layout to stay when user gets redirected to the other page
Want an average page to be nested inside the content division
. So if it is a form I want this form to be displayed like:
http://jsfiddle.net/challenger/XgFGb/17/Upvotes: 6
Views: 11501
Reputation: 241
Add this piece of code in the content area of your master.php file... I am using it like this and it perfectly working for me
<li><a href="master.php?page=blog.php">BLOG</a></li>
<div class="container content">
<?php
if(isset($_GET['page']))
{
$page_name = $_GET['page'];
include("/".$page_name);
}
?>
</div>
Upvotes: 1
Reputation: 2973
Ok. The solution that suits me the best is described here http://www.phpro.org/tutorials/Introduction-to-PHP-templating.html#9. It is easy, fast to imlement and doesn't enforce you to use a templating engine. Cool!
Upvotes: 1
Reputation: 6049
Here's what PHP standard framework/api supports:
The require("/definitions.php")
function loads class, function and constants defines from a file and outputs the content outside of PHP code to php://stdout (on a webserver this is what is sent to the browser). You might wanna use require_once
for importing dependencies (php files with definitions).
Use the PHP's open and close tags to obtain something close to templating functionality.
For example a normal page would look like this:
while an included (and repeatably includable) one could look like this:
I'm not saying "don't use templating engines", just showing a clear and simple way of achieving things which PHP is purposely built for. If this is enough for you needs, I then do say "don't use templating engines for the sake of it" (btw, if you are tidy, you can easily separate logic from views, without strict and sometimes cumbersome MVC frameworks).
Upvotes: 6
Reputation: 1618
A while ago (several years), I achieved something like this using Smarty, and extending it to contain a method to the effect of DisplayMaster("NameOfTemplate", "NameOfMasterTemplate")
It works by rendering a template and passing the result into another (master) template.
Above has 2 templates: NameOfTemplate, just has the main content section, e.g.
<div>...{$someProcessing}</div>
NameOfMasterTemplate has the outer html
<html>...<body><div class="layout">{$innerHtml}</div></body></html>
Upvotes: 1
Reputation: 6441
The Twig template engine offers Template Inheritance
The most powerful part of Twig is template inheritance. Template inheritance allows you to build a base "skeleton" template that contains all the common elements of your site and defines blocks that child templates can override.
Can be used as a standalone, but was made by the people behind the popular symfony framework.
Upvotes: 1
Reputation: 57756
In PHP, there is a very similar technique called templating. Instead of a master page, you have a template. The language itself has no built-in templating features, but there are third-party templating engines (Smarty, PHPTAL, and XTemplate, to name a few).
If you want to have "real" master pages, it is entirely possible to implement them. Just wrap you master page into a class and include() that class into your content pages.
Also Zend Framework supports a two step view, where a view template is rendered inside a layout template. I think this satisfies your need for master pages.
See following links:
Upvotes: 1
Reputation: 20612
Off hand, I know that the Laravel framework includes the Blade templating engine. It uses a syntax very similar to Razor.
Example:
@layout('master')
@section('navigation')
@parent
<li>Nav Item 3</li>
@endsection
@section('content')
Welcome to the profile page!
@endsection
(Razor, Blade, loller skates)
Upvotes: 1