Market Data Analyst (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

Templates, The PHPLIB Way
Using PHPLIB Template
Let's start with a cheesy example with some sample code. We'll assume that there is a template in the /home/mydir/mytemplates/ named MyTemplate.ihtml that has some text that reads something like this:
Congratulations! You won a new {some_color} Honda Prelude!
Notice that "{some_color}" has curly braces around it. The curly braces indicate that some_color is a template variable. We may want to write a PHP script that will load the template, insert the value of the PHP variable $my_color where the {some_color} template variable tag is, and then output the new text. If $my_color happens to be set to "blue", the final output should look like:
Congratulations! You won a new blue Honda Prelude!
Here's the PHP script that will do just that:

<?php

include "template.inc";

$my_color = "blue"; // we'll use this later

$t = new Template("/home/mydir/mytemplates/");
    
// create a template object named $t
$t->set_file("MyFileHandle","MyTemplate.ihtml");
    
// set MyFileHandle = our template file
$t->set_var("some_color",$my_color);
    
// set template variable some_color = $my_color value
$t->parse("MyOutput","MyFileHandle");
    
// set template variable MyOutput = parsed file
$t->p("MyOutput"); // output the value of MyOutput (our parsed data)

?>
The first line is an include command to give you PHPLIB Template functionality. Of course PHPLIB does a lot more than templates, but if you only want to use the template feature just include template.inc (template.inc is one of the files that comes with PHPLIB). PHPLIB Template uses object-oriented programming, so the next thing we do is create a Template object. The code <? $t = new Template("/home/mydir/mytemplates/"); ?> creates a new Template object $t. This $t object will be your handle for accessing all of the template functions for the rest of the PHP script. If you wanted to, you could create additional Template objects (each with their out template variable namespace), but one is usually all you need. The path ("/home/mydir/mytemplates/") in the Template constructor call sets the root path where your templates are located, but if you leave it out it defaults to the same directory as your PHP script.
Then we call set_file() to define a handle "MyFileHandle" linked to MyTemplate.ihtml (the template isn't actually loaded until parse() is called). By the way, the .ihtml extension of the template filename is customary for PHPLIB templates, but you can use .html, .tpl, or any other extension. Then we call set_var() to set the template variable some_color to the value of $my_color (which is "blue"), which means that all occurrences of {some_color} in the template will be replaced with the word blue when we call parse().
And then we call parse(), which parses MyFileHandle by loading MyFileHandle (MyTemplate.ihtml) and replacing any template variables ("{some_variable}") with their template variable values, and the resulting parsed text is placed in MyOutput. Nothing is output to the webserver until p("MyOutput") is called, which outputs the final parsed text.
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
*傍楼! 咯己苞 榴扁绰 具茄 悼康惑*蜡府窍11/17/05 14:32
我是我我是我我是我我是我我是我我是我我是我我是我我是我11/17/05 07:11
how to install only template.inc into our dirsuresh08/11/05 02:07
PHP and HTML sortedJaseTaylor04/17/05 17:55
How to send variables from java to php?php_java01/19/05 06:40
墨/靛/倒妨阜扁肺/噶啊瘤脚盒/怖 焊技夸!捞驱柳11/26/04 04:59
脚.侩.阂.樊.磊/寸老500/措.免.厚.过捞锐柳11/19/04 00:44
墨/靛/倒妨阜扁肺/噶啊瘤脚盒/怖 焊技夸!捞沥楷11/16/04 18:42
措'免'芭'例'矫'100%登'霸'窍'绰'规'过茄版驱11/15/04 19:24
PHP/CGI Desktop Prog HelpBill09/19/03 22:17
RE: How to integrate WYSIWYG editors?Manny08/30/03 15:25
Hey David, what about blocks?Abraham08/07/03 08:08
RE: Do you have to INSTALL phplib??Strong07/21/03 01:20
RE: Turning off $variable substitutionfrodo03/14/03 17:51
RE: Variable passed to reset() is not an arraArmino02/12/03 06:40
RE: How to integrate WYSIWYG editors?Dan Jallits12/23/02 20:38
RE: Variable passed to reset() is not an array...alex08/31/02 11:16
RE: Variable passed to reset() is not an array...xxx08/31/02 11:02
creating new templateLobster08/12/02 17:31
RE: Turning off $variable substitutionJohn Branstad08/02/02 11:50
Variable passed to reset() is not an array...alex07/31/02 11:39
RE: Sending output to a fileJason07/25/02 19:15
RE: Do you have to INSTALL phplib??omer dogan07/22/02 19:01
How to integrate WYSIWYG editors?Clive Richardson07/16/02 04:30
RE: Is This the Front of the PHP Templating CurveSarah07/08/02 22:25
RE: My view on templates and PHPSarah07/08/02 21:56
RE: Do you have to INSTALL phplib??Sarah07/08/02 21:53
Do you have to INSTALL phplib??Robert07/06/02 14:44
Turning off $variable substitutionBert beckmann07/02/02 02:41
RE: set_block no workie for you?Goobernutz06/18/02 10:47
RE: 'Not a directory'Mike Julian06/02/02 16:18
'Not a directory'Mike Julian05/28/02 21:26
RE: My view on templates and PHPars05/24/02 17:27
Sending output to a fileElizabeth Sterling04/10/02 16:59
RE: Is This the Front of the PHP Templating CurveGlen Hassell04/10/02 08:04
Is This the Front of the PHP Templating CurveRafiki Cai03/22/02 14:06
My view on templates and PHPChristopher Brown-Floyd03/16/02 11:07
where is correct path with FastTemplate..morris03/12/02 13:02
RE: Templates - Can Include PHP Code?Reini Urban03/05/02 17:13
& variablesSalvino02/21/02 06:30
RE: Templates in Templatesmaniyan s.v.s01/22/02 06:00
An excellent tutorial (en francais)guibod01/07/02 06:20
RE: include (); PROBLEMS :-((E. Sterling12/19/01 11:20
variables in an includeE. Sterling12/19/01 10:05
RE: Templates in TemplatesE. Sterling12/19/01 09:59
RE: Are templates search engine friendly?Samuel L. Diaz12/13/01 21:52
RE: Are templates search engine friendly?High-Fish12/10/01 16:38
RE: Templates and MySQLJacques Morin12/10/01 14:42
not an ideal templeteing systemBrian Cavanagh12/08/01 22:43
Templates and MySQLCrystal Cope12/06/01 10:57
RE: Item-SubItem using nested blocksJi Tao11/06/01 08:52
Are templates search engine friendly?Samuel L. Diaz10/29/01 22:15
RE: has anyone made this workTim Hewitt10/20/01 15:34
RE: How is this different from FastTemplate?Scott J. Miles10/18/01 16:12
RE: Item-SubItem using nested blocksRory10/16/01 06:00
Multiple Template DirectoryPhil Smalley10/03/01 05:43
How is this different from FastTemplate?Matthew Weier O'Phinney10/02/01 08:57
include (); PROBLEMS :-((Nick09/13/01 16:03
Warning while using blocksRalph08/15/01 07:03
Item-SubItem using nested blocksEmil Malinov06/30/01 18:04
RE: set_block no workie for you?Sondra Russell06/01/01 09:35
RE: Next articleMiles Lightwood06/01/01 09:18
Templates in TemplatesSergey Petrov06/01/01 03:05
php and Oracle on windows 98William & Jhon05/28/01 12:10
has anyone made this workCayley05/23/01 16:01
Exact way to put php into a .ihtml templateEdward Stoops Jr.05/22/01 09:12
flash integrationUdi05/15/01 00:31
A question about phplib template variablesTim Myers05/01/01 23:35
RE: Javascript problemR Kurniawan04/26/01 01:16
RE: Templates - Can Include PHP Code?Marinne Chuang04/17/01 13:56
Next articleNightOwl04/03/01 07:38
RE: Templates - Can Include PHP Code?Chris Saylor03/21/01 15:00
Backslashes appearing before every "Adomas03/11/01 09:59
RE: Templates - Can Include PHP Code?Phil03/08/01 22:04
RE: Templates - Can Include PHP Code?Peter Manchester03/08/01 08:40
RE: set_block no workie for you?dan donaldson03/07/01 18:20
set_block no workie for you?Denny03/01/01 16:42
Templates, The PHPLIB WayBill Eddins02/14/01 09:32
RE: Problems with template directoryRussell Johnston02/09/01 06:52
RE: Problems with template directoryBillboe02/06/01 21:47
RE: Javascript problemNicole01/31/01 17:36
RE: Javascript problemNicole01/31/01 17:35
RE: Javascript problemFinnur01/25/01 08:33
Problems with template directorycybernd12/09/00 11:17
RE: Using PHPLIB TemplateLen12/06/00 06:59
Using PHPLIB TemplateCory12/01/00 17:20
RE: Javascript problemTolga Orhon11/17/00 12:03
RE: set_blockDavid10/27/00 19:02
RE: set_blockDaniele Dignani10/11/00 03:25
RE: Templates - Can Include PHP Code?David Lizano10/10/00 11:43
set_blockMico Nast10/06/00 06:26
RE: Javascript problemAlejandro V醩quez09/27/00 10:39
Javascript problemDenis Healy08/18/00 08:27
Templates - Can Include PHP Code?Ken M. Mevand07/13/00 09:02
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.