pTemplate Better PHP Coding & HTML Design Separately

Intro

pTemplate is a PHP Class, designed & programmed by developer, for developer; to create native PHP applications, PHP website, big PHP projects better, nicer and easy to read by others in the team. pTemplate may help us process the tasks of PHP programming and design HTML separately, the coders just take the backend issue, not need to worry about the HTML layout; other designers in team focus on site HTML layout, design without any programming skills.

pTemplate mainly is inspired to avoid the mixture of PHP, HTML, CSS & JavaScript in the same file. This type of file is VERY HARD to read & VER DIFFICULT to maintain & understand; it's very hard to trace which closed parenthesis is for which opening one, where if, then, else statements we're viewing, etc; especially if you have new members join the team, they'll have to learn the code from beginning. Let's check the difference:

mixture of mess-up PHP, HTML

tags, before the hidden fields. * * @since 2.1.0 */ do_action( 'lostpassword_form' ); ?>

pTemplate, HTML markups only

[=INCLUDE_LOST_PWD_FORM=]

That's awesome! Size of our HTML file reduced about 50%. No dozens of PHP tags to show the variables only.

No more PHP tag, so not need to worry about runtime errors whenever we just miss a semi-colon, a parenthesis or just put if/else statement in wrong place. Super easy to read & debug HTML layout.

Do you feel exciting?

Especially: pTemplate is very helpful to the PHP projects that need theme's customisations from community. Or any commercial projects that have awesome themes. Theme makers may easily design HTML layouts, and they works for sure without needing to know about project's PHP backend.

Awesome features











Implement

Put the PHP file pTemplate.php into any folder you want, I prefer "includes" .
Include this file if you want to use. That's ALL!

Some necessary information to know


Basic usages

Initially, we should assume we already have all folders & files below for the examples during this documentation:


Add a file into pTemplate

												
/**
** @ index.php
**/

include_once( 'includes/pTemplate.php' );

// your PHP to process things here

$pTemplate->add_file( 'templates/index_homepage.html' );
$pContent = $pTemplate->pparse_file( 'templates/index_homepage.html' );
echo $pTemplate->pparse($pContent);
exit;

That's awesome finish! In the index.php above, we used the methods:


Shorter way to include

												
/**
** @ index.php
**/

include_once( 'includes/pTemplate.php' );

// your PHP to process things here

$pContent = $pTemplate->include_file( 'templates/index_homepage.html' );
echo $pContent;
exit;

Assign single content to pTemplate variables

We have 2 ways to assign single content to pTemplate variables, assign a single value or array of single values.

												
/**
** @ index.php
**/

include_once( 'includes/pTemplate.php' );

// process your $page_title & $page_content here

// assign a single value
$pTemplate->assign_var('PAGE_TITLE', $page_title);
$pTemplate->assign_var('PAGE_CONTENT', $page_content);

// assign array of values
$pTemplate->assign_vars(array(
	'PAGE_TITLE'	=> $page_title,
	'PAGE_CONTENT'	=> $page_content
));

$pTemplate->add_file( 'templates/index_homepage.html' );
$pContent = $pTemplate->pparse_file( 'templates/index_homepage.html' );
echo $pTemplate->pparse($pContent);
exit;

That's quick finish! In the index.php above, we used 2 new methods:


Assign a set of data to pTemplate variables

Above, we may easy assign the single values into pTemplate variables, but we always need to show the sets of data, such as: list of friends with full info, list of students, etc. Let's check the very simple example below:

												
/**
** @ index.php
**/

include_once( 'includes/pTemplate.php' );

// process your $page_title & $page_content here

// assign sets of data
$pTemplate->assign_block_vars('myfriend', array(
	'FULL_NAME'	=> $full_name,
	'PICTURE'	=> $picture,
	'ADDRESS'	=> $address,
	'EMAIL'		=> $email,
	'PHONE'		=> $phone,
	// more fields to assign
));

$pTemplate->add_file( 'templates/index_homepage.html' );
$pContent = $pTemplate->pparse_file( 'templates/index_homepage.html' );
echo $pTemplate->pparse($pContent);
exit;

We may assign unlimited records to pTemplate variable, myfriend with a tiny foreach / while loop, pTemplate should handle everything properly. This is really awesome process!


Show set of data through pTemplate markup

After assign a data-set into pTemplate variable, certainly we'll need to show them out, this task is really too easy & very fast to do with 2 simple pTemplate markup signs in HTML files.


  • [=myfriend.FULL_NAME=]
    Address: [=myfriend.ADDRESS=]
    Email: [=myfriend.EMAIL=]
    Phone: [=myfriend.PHONE=]

Our job is done! pTemplate shall automatically do a loop statement and show all records in myfriend we have assigned before. No more dozens of redundant PHP or HTML codes.


Show / Hide blocks of HTML code

In pTemplate, there's an awesome way to control the appearances of HTML sections. With a few characters, YES, just few characters, NOT code lines; in both PHP & HTML files, we'll be able to show / hide many HTML sections.

/**
** @ ucp.php
**/

include_once( 'includes/pTemplate.php' );

// your PHP to process things here

$signup_form = true;
if( $user_signing ) {
	$signup_form = false;
	$signin_form = true;
}

$pTemplate->add_file( 'templates/ucp.html' );
$pContent = $pTemplate->pparse_file( 'templates/ucp.html' );
echo $pTemplate->pparse($pContent);
exit;


This awesome task is done! We did not use any method, just pTemplate markups & some PHP variables to help pTemplate recognizes which section we're wanting to show.

In the above example, by default, we'll want to show the sign-up form; if guests browse to sign-in page, we'll set $signin_form = true & $signup_form = false to show sign-in section & hide sign-up section. We may also program more options for Forgot & others easily & rapidly.


Multilingual support & Constant strings

Generally, pTemplate use a GLOBAL PHP array variable - $lang to assign the GLOBAL constant markups, let's see below example to have a quick understand then we'll go further:

/**
** @ index.php
**/

include_once( 'includes/pTemplate.php' );

// your PHP to process things here
$lang['Page title'] = 'pTemplate: Better PHP Coding & HTML Design Separately';

$pTemplate->add_file( 'templates/index_homepage.html' );
$pContent = $pTemplate->pparse_file( 'templates/index_homepage.html' );
echo $pTemplate->pparse($pContent);
exit;

_lang{Page title}
pTemplate: Better PHP Coding & HTML Design Separately

pTemplate reserved a string _lang{} for parsing the GLOBAL constant markups. We may insert any character between curly brackets, pTemplate shall find, parse & display the reserved HTML codes automatically.


By default, if you forgot to define the data value for a constant string, pTemplate would use its index value to show. This is easiest & fastest solution to make PHP scripts run flawlessly. Example:

_lang{Some constant string you forgot to define}

Some constant string you forgot to define


But if you want to force other developers in team HAVE TO declare the language before may use, just set $bShow_language_index = false in pTemplate.php. We'll show blank space if some language string is still not defined.

	 
var $PTEMPLATE_CACHE_DIR,
	$bShow_language_index = true;
							

Now it's time to explain about multilingual support, if you want to show interface of multiple languages, just declare your languages into PHP files, it's the best solution I'll want to recommend, but you may try others. Let's check a simple below:

Examples

Multilingual support
/**
** @ lang_en.php
**/
$lang = array();
$lang['Contact']	= 'Contact us';
$lang['Support']	= 'Support';
$lang['Say hi']		= 'Say hi!';
/**
** @ lang_vi.php // Vietnamese
**/
$lang = array();
$lang['Contact']	= 'Liên hệ';
$lang['Support']	= 'Hỗ trợ';
$lang['Say hi']		= 'Xin chào!';
												
/*
* @ index.php
*/
include_once( 'includes/pTemplate.php' );

// include default language
include_once( 'includes/lang_en.php' );

// visitors choose Vietnamese lanauge
if( $dipslay_language == 'vi' ) {
	include_once( 'includes/lang_vi.php' );
}
 
$pTemplate->add_file( 'templates/multilingual_support.html' );
$output = $pTemplate->pparse_file( 'templates/multilingual_support.html' );
 
echo $output;
exit;

_lang{Contact}
_lang{Support}
_lang{Say hi}

Contact us
Support
Say hi!

Liên hệ
Hỗ trợ
Xin chào!

Some advanced usages

There are some useful complex usages I'll want to show below, please tell me if you find more new good cases.

Automatically show section if data-set is available

Let's review this simple case: we'll want to show a list of downloadable files to VIP member only. Evidently, we'll need to apply BEGIN_LOOP markup into <li> tag or some other tag. Let's check below example:

For some reason, sometimes we'll have an empty array for downloadable_list in runtime enviroment. The above code would be:

That's not what we're expecting. In order to solve this problem, let apply the START_SECTION markup before BEGIN_LOOP with the same name for both 2 markups. Better solution:

Examples

Section auto showed if data-set is available
include_once( 'includes/pTemplate.php' );

$socials = array(
    array(
        'name'  => 'Facebook',
        'icon'  => 'icon-facebook',
        'url'   => 'https://www.facebook.com/pages/PREscriptZ/1385919551647196',
        'title' => 'Like us',
        'text'  => 'Like us to get chance for 80% DISCOUNT'),
    array(
        'name'  => 'Twitter',
        'icon'  => 'icon-twitter',
        'url'   => 'https://twitter.com/PremiumScript',
        'title' => 'Follow us',
        'text'  => 'Follow us to get chance for 80% DISCOUNT'),
    array(
        'name'  => 'Google+',
        'icon'  => 'icon-google-plus',
        'url'   => 'https://plus.google.com/b/107963842839238880696/107963842839238880696/posts',
        'title' => 'Plus now',
        'text'  => 'Plus now to get chance for 80% DISCOUNT'),
);
  
foreach( $socials as $social ) {
    $pTemplate->assign_block_vars('social_list', array(
        'NAME' => $social['name'],
        'ICON' => $social['icon'],
        'URL'  => $social['url'],
        'TITLE'=> $social['title'],
        'TEXT' => $social['text'],
    ));
}
// do not MUST set $social_list = true
$pTemplate->add_file( 'templates/section_auto_show.html' );
$output = $pTemplate->pparse_file( 'templates/section_auto_show.html' );
 
echo $output;
exit;
Footer



While parsing the file (with downloadable_list markup inside), pTemplate shall detect automatically; if we have at least 1 record of data-set downloadable_list, it will start setting $downloadable_list = true; otherwise there's no $downloadable_list. Then certainly the block shall be hidden when output comes.

Furthermore, if you could control the data-sets better, or want to do more things before/after its output, let's read the below case.


Embed a data-set into section


Do some things before data-set showed up

Do more things after data-set already showed up

Fill a data-set into multiple pTemplate markups

We usually have this situation: with a data-set, we'll need to display in multiple places that do not have many big differences. pTemplate is great for these cases with some tweaks:

Examples

A data-set for multiple markups
	
include_once( 'includes/pTemplate.php' );

$socials = array(
    array(
        'name'  => 'Facebook',
        'icon'  => 'icon-facebook',
        'url'   => 'https://www.facebook.com/pages/PREscriptZ/1385919551647196',
        'title' => 'Like us',
		'text'	=> 'Like us to get chance for 80% DISCOUNT'),
    array(
        'name'  => 'Twitter',
        'icon'  => 'icon-twitter',
        'url'   => 'https://twitter.com/PremiumScript',
        'title' => 'Follow us',
		'text'	=> 'Follow us to get chance for 80% DISCOUNT'),
    array(
        'name'  => 'Google+',
        'icon'  => 'icon-google-plus',
        'url'   => 'https://plus.google.com/b/107963842839238880696/107963842839238880696/posts',
        'title' => 'Plus now',
		'text'	=> 'Plus now to get chance for 80% DISCOUNT'),
);
 
foreach( $socials as $social ) {
    $pTemplate->assign_block_vars('social_list', array(
        'NAME' => $social['name'],
        'ICON' => $social['icon'],
        'URL'  => $social['url'],
        'TITLE'=> $social['title'],
        'TEXT' => $social['text'],
    ));
}

$pTemplate->add_file( 'templates/dataset_multiple_markups.html' );
$output = $pTemplate->pparse_file( 'templates/dataset_multiple_markups.html' );

echo $output;
exit;
Header


main content here

Footer
Header
  • Facebook
    Like us to get chance for 80% DISCOUNT

  • Twitter
    Follow us to get chance for 80% DISCOUNT

  • Google+
    Plus now to get chance for 80% DISCOUNT

Footer

Show many sections with 1 pTemplate variable only

In contrast to above case, now we'll need to show multiple HTML sections with 1 pTemplate variable only. This case is easier & very simple to do. There's no limitation of pTemplate markups that pTemplate can parse, you'll only need to set as you wish.

Header

Downloadable area

main content here

Footer

Downloadable area

Embed a section into another sections

At present, pTemplate only supports 3 levels for section markups: START_SECTION START_SUB START_CHILD

Examples

A section in another sections
include_once( 'includes/pTemplate.php' );
 
$downloadable_area = true;

if( $user_type['VIP']==VIP ) {
	$vip_area = true;
	if( $user_type['PLATIUM']==PLATIUM ) {
		$platium_area = true;
	}
}
 
$pTemplate->add_file( 'templates/section_in_sections.html' );
$output = $pTemplate->pparse_file( 'templates/section_in_sections.html' );
 
$pTemplate->pparse( $output );
echo $output;
Downloadable area

Welcome to our downloadable area

This area is for VIP only

And this area is for platium only


Embed a boolean section into a data-set

For this case: we'll need to show / hide some boolean sections in a data-set output, depend on user's behavior. Example: guests only see names & pictures of our friends, we have rights to access friend's emails & addresses.

A section in another sections (long version)
include_once( 'includes/pTemplate.php' );
 
$friends = array(
				array(
					'Name'		=> 'Friend Name 1',
					'Email'		=> '[email protected]',
					'Address'	=> 'Friend 1 Address',
				),
				array(
					'Name'		=> 'Friend Name 2',
					'Email'		=> '[email protected]',
					'Address'	=> 'Friend 2 Address',
				),
				array(
					'Name'		=> 'Friend Name 3',
					'Email'		=> '[email protected]',
					'Address'	=> 'Friend 3 Address',
				),
			);
			
$public_email	= isset($_GET['email']) && !empty($_GET['email']) ? true : false;
$public_address = isset($_GET['address']) && !empty($_GET['address']) ? true : false;

foreach( $friends as $friend ) {
	$pTemplate->assign_block_vars('friend_list', array(
		'NAME'		=> $friend['Name'],
		'EMAIL'		=> $public_email ? $friend['Email'] : 'Private',
		'ADDRESS'	=> $friend['Address'],
					
		'PUBLIC_EMAIL'	=> $public_email,
		'PRIVATE_EMAIL' => !$public_email,
		'PUBLIC_ADDRESS'=> $public_address,
	));
}
 
$pTemplate->add_file( 'templates/section_in_dataset.html' );
$output = $pTemplate->pparse_file( 'templates/section_in_sections.html' );
echo $output;
Friend list
  • [=friend_list.NAME=]
    [=friend_list.EMAIL=]
    Private email
    [=friend_list.ADDRESS=]

A shorter version to solve this situation
include_once( 'includes/pTemplate.php' );
 
$friends = array(); // with above data
			
$public_email	= isset($_GET['email']) && !empty($_GET['email']) ? true : false;
$public_address = isset($_GET['address']) && !empty($_GET['address']) ? true : false;

// HAVE TO use HTML in PHP
foreach( $friends as $friend ) {
	$pTemplate->assign_block_vars('friend_list', array(
		'NAME'		=> $friend['Name'],
		'EMAIL'		=> $public_email ? $friend['Email'] : 'Private',
		'ADDRESS'	=> $public_address ? $friend['Address'] : '',
	));
}
 
$pTemplate->add_file( 'templates/section_in_dataset_alternate.html' );
$output = $pTemplate->pparse_file( 'templates/section_in_dataset_alternate.html' );
echo $output;
Friend list
  • [=friend_list.NAME=]
    [=friend_list.EMAIL=]
    [=friend_list.ADDRESS=]

Compress output, expect some sections

Sometimes we can not compress all HTML output, especially with HTML code we'll want to show with some syntax highlighter, such as: SyntaxHighliter, prettify, etc. Compressed output is going to show the mess-up only. For the case like this, you only need to surround the desired sections with START_SECTION donot_compress STOP_SECTION. Let's check the example:

Compress output, expect some sections
include_once( 'includes/pTemplate.php' );
 
$pTemplate->add_file( 'templates/pparse_file_compress_expect.html' );
// parse file: remove all GLOBAL markups, return result, compress result
$output = $pTemplate->pparse_file( 'templates/pparse_file_compress_expect.html', true, true, true );
echo $output;
All above HTML codes are compressed.
And now it's time to show uncompressed lines:

Method Referrence

pTemplate($root = "./")
$root path to theme directory you're using with pTemplate, by default it's root of your site
Return: none

pTemplate's constructor method. In the case you have many themes, let set the default one by passing into this $root parameter.


In my examples, default theme directory is root ./ with templates inside
include_once( 'includes/pTemplate.php' );

$pTemplate->add_file( 'templates/index_homepage.html' );
$pContent = $pTemplate->pparse_file( 'templates/index_homepage.html' );
echo $pTemplate->pparse($pContent);
exit;
Set default theme directory on initialization of pTemplate object
// MUST open pTemplate.php then edit
$pTemplate = new pTemplate('themes/plain/');

// now you may call without directory in paths
// because now root is 'themes/plain/'
$pTemplate->add_file( 'index_homepage.html' );

There's another way to change root directory without editing the pTemplate.php, by set_rootdir method below. This is main reason I help you create pTemplate object on including .


set_rootdir($dir)
$dir path to new root directory you want to change
Return: true if success or false

Change the root directory or the theme directory when you have many themes to show.


Examples

Set new root/theme directory
include_once( 'includes/pTemplate.php' );

$pTemplate->set_rootdir('templates/plain/');

// now root is 'templates/plain/'
$pTemplate->add_file( 'set_rootdir.html' );
$pContent = $pTemplate->pparse_file( 'set_rootdir.html' );
echo $pTemplate->pparse($pContent);
exit;

Honestly, I do not like this method, then prefer to set a constant to a directory. Let use what you prefer!

Define a constant to theme directory
define( 'THEME', 'templates/');
include_once( 'includes/pTemplate.php' );

$pTemplate->add_file( THEME . 'index_homepage.html' );
$pContent = $pTemplate->pparse_file( THEME . 'index_homepage.html' );

assign_var($varname, $varval, $bAppend = false)
$varname a GLOBAL pTemplate variable, must follow the roles at Implement section.
$varval a single data string shall be assigned to $varname, you may assign an empty string but this method is not designed for this task, you'll only waste more memory for this action; we already have START_SECTION for conditional statements.
$bAppend default is false, this method is set to override your existing variable.
Return: true

This method is root-level variable assignment. By default, assigns a single content to a GLOBAL pTemplate variable; override any existing content with the same name. Therefore, you MUST be careful if you don't want to erase some content.

Set the 3rd parameter $bAppend is true, we'll append our existing data.


Examples

Assign a single data string into pTemplate variable
												

include_once( 'includes/pTemplate.php' );

// PHP to process things of Index page here
$page_title = 'Demo: assign_var, assign single data string';
$page_content = 'Welcome to Demo: assign_var, assign single data string';

$pTemplate->assign_var('PAGE_TITLE', $page_title);
$pTemplate->assign_var('PAGE_CONTENT', $page_content);

$pTemplate->add_file( 'templates/assign_var.html' );
$output = $pTemplate->pparse_file( 'templates/assign_var.html' );

$pTemplate->pparse( $output );
echo $output;

[=PAGE_TITLE=]

[=PAGE_CONTENT=]

Demo: assign_var, assign single data string

Welcome to Demo: assign_var, assign single data string

Append a single data string
												

include_once( 'includes/pTemplate.php' );

// PHP to process things of Index page here
$page_title = 'Demo: assign_var, append single data string';
$page_content = 'Welcome to Demo: assign_var, append single data string';

$pTemplate->assign_var('PAGE_TITLE', $page_title);
$pTemplate->assign_var('PAGE_CONTENT', $page_content, true);

$pTemplate->add_file( 'templates/assign_var_append.html' );
$output = $pTemplate->pparse_file( 'templates/assign_var_append.html' );

$pTemplate->pparse( $output );
echo $output;

[=PAGE_TITLE=]

[=PAGE_CONTENT=]

Demo: assign_var, assign single data string

Welcome to Demo: assign_var, assign single data string

assign_vars($vararray, $bAppend = false)
$vararray an array of GLOBAL pTemplate variables, each item must have a pair of key & value; and they must follow the roles at Implement section.
$bAppend default is false, this method is set to override your existing variable.
Return: true

Like assign_var, this method is root-level variable assignment. By default, assigns array of single content into array of GLOBAL pTemplate variables; override any existing content with the same name. Certainly, you MUST be careful if you don't want to erase some content.

Set the parameter $bAppend is true, we'll append our existing data.


Examples

Assign array of single strings into pTemplate variables
												

include_once( 'includes/pTemplate.php' );

// PHP to process things of Index page here
$page_title = 'Demo: assign_vars, assign array of single strings';
$page_content = 'Welcome to Demo: assign_vars, assign array of single strings';

$pTemplate->assign_vars(array(
	'PAGE_TITLE'	=> $page_title,
	'PAGE_CONTENT'	=> $page_content
));

$pTemplate->add_file( 'templates/assign_vars.html' );
$output = $pTemplate->pparse_file( 'templates/assign_vars.html' );

$pTemplate->pparse( $output );
echo $output;

[=PAGE_TITLE=]

[=PAGE_CONTENT=]
										

Demo: assign_vars, assign array of single strings

Welcome to Demo: assign_vars, assign array of single strings

Append array of single strings
												

include_once( 'includes/pTemplate.php' );

// PHP to process things of Index page here
$page_title = 'Demo: assign_vars, assign array of single strings';
$page_content = 'Welcome to Demo: assign_vars, assign array of single strings';

$pTemplate->assign_vars(array(
	'PAGE_TITLE'	=> $page_title,
	'PAGE_CONTENT'	=> $page_content
	), true
);

$pTemplate->add_file( 'templates/assign_vars_append.html' );
$output = $pTemplate->pparse_file( 'templates/assign_vars_append.html' );

$pTemplate->pparse( $output );
echo $output;

[=PAGE_TITLE=]

[=PAGE_CONTENT=]
										

Demo: assign_vars, assign array of single strings

Welcome to Demo: assign_vars, assign array of single strings

assign_block_vars($varblock, $vararray)
$varblock name of data-set, must follow the roles at Implement section.
$vararray an array of data-set, each array item must have a pair of key & value; and they must follow the roles at Implement section.
Return: true

This method is a super fast solution to fill & show sets of data, with a few code lines of both PHP & HTML. By default, this method is designed to append our array data to set name. No override.

Normally, fill / display the data sets should go with the loop statements. Let's check the basic examples below:


Examples

Assign a set of data into pTemplate variables
										


include_once( 'includes/pTemplate.php' );

// assign a set of data
// usually data-set fetched from database
$socials = array(
	array(
		'name'	=> 'Facebook',
		'icon'	=> 'icon-facebook',
		'url'	=> 'https://www.facebook.com/pages/PREscriptZ/1385919551647196',
		'title'	=> 'Like us'),
	array(
		'name'	=> 'Twitter',
		'icon'	=> 'icon-twitter',
		'url'	=> 'https://twitter.com/PremiumScript',
		'title'	=> 'Follow us'),
	array(
		'name'	=> 'Google+',
		'icon'	=> 'icon-google-plus',
		'url'	=> 'https://plus.google.com/b/107963842839238880696/107963842839238880696/posts',
		'title'	=> 'Plus now'),
);

foreach( $socials as $social ) {
	$pTemplate->assign_block_vars('social_list', array(
		'ITEM_NAME'	=> $social['name'],
		'ITEM_ICON'	=> $social['icon'],
		'ITEM_URL'	=> $social['url'],
		'ITEM_TITLE'=> $social['title']
	));
}

$pTemplate->add_file( 'templates/assign_block_vars.html' );
$output = $pTemplate->pparse_file( 'templates/assign_block_vars.html' );

$pTemplate->pparse( $output );
echo $output;

add_file ($varfile)
$varfile path to file you want to add
Return: true if success or die()

Add a file into pTemplate to process & parse markups; you may add any text-based file: HTML, CSS, JavaScript, txt. Only need to correct the path exactly, or pTemplate.php issues a die() statement, then exit.

This method just adds the file to manage, there's no any parsing process started.


Examples

Add 1 file
												
include_once( 'includes/pTemplate.php' );

// your PHP to process things here

$pTemplate->add_file( 'templates/index_homepage.html' );
Add multiple files
												
include_once( 'includes/pTemplate.php' );

$pTemplate->add_file( 'templates/index_homepage.html' );
$pTemplate->add_file( 'templates/header.html' );
$pTemplate->add_file( 'templates/main_content.html' );
$pTemplate->add_file( 'templates/right_side.html' );
$pTemplate->add_file( 'templates/footer.html' );
Add then parse
												
include_once( 'includes/pTemplate.php' );

// PHP to process things of header here

$pTemplate->add_file( 'templates/header.html' );
$output = $pTemplate->pparse_file( 'templates/header.html' );

// PHP to process things of main content here

$pTemplate->add_file( 'templates/main_content.html' );
$output .= $pTemplate->pparse_file( 'templates/main_content.html' );

// PHP to process things of footer here

$pTemplate->add_file( 'templates/footer.html' );
$output .= $pTemplate->pparse_file( 'templates/footer.html' );

echo $output;
exit;

include_file ($varfile, $bRemove_VARS = false, $bReturn = true, $bCompress = false)
$varfile path to file you want to parse HTML markups
$bRemove_VARS if true, after parse the file, pTemplate shall remove all remaining GLOBAL, COMMON markups; normally this option goes with $bReturn = false when you want to print out the result after parsed, don't need to parse anymore
$bReturn default is true, method returns string for other processes; or returns true after print out
$bCompress set this option true when you want to compress the parsed result
Return: true if success or die()

This method is a combination of PHP include() & add_file: firstly it includes $varfile & processes PHP codes; then create a new temporary file (in PHP temporary directory) & call add_file to parse pTemplate markups; finally returns the result.

I'll NOT want to recommend this method, because the spirit of pTemplate is having awesome PHP & HTML codes separately; it's designed to strip out PHP codes in HTML files, for easy maintain PHP code & make HTML files become nicer to read. And you'll HAVE TO accept the PHP security errors if call this method, because it uses eval() to process the PHP codes.

But let use it as you wish


Examples

Add file with PHP codes into pTemplate
												


/*
* @ index.php
*/
include_once( 'includes/pTemplate.php' );

// PHP to process things of Index page here
$page_title = 'Demo: parse_file, with default parameters';
$page_content = 'Welcome to Demo: parse_file, with default parameters';

$included_file_title = '$var_of_title';

$pTemplate->assign_vars(array(
	'PAGE_TITLE'	=> $page_title,
	'PAGE_CONTENT'	=> $page_content,
	'INCLUDED_PAGE_CONTENT'	=> $pTemplate->include_file('include_file_php.php', false, true) // keep global markups
));

$pTemplate->add_file( 'templates/include_file_php.html' );
$output = $pTemplate->pparse_file( 'templates/include_file_php.html' );

echo $output;
exit;
												


/*
* @ include_file_php.php
*/

<?php global $included_file_title; echo $included_file_title;?>
<? $included_file_content = 'some HTML markups processed in include_file_php.php'; echo $included_file_content;?>

[=PTEMPLATE_MARKUP_KEPT_HERE=]

[=PAGE_TITLE=]

[=PAGE_CONTENT=]

[=INCLUDED_PAGE_CONTENT=]

Demo: include_file, include PHP file

Welcome to Demo: include_file, include PHP file
$var_title of included page
some HTML markups processed in include_file_php.php

[=PTEMPLATE_MARKUP_KEPT_HERE=]

pparse_file ($varfile, $bRemove_VARS = false, $bReturn = true, $bCompress = false)
$varfile path to file you want to parse HTML markups
$bRemove_VARS if true, after parse the file, pTemplate shall remove all remaining GLOBAL, COMMON markups; normally this option goes with $bReturn = false when you want to print out the result after parsed, don't need to parse anymore
$bReturn default is true, method returns string for other processes; or returns true after print out
$bCompress set this option true when you want to compress the parsed result
Return: true then print out if $bReturn = true or return the parsed content

After add file, we'll use this method to parse HTML markups in the file & fill our data into these HTML markups.


Examples

Add then parse file, with default parameters
												

include_once( 'includes/pTemplate.php' );

// PHP to process things of Index page here
$page_title = 'Demo: parse_file, with default parameters';
$page_content = 'Welcome to Demo: parse_file, with default parameters';

$pTemplate->assign_var('PAGE_TITLE', $page_title);
$pTemplate->assign_var('PAGE_CONTENT', $page_content);

$pTemplate->add_file( 'templates/pparse_file_defaults.html' );
$output = $pTemplate->pparse_file( 'templates/pparse_file_defaults.html' );

echo $output;
exit;

[=PAGE_TITLE=]

[=PAGE_CONTENT=]
										

Demo: parse_file, with default parameters

Welcome to Demo: parse_file, with default parameters

Add then parse file, keep GLOBAL markups
												

include_once( 'includes/pTemplate.php' );

// PHP to process things of Index page here
$page_title = 'Demo: parse_file, keep GLOBAL markups';
$page_content = 'Welcome to Demo: parse_file, keep GLOBAL markups';

$pTemplate->assign_var('PAGE_TITLE', $page_title);
$pTemplate->assign_var('PAGE_CONTENT', $page_content);

$pTemplate->add_file( 'templates/pparse_file_keep_vars.html' );
$output = $pTemplate->pparse_file( 'templates/pparse_file_keep_vars.html', false, false );

[=PAGE_TITLE=]

[=PAGE_CONTENT=]

[=SOME_GLOBAL_MARKUP=]

[=SOME_OTHER_MARKUP=]
										

Demo: parse_file, keep GLOBAL markups

Welcome to Demo: parse_file, keep GLOBAL markups

[=SOME_GLOBAL_MARKUP=]

[=SOME_OTHER_MARKUP=]

Add & parse file, then compress output
												

include_once( 'includes/pTemplate.php' );

// PHP to process things of Index page here
$page_title = 'Demo: parse_file, with default parameters';
$page_content = 'Welcome to Demo: parse_file, with default parameters';

$pTemplate->assign_var('PAGE_TITLE', $page_title);
$pTemplate->assign_var('PAGE_CONTENT', $page_content);

$pTemplate->add_file( 'templates/pparse_file_compress.html' );
$output = $pTemplate->pparse_file( 'templates/pparse_file_compress.html', true, true, true );

echo $output;
exit;

[=PAGE_TITLE=]

[=PAGE_CONTENT=]

Demo: parse_file, compress output

Welcome to Demo: parse_file, compress output

pparse (&$content, $bRemove_VARS = true, $bReturn = true, $bCompress = false)
$content content (HTML codes) you would like to parse
$bRemove_VARS default is true, after parse the file, pTemplate shall remove all remaining GLOBAL, COMMON markups; normally this option goes with $bReturn = false when you want to print out the result after parsed, don't need to use parsed result anymore
$bReturn default is true; method returns string for other processes, or returns true after print out
$bCompress set this option true when you want to compress the parsed result
Return: true then print out if $bReturn = true or return the parsed content

This is just a pTemplate's garbage collector, usually used before the output statement, used to translate all _lang{} strings & remove all forgotten GLOBAL, COMMOM markups that pparse_file leaved by $bRemove_VARS = false.

By default, I'll want to RECOMMEND this method before you would like to print out the soruce code, because we're professional & responsible developers , we can not show weird / uncomprehensible things to visitors.


Examples

Delete forgotten GLOBAL markups
										
										
include_once( 'includes/pTemplate.php' );

$pTemplate->add_file( 'templates/pparse_global_vars.html' );
// parse file, leave global markups for using later
$output = $pTemplate->pparse_file( 'templates/pparse_global_vars.html', false );
 
// after very very very very long code
// we forgot to remove GLOBAL markups
// just call pparse, no worry about where they're
$pTemplate->pparse( $output );
 
echo $output;
exit;
										

[=PAGE_TITLE=]

[=PAGE_CONTENT=]

[=SOME_GLOBAL_MARKUP=]

[=SOME_OTHER_MARKUP=]
										

Demo: Delete forgotten GLOBAL markups before output

Welcome to Demo: Delete forgotten GLOBAL markups before output

cache ($cache_name, $action = self::PCACHE_GET, $content = '')
$cache_name must give the cached content a name, this parameter could be a section name or a file name
$action default is $pTemplate::PCACHE_GET, get the cached content to display; or $pTemplate::PCACHE_SET for caching some content
$content must pass value into this parameter if want to set a static file
Return
- true if SET a cache content successfully or false
- string if there's a cache content to GET or empty

Usually we may cache the static files to make site loaded better & faster. pTemplate's cache method even gives you an ability to cache some HTML block, cache a part of files, or many parts of files.

Unfortunately, pTemplate does not provide the features of caching dynamic content. If need it, certainly we need a database to store the time of cache versions, how long they still stay, when we should update new content cache, etc. Generally, caching dynamic content is not simple task; hence, I provide a difference solution in other products.


CAUTION at present pTemplate only supports 1 level of caching sections. This means you MUST give the unique names for sections you would like to cache. Ex: live_header, live_header_other, live_header_more


Examples

Cache a static file
										
										
include_once( 'includes/pTemplate.php' );

$pTemplate->add_file( 'templates/cache_static_file.html' );
$output = $pTemplate->pparse_file( 'templates/cache_static_file.html' );
 
// cache footer file
$pTemplate->cache( 'cache_static_file.html', $pTemplate::PCACHE_SET, $output );
 
echo $output;
exit;
										
// GET a cached content
include_once( 'includes/pTemplate.php' );

$footer = $pTemplate->cache( 'cache_static_file.html' );

if( empty($footer) ) {
	$pTemplate->add_file( 'templates/cache_static_file.html' );
	$footer = $pTemplate->pparse_file( 'templates/cache_static_file.html' );
}
 
echo $footer;
exit;

Cache a section
										
include_once( 'includes/pTemplate.php' );

$cached_header = $pTemplate->cache('live_header');
if( !empty($cached_header) )
	$pTemplate->assign_var('CACHED_HEADER', $cached_header);
else
	$live_header = true; // MUST enable section if want to cache it

$pTemplate->add_file( 'templates/cache_section.html' );
// MUST apply parse_file to get parsed result before cache it
$output = $pTemplate->pparse_file( 'templates/cache_section.html' );
 
// cache header section
if( $live_header ) // still have not cache of this section yet
	$pTemplate->cache( 'live_header', $pTemplate::PCACHE_SET );
 
echo $output;
exit;
										

[=CACHED_HEADER=]
			

Header



compress(&$codes, $bRemoveComment = true)
$codes HTML code you would like to compress
$bRemoveComment by default is true: start removing all HTML comments
Return: none

A public method to give ability for compressing some HTML code.


Examples

									
									
include_once( 'includes/pTemplate.php' );
$html = "

Here's an example of HTML codes need to be compressed. Bold here / Italic now

"; $pTemplate->compress($html);

Here's an example of HTML codes need to be compressed. Bold here / Italic now


reset_files()
Return: none

Used in the case you want to keep pTemplate variables but remove all current files added by add_file method.


get_block( $blockname )
$blockname name of data-set you want to get all values
Return: array of data if success or false

For some reason, you'll need to re-assign a data-set into PHP variable, this method may help you quickly.


include_once( 'includes/pTemplate.php' );
  
$friends = array(
                array(
                    'Name'      => 'Friend Name 1',
                    'Email'     => '[email protected]',
                    'Address'   => 'Friend 1 Address',
                ),
                array(
                    'Name'      => 'Friend Name 2',
                    'Email'     => '[email protected]',
                    'Address'   => 'Friend 2 Address',
                ),
                array(
                    'Name'      => 'Friend Name 3',
                    'Email'     => '[email protected]',
                    'Address'   => 'Friend 3 Address',
                ),
            );
 
foreach( $friends as $friend ) {
    $pTemplate->assign_block_vars('friend_list', array(
        'NAME'      => $friend['Name'],
        'EMAIL'     => $public_email ? $friend['Email'] : 'Private',
        'ADDRESS'   => $friend['Address'],
                     
        'PUBLIC_EMAIL'  => $public_email,
        'PRIVATE_EMAIL' => !$public_email,
        'PUBLIC_ADDRESS'=> $public_address,
    ));
}

// re-assign data-set into PHP variable
$my_friends = $pTemplate->get_block( 'friend_list' );

get_section( $section_name )
$section_name name of section you would like to get stored data content
Return: string of data if success or empty

Sometimes, you'll need to re-assign a section into PHP variable, this method may help you quickly.


CAUTION at present pTemplate only supports data string for section names. This means you MUST give the unique names for sections you would like to get. Ex: header_menu, header_nav


Examples

									

Content before header

Header

Content after header

									
include_once( 'includes/pTemplate.php' );
$pTemplate->add_file( 'templates/index_homepage.html' );
$output = $pTemplate->pparse_file( 'templates/index_homepage.html' );

// get content of header section
echo $pTemplate->get_section('live_header');
Header
  • Facebook
    Like us to get chance for 80% DISCOUNT

  • Twitter
    Follow us to get chance for 80% DISCOUNT

  • Google+
    Plus now to get chance for 80% DISCOUNT

THE END