I have used FastTemplates for my PHP templating engine for years now, but it seems it is no longer supported. I still like it. It works well with some projects, but I decided maybe it was time to move on to something new. In my search, I came across a post from Web Resources Depot listing 19 PHP template engines. Some of these I knew (like Smarty), and most seemed more complicated than I needed. Let’s face it, I want to create web sites and develop PHP packages not learn a whole new markup system.
One, however, showed some promise: XTemplate with the promise of code/markup separation, light weight, and simple. It is all that, but the one thing it lacks is good documentation. Even the API document created by PHPDoc is little to no help in using the engine. I had to plow through the sample files, experiment, and guess. I even searched the web to no avail! No one has written much on this nice little template engine, but a lot of people are asking for tutorials.
In a effort to give this little engine a chance, I have decided to write a few tutorials myself. Besides, I need the references for my own use, and I might as well share.
The Basics
The heart of any template engine is the ability to replace a tag with useful information (possibly from a database or XML file). XTemplate uses curly brackets for its tags. For example: {TITLE} would be a tag you could use for putting a title to a page.
So, let’s get started with the simplest template examples.
The Template
<!-- BEGIN: main --> < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>{TITLE}</title> </head> <body> <h1>Hello, from {NAME}</h1> This is your {NUMBER} XTemplate tutorial example. Message: {MESSAGE} </body> </html> <!-- END: main -->
You will notice that the basic structure of the template is the same as any other XHTML file. The only difference is we have defined four XTemplate tags:
- {TITLE} — where we will put the page title
- {NAME} — where we will insert a name
- {NUMBER} — where we will insert a name
- {MESSAGE} — where we will insert a message
Also, note that the entire text is wrapped with two comments. These comments mark the beginning and end of a XTemplate block named main. The spacing is important, I learned, in order for XTemplate to read the block. There must be a space before BEGIN: and END:, and after the colon where you define the name of the block.
The Code
< ?php /* * File: sample1.php - first tutorial example for XTemplate */ include_once("xtemplate.class.php"); $xtpl = new XTemplate('sample1.xtpl'); $xtpl->assign('TITLE', 'Xtemplate Sample 1'); $xtpl->assign('NAME', 'E. E. Perry'); $xtpl->assign('NUMBER', '1st'); $xtpl->assign('MESSAGE', 'Congratulations on your first template using XTEmplate!'); $xtpl->parse('main'); $xtpl->out('main'); ?>
First, we include the XTemplate class (line 5). Then we create a new class object (line 7). We then call XTemplate’s assign function to assign a value to each of our XTemplate tags (lines 9-12). Next, we call the parse function to process the template’s main block (line 14). Finally, we call the out function to output the results to the browser (line 15).
Next time, we will set up dynamic block in our template, showing the extended capabilities of the class.
Hi
Could you tell me how to install xtemplate?
Can i install it on local windows pc?
if so how to do it.
There is no install really. You just add the template library file to your project, include it, and use it. No special install required. Download the files from here http://www.phpxtemplate.org/XTemplateDownloads.
Hi Thank you so much.
Can i use it for opencart? or only php
It is a PHP class. If opencart can make use of PHP classes then yes, otherwise, no.
sir please help me how to use a php x template because i don’t know to use it just reply immediately
Take a look at my other post on XTemplate, but that is about all I have to offer on the subject.
Thanks E E Perry, for your useful work.
I started readind this one and will continue up to the others.
In this tut, links to your site (For example Sample Page: http://samples.eeperry.com/xtpl_tuts/) relating to examples in the tut are dead, I’m sure you know about it.
Yes, the site I was using for the PHP samples has become unreliable. I have removed the links for now.
Thanks E E Peery sir for such descriptive information on XTemplate. I loved the way of using php apart from the native usage. I searched a lot about it, and this post gave me what I needed exactly. Sir, can you provide with some more resources on this topic (if any). Also list me the advantages of using XTemplate from native php coding.
Have a nice day !!
Regards,
Justin
I don’t have any plans on writing any more on this topic. The advantage, if any, it having a template that prevents you from repeating code over and over again 🙂
Brother, I developed a library based on xtemplate that allows you to inherit templates. That is, having a main layout template, to then introduce sub-templates of content and / or reusable.
https://gitlab.com/fdsoil/FDSoil/blob/master/class/FunXtpl.trait.php
https://gitlab.com/fdsoil/FDSoil/tree/master/html
https://gitlab.com/fdsoil/FDSoil/tree/master/packs/xtpl