Google
 
Web www.albaspectrum.com

computer, internet, programming, Microsoft Dynamics, Oracle, Java, J2EE, EJB, SAP, ecommerce strategies, hightech jobs, h1b, web design, MS SQL Server, reporting, customizations, software, ERP, MRP, accounting systems, CRM - popular articles

 

Designing Your Own Web Template Toolkit in 2 Minutes

What is a Web Template?
The most common purpose of standard template toolkits is to separate presentation from logic. Presentation is usually referred to by markup languages like HTML, XHTML. Logic is the part handled by programming languages like PHP, Perl, Python, Ruby, etc.

Standard Web Template Toolkits
There are many open source web template toolkits available. For example,

The feature sets of these standard template toolkits can be fairly large. There are many built-in functions that can completely separate presentation from logic.

Let's Write Our Own Web Template Toolkit
We will create two files:

Let's start easy and write the template.inc file:

--------------------
# file: template.inc
<html>
<head></head>
<body>
< h1 >Our Company< /h1 >
{{content}}
<small>Copyright Foo Bar</small>
</body>
</html>
--------------------

Note that we have defined a template variable above enclosed in {{}} as {{content}}. We will parse this template variable in our Perl code in index.pl.

--------------------
#!/usr/bin/perl
# file: index.pl
use CGI qw/:standard/;
print header;

my $content = "Welcome to our website. This is the main content
area...blah blah, yada yada yada";

open(FH, 'template.inc') || die "Can't open file; $!n";
while(my $line=) {

$line =~ s/{{content}}/$content/;

print $line;
}
close FH;
--------------------

Let's analyze the code further:

#!/usr/bin/perl
use CGI qw/:standard/;
print header;

The above block initializes the perl script, uses the CGI module and sends some basic headers to the browser.

my $content = "Welcome to our website. This is the main content
area...blah blah, yada yada yada";

This declares what the content of the site will be.

open(FH, 'template.inc') || die "Can’t open file; $!n";
while(my $line=) {

$line =~ s/{{content}}/$content/;

print $line;
}
close FH;

This is our template engine – 6 lines of code! It opens the template file, reads each line one by one. As soon as it finds the predefined template variable {{content}} it substitutes it with the $content variable and prints the output to the browser. If no match is found the line is simply printed to the browser as is. Finally out of the loop, it closes the file handle.

A more objective use of the code above would be:

Why use Web Templates?
Separating presentation from logic in web templates provide 2 important functions.

1. Efficiency for Designers and Programmers
Designers are usually interested with presentation aspects of a website while programmers are concerned with logic. As template toolkits can help separate this process designers don’t usually need to touch programming code and vice versa for programmers.

2. Reusing Common Page Elements
A common use of templates is simply to reuse the most common elements of a web page like the header (your company logo and slogan), menus (main navigation bar) and footer (copyright message).

Conclusion
Sometimes developers will use a very lightweight toolkit or a home-brewed system where the bulk of the presentation is separated from logic but not all. The code base can be much smaller if some mixing of presentation and logic is allowed.

There are advantages and disadvantages of both hybrid and full-scale template implementations. The two questions below can help you decide which approach to follow.

- Are you willing to maintain extra code to completely separate presentation from logic?
- Do you value simplicity more than complete purity and willing to live with a bit of ugly code?

Copyright http://www.cider9.com/ - All rights reserved. You are free to redistribute this article as long as you retain the above copyright notice.

Alba Spectrum popular articles series: FAQ, Reviews, Introductions, Product Selections, Advises, Definitions, online marketing

We are serving wholesale & retail customers in Illinois, California, Texas, Wisconsin, New York, Washington, Ohio, Michigan, Indiana, Arizona, New Mexico, Louisiana, Florida, Georgia, Minnesota, Utah, Virginia, Georgia, Hawaii, Iowa, Colorado, Ontario, Quebec, Alberta, British Colombia.  We also serve customer internationally in New Zealand, Europe: UK, France, Poland, Italy, Germany, Russia, India, Byrma, Thailand, Holland, Denmark, Sweden, Norway, Indonesia, Austria, New Zealand, Pakistan, Afghanistan, Iran, Spain, Argentina, Brazil, Chile, Uruguay, Paraguay, Peru, Equador, Colombia, Venezuela, Panama, Costa Rica, Canada, South Africa, Nigeria, Portugal, Greece, Turkey, Asia: India, China, Philippines, South Korea, plus business metros: Chicago, Los Angeles, Phoenix, Boston, Atlanta, Minneapolis, Fargo, Seattle, Miami, Orlando, Detroit, Buffalo, Toronto, Paris, London, Montreal, Denver, Warsaw, Berlin, Prague, Rome, Karachi, Sao Paulo, Rio de Janeiro, Moscow, Buenos Aires, Dehli, Mumbai, Beigin, Cairo, San Francisco, Fremont, Naperville, Oakland, Melburn, Sidney, Sent Petersburg, Tampa, New Orleans, Houston, Dallas, Mexico City, Bogota, Caracas, Lima, Salvador, Recife, Brasilia, Curitiba, Goiania. http://www.albaspectrum.com