Implementing master pages functionality. PHP
Asked Answered
T

7

6

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/

  1. I want the master page/layout to stay when user gets redirected to the other page
  2. 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/
Thacher answered 18/5, 2012 at 13:46 Comment(4)
Do you use any frameworks in PHP? PHP has no MVC out of the box, so you need to pick a framework that is MVC, and most of the time they all have their own "layout/templating" engine. Take a look at Code Igniter, Kohana, Sympfony, etc.Nadiya
I don't consider MVC pattern at all. All that is needed is a concept of master pages.Thacher
Well you compared to ASP.NET which is MVC, so I figured out you needed some kind of mvc engine...Nadiya
You will need MVC in any case if you want to use templates. Try out PHP fat free framework. It is very light. bcosca.github.com/fatfreeAppellation
T
1

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!

Thacher answered 23/5, 2012 at 5:20 Comment(0)
T
6

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: normal page

while an included (and repeatably includable) one could look like this: enter image description here

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).

Throckmorton answered 22/5, 2012 at 15:36 Comment(0)
M
1

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)

Mesarch answered 18/5, 2012 at 13:53 Comment(0)
H
1

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:

  1. Approximating Master Pages in PHP
  2. Is there anything like MasterPages on CodeIgniter Framework?
  3. PHP Equivalent of Master page in ASP.NET
  4. ASP.Net MVC or Zend Framework. What is your opinion
  5. http://hoolihan.net/blog-tim/2008/09/24/simple-masterpages-with-php/
Hirz answered 22/5, 2012 at 9:23 Comment(0)
G
1

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.

Gyrocompass answered 22/5, 2012 at 15:42 Comment(0)
T
1

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!

Thacher answered 23/5, 2012 at 5:20 Comment(0)
V
1

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>
Vapid answered 28/5, 2012 at 3:30 Comment(0)
A
1

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>  
Actinism answered 22/5, 2017 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.