Template Engine & Master Pages
Required Namespace
  • Ness
    • UI
      • Page

You can include this library to your code file by typing the below code;

use Ness\UI\Page


About Ness PHP's Template Engine #top

Ness PHP's master pages allows you to create layouts for your web pages in your application.It is designed to eliminate the need to write the same html code for each repetitive design every time.Once you design a template, you can easily relate it to all the pages and accelerate your coding experience to twice that. This topic can be thought of as the continuation of the 'View' topic you can read more about views Here


Create New Master Page #top

You can create unlimited master pages for your application for using them later with view files. All your master pages should be located in 'Template' directory which can be found in Application root. For example you can create two master pages one for backend(admin panel) and one for front-end (user-area) and switch between them while creating new views using the master pages you have created.

Here is a simple example for a Master Page definition. Save this file as adminpanel_master.php in Template directory.

                                
<!DOCTYPE html>
<html>
<head>
	<title>{@page_title} - Simple Blog</title>
	<link rel="stylesheet" type="text/css" href="<?= Ness\ContentManager::getStyleSheet("style.css");?>"> 

</head>
<body>

<div class="menu">
	<ul>
		<li>
			<a href="#">Dashboard</a>
			<a href="#">Profile</a>
			<a href="#">Create User</a>
			<a href="#">Edit Posts</a>
		<li>
	</ul>

<?php 
	/**
	 * This line below must me available in all your master pages.
	 * echo $this->contents; means your content place holder. Your data in
	 * views will be loaded here.
	 */
	echo $this->contents;
?>



<div class="footer">
	<h4>(C) Ness Tutorials - Sinan SALIH </h4>
</body>
</html>
                                    
                                
As you can realise there is basic design for our admin panel with menu, footer and a code line like; echo $this->contents; this code snippet is required for the inheritance. When you create a new sub-view from the template all codes (HTML+PHP) in your View/viewfile.php will be parsed and placed in the line where $this->contents is printed.


Template Inheritance #top

Using templates in Ness PHP is extremely easy. Just create a new view file and specify the master page you have created.

                                    
<?php
    $page = new Ness\UI\Page();
    $page->setLayout('adminpanel_master.php');
    $page->BeginContent();
?>
<!-- PAGE CONTENT STARTS -->


<!-- YOUR HTML/PAGE CONTENTS FOR THIS PAGE -->



<!-- PAGE CONTENT ENDS -->
<?php
    $page->EndContent();
?>
                                    
                                
As you can use we are using adminpanel_master.php template file. All contents between
$page->BeginContent();
and
$page->EndContent();
will be parsed and put in the template file where the
echo $this->contents;
is defined in adminpanel_master.php


How Template Engine Works #top


Template Engine Language #top

If you want to use template engine, you need to follow some rules. Some of them are like the ones below

  1. Content Place Holder Please do not forget to define and print $this->contents variable where you want to put your view's html contents.


  2. Define Parameters If you want to set parameters between layouts and views, you need to type {@parameter1} in your layout file. You can set a value for your parameters by typing $view_variable->SetParameter("parameter1", "my value is..");


  3. Page Title You can set a different title for every view. You can complete this task by setting a parameter but also if you type echo $this->title; on your template file you can use and set a title by SetTitle command


Methods #top

SetLayout
$view->SetLayout(string  $prm_file);
Use a template file in your views with this command
Return Void
Parameters $prm_file String; File name of master page



SetParameter
$view::SetParameter(string  $prm_name,string  $prm_value );
Set content for parameter defined in template page.
Return Void
Parameters $prm_name String; Parameter name available in template
$prm_value String; Value to set for the parameter



SetParameterNull
$view->SetParameterNull(string  $prm);
Set null value to unused parameter in template files
Return Void
Parameters $prm String; Parameter name



SetTitle
if you do not use parameters for setting title you can use this function. Remember <?php echo $this->title; ?> must be defined in template.

$view->SetTitle(string  $title);
Set a title for an inheritanced view
Return Void
Parameters $title String; Title set for the page



insertWidget
Page::insertWidget(string  $prm);
Include a widget from Application/Content/widget path
Return Object
Parameters $prm String; File name; ex: widget.html



BeginContent() & EndContent()

Start and Finish content definitions for view files when using a template.


$view->BeginContent();

    ...
    ..
    .
    Contents Here
    .
    ..
    ...

$view->EndContent();