.. for purchasing our script. If you have any questions that are beyond the scope of this help file, please feel free to email via our user page contact form here or go to our support page.
please contact us with email you used to buy pTemplate, we'll release the key for live demo examples, you'll not need localhost tests.
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:
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.
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!
pTemplate.cache should be created right in the folder you used to contain this pTemplate.php, and the folder is only created once you start creating the file cache. Edit in code lines below if you want to change this folder.
function pTemplate($root = "./") {
$this->set_rootdir($root);
$this->PTEMPLATE_CACHE_DIR = dirname(__FILE__) . '/pTemplate.cache';
}
pTemplate.php, I created a new object $pTemplate, rename it if you do not like this variable.
pTemplate markups (used in HTML files) must be:
[=HOMEPAGE=], [=CONTACT_TITLE=], [=ALERT_MESSAGE=]
pTemplate variables (used in PHP files) must be:
HOMEPAGE, CONTACT_TITLE, ALERT_MESSAGE
pTemplate sections, data-sets (used in both PHP & HTML files) must be:
signup_form, service_list, product_dataset
_lang{} is reserved for parsing GLOBAL constant language strings.
donot_compress is name of section, reserved for marking uncompressed sections.
Initially, we should assume we already have all folders & files below for the examples during this documentation:
/** ** @ 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:
pTemplate
/** ** @ index.php **/ include_once( 'includes/pTemplate.php' ); // your PHP to process things here $pContent = $pTemplate->include_file( 'templates/index_homepage.html' ); echo $pContent; exit;
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:
pTemplate variable
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!
pTemplate container-variable
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.
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.
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.
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:
/** ** @ 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!
There are some useful complex usages I'll want to show below, please tell me if you find more new good cases.
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:
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;
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.
Do some things before data-set showed up
Do more things after data-set already showed up
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:
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;
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.
HeaderDownloadable area
Footer main content here
Downloadable area
At present, pTemplate only supports 3 levels for section markups: START_SECTION START_SUB START_CHILD
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 areaWelcome to our downloadable area
This area is for VIP only
And this area is for platium only
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.
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=]
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=]
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:
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;
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.
./ with templates insideinclude_once( 'includes/pTemplate.php' ); $pTemplate->add_file( 'templates/index_homepage.html' ); $pContent = $pTemplate->pparse_file( 'templates/index_homepage.html' ); echo $pTemplate->pparse($pContent); exit;
// 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:trueif success orfalse
Change the root directory or the theme directory when you have many themes to show.
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( '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)
$varnameaGLOBAL pTemplatevariable, must follow the roles atImplementsection.
$varvala 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 haveSTART_SECTIONfor conditional statements.
$bAppenddefault isfalse, 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.
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
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)
$vararrayan array ofGLOBAL pTemplatevariables, each item must have a pair of key & value; and they must follow the roles atImplementsection.
$bAppenddefault isfalse, 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.
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
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)
$varblockname of data-set, must follow the roles atImplementsection.
$vararrayan array of data-set, each array item must have a pair of key & value; and they must follow the roles atImplementsection.
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:
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:trueif success ordie()
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.
include_once( 'includes/pTemplate.php' ); // your PHP to process things here $pTemplate->add_file( 'templates/index_homepage.html' );
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' );
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)
$varfilepath to file you want to parse HTML markups
$bRemove_VARSiftrue, after parse the file,pTemplateshall remove all remaining GLOBAL, COMMON markups; normally this option goes with$bReturn = falsewhen you want to print out the result after parsed, don't need to parse anymore
$bReturndefault istrue, method returnsstringfor other processes; or returnstrueafter print out
$bCompressset this optiontruewhen you want to compress the parsed result
Return:trueif success ordie()
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
/*
* @ 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 ininclude_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 ininclude_file_php.php
[=PTEMPLATE_MARKUP_KEPT_HERE=]
pparse_file ($varfile, $bRemove_VARS = false, $bReturn = true, $bCompress = false)
$varfilepath to file you want to parse HTML markups
$bRemove_VARSiftrue, after parse the file,pTemplateshall remove all remaining GLOBAL, COMMON markups; normally this option goes with$bReturn = falsewhen you want to print out the result after parsed, don't need to parse anymore
$bReturndefault istrue, method returnsstringfor other processes; or returnstrueafter print out
$bCompressset this optiontruewhen you want to compress the parsed result
Return:truethen print out if$bReturn = trueor 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.
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
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=]
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)
$contentcontent (HTML codes) you would like to parse
$bRemove_VARSdefault istrue, after parse the file,pTemplateshall remove all remaining GLOBAL, COMMON markups; normally this option goes with$bReturn = falsewhen you want to print out the result after parsed, don't need to use parsed result anymore
$bReturndefault istrue; method returnsstringfor other processes, or returnstrueafter print out
$bCompressset this optiontruewhen you want to compress the parsed result
Return:truethen print out if$bReturn = trueor 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.
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_namemust give the cached content a name, this parameter could be a section name or a file name
$actiondefault is$pTemplate::PCACHE_GET, get the cached content to display; or$pTemplate::PCACHE_SETfor caching some content
$contentmust pass value into this parameter if want to set a static file
Return
-trueif SET a cache content successfully orfalse
-stringif there's a cache content to GET orempty
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
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;
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)
$codesHTML code you would like to compress
$bRemoveCommentby default istrue: start removing all HTML comments
Return: none
A public method to give ability for compressing some HTML code.
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:arrayof data if success orfalse
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:stringof data if success orempty
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
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');