The pHpLib template is very easy to develop a website.The pHpLib basically based on a template class. which have only five function are used to differ the php code from html code.
The template class allows you to keep your HTML code in some external files which are completely free of PHP code, but contain replacement fields. The class provides you with functions which can fill in the replacement fields with arbitrary strings.
Firstly we create a object o template class.
<php
include('template.php');
$tpl=new Template();
>
The following function are mostly used in pHpLib
1. set_file
2. set_block
3. set_var
4. parse
1. set_file
This is basically use for set a file tpl.html file.It has two parameters one is handle another one is path+filename. where we write the html code combined with tempate variables and blocks.
synatx
set_file('file_handle', path+filename);
example
$templatePath ='/home/'
$tpl->set_file('template',$templatePath .'test.tpl.htm');
2. set_block
This is basically use for looping and conditional logic in html. we set a block means that inner part of block moving like for loop and condtion like if. This is must combined use with parse and set_var function.
synatx
set_block('file_handle','block_name','block_handle')
eg
$tpl->set_block('template','TPL_SHOW_DATA_BLK','HAN_SHOW_DATA_BLK');
3. set_var
This is used to set a php variable to template based variable.which used in template file.
In php we define a variable with $ sign. In php lib we use {} for a variable.
eg.
Php variable $flag
template Variable TPL_VAR_FLAG
synatx
set_var('template_variable_name',php_variable_name);
eg
$tpl->set_var('TPL_VAR_FLAG',$flag);
4. parse
This is basically used to parse a block and parse a file.It replace template_variable_name and blocks to corresponding htmlcode.
it has three parameters block handle,block name,true/false
if true, the substituted variables are appended to $target otherwise the existing value of $target is replaced
parse('block_handle','block_name',true/false);
eg.
$tpl->parse('HAN_SHOW_DATA_BLK','TPL_SHOW_DATA_BLK',true);
example 1
<php
include('template.php');
$tpl=new Template();
$templatePath ='/home/'
$tpl->set_file('template',$templatePath .'test.tpl.htm');
$tpl->set_block('template','TPL_SHOW_DATA_BLK','HAN_SHOW_DATA_BLK');
if($flag){
$tpl->set_var('TPL_VAR_FLAG',$flag);
$tpl->parse('HAN_SHOW_DATA_BLK','TPL_SHOW_DATA_BLK',true);
}else{
$tpl->set_var('HAN_SHOW_DATA_BLK','');
}
>
html code
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title> new document
</head>
<body>
<!-- BEGIN TPL_SHOW_DATA_BLK -->
{TPL_VAR_FLAG}
<!-- BEGIN TPL_SHOW_DATA_BLK -->
</body>
<html>