|go ahead... be a heretic|
Name Change: From 'Simple Template' to 'Template with optional PHP execution'
Well, I've been hearing quite a bit about the 'FastTemplate' and other Template modules in several languages, (Perl and PHP in particular), and while I think it probably works quite well and does it's job properly, I have found that it can be a little too complex at times, especially when you do not need anything nearly so complicated.
I designed this template system because my website needed to have the template execute some PHP code within it, before being used by PERL as a template. Although it was designed to do that, the first method of use discussed here does not use PHP.
This is a quick tutorial on creating and using a simple template.
All right, the first thing you need is the HTML or other code you will be making into a template. The following is just our example code.
That should look like any other HTML document might since we have not started to make it into a template yet. Now, lets say we want the title to always be "MySite: page_name". We would change the code to something like this:
Now, once that is done, lets say we want to make the link point to another page specified by a program. It is just as simple as before:
Wherever we wish to include something that changes (The title, the page the link pointed to) we simply insert a Perl variable.
Here is another example, with a few more sections being determined by the program:
That example is a bit more complex, but it still works. Now, on to how to actually USE the template...
Using the Template:
Using the template is also quite simple. In the program that needs to use the template, simply assign values to each of the Perl variables you declared in the template. Here's an example:
Insert this into a file named "common.pl" and place it in the same directory with the template file (I suggest naming the template HTML something simple... like "template.html" and placing them all within a "Template" folder if possible)
Now we are ready to actually USE the template... this is a simple program which really doesn't do anything other than set variables and print the page:
This simple little bit of code takes all the variables used in the template and evaluates them. Causing Perl to replace all the variables in the template... with the values of the variable in the program.
This means that the title line now reads <TITLE>MySite: First Templated Page</TITLE>
That was a brief explanation of how to create those types of templates. More little hints and tips can be found just below this paragraph.
Hints & Tips:
If you have a variable that you don't think you will need to change often (IE: The copyright variable in the example) then declare it within common.pl to mean what it will normally mean.
You can actually have Perl execute any PHP you have on the template by using the LWP module (or sockets) to get the file, here's an example of how to do that (New version of common.pl):
Try to keep all the template variables in the same case to prevent mistakes with $MAIN vs $Main or $main
This system actually works with PHP pages as well as Perl due to the similar use of variables.
Although it won't be obvious, this site uses this template system. (Ok, shameless plug... it's my site)
I hope someone out there finds this useful. There may be a few mistakes in syntax as I wrote this just after I woke up. If you find one, please point it out and I'll try to have it corrected.
To use a $ in your template, you will need to escape it like you would in any perl string. \$ The same may apply to &, % and @.
Warning: This method does NOT work with use strict enabled... you may disable use strict around it however using no strict at the start and use strict once more at the bottom. To use 'no strict' you will need to place all of the code for the template into a block of it's own. Eg:
Other Templating Options:
HTML::Mason (Mentioned by wog )
Generic Search: Template