Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing master pages functionality. PHP

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/
like image 235
lexeme Avatar asked May 18 '12 13:05

lexeme


People also ask

What is the function of master page?

A Master Page is a nonprinting page that you can use as the template for the rest of the pages in your document. Master pages can contain text and graphic elements that will appear on all pages of a publication (i.e. headers, footers, page numbers, etc.)

What is the use of master pages explain with suitable example?

A master page is an ASP.NET file with the extension . master (for example, MySite. master) with a predefined layout that can include static text, HTML elements, and server controls. The master page is identified by a special @ Master directive that replaces the @ Page directive that is used for ordinary .

What are the steps to create a master page?

To create the master pageIn Solution Explorer, right-click the name of your Web site, and then click Add New Item. Under Visual Studio installed templates, click Master Page. In the Name box, type Master1. Select the Place code in separate file check box.

What is the purpose of a master page in desktop publishing?

Use master pages to repeat design and layout elements on multiple pages in a publication. Using master pages for these common elements gives your publication a more consistent appearance and allows you to create and update these elements in one place, rather than changing them on each publication page.


3 Answers

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

like image 66
Tiberiu-Ionuț Stan Avatar answered Oct 10 '22 07:10

Tiberiu-Ionuț Stan


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)

like image 30
Dan Lugg Avatar answered Oct 10 '22 05:10

Dan Lugg


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/
like image 23
Somnath Muluk Avatar answered Oct 10 '22 05:10

Somnath Muluk