The aim of this article is to show net templates and to rapidly write one to expertise how template toolkits work on the most basic level. The aim is to assist inexperienced persons perceive the basic principles.
What is a Internet Template?
The most common function of normal template toolkits is to separate presentation from logic. Presentation is normally referred to by markup languages like HTML, XHTML. Logic is the half handled by programming languages like PHP, Perl, Python, Ruby, etc.
Normal Internet Template Toolkits
There are lots of open supply net template toolkits available. For example,
* Perl: Template.pm module
* PHP: Smarty
* Python: Cheetah
* Ruby: Internet::Template module
The function units of these standard template toolkits can be fairly large. There are lots of constructed-in functions that can completely separate presentation from logic.
Let's Write Our Own Internet Template Toolkit
We are going to create two information:
* index.pl: will contain Perl codes
* template.inc: will contain HTML codes
Let's start simple 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>
--------------------
Word that now we have defined a template variable above enclosed in as content. We are going to 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. That is the main content
area...blah blah, yada yada yada";
open(FH, 'template.inc') ~ die "Cannot open file; $!n";
whereas(my $line=)
$line =
shut FH;
--------------------
Let's analyze the code additional:
!/usr/bin/perl
use CGI qw/:standard/;
print header;
The above block initializes the perl script, makes use of the CGI module and sends some basic headers to the browser.
my $content = "Welcome to our website. That is the main content
area...blah blah, yada yada yada";
This declares what the content of the positioning will be.
open(FH, 'template.inc') ~ die "Cannot open file; $!n";
whereas(my $line=)
$line =
shut FH;
That is our template engine - 6 strains of code! It opens the template file, reads every 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 discovered the road is simply printed to the browser as is. Lastly out of the loop, it closes the file handle.
A extra objective use of the code above can be:
* make it a function and place in separate file so other *.pl information can name it
* as a substitute of exhausting linking content, go it as a practical argument in order that the person can declare and use template variables at will
Why use Internet Templates?
Separating presentation from logic in net templates provide 2 important functions.
1. Efficiency for Designers and Programmers
Designers are normally interested with presentation features of a web site whereas programmers are involved with logic. As template toolkits might help separate this course of designers don't normally want to the touch programming code and vice versa for programmers.
2. Reusing Frequent Page Components
A typical use of templates is simply to reuse the commonest parts of an internet web page just like the header (your company logo and slogan), menus (most important navigation bar) and footer (copyright message).
Conclusion
Generally builders will use a very lightweight toolkit or a house-brewed system the place the majority of the presentation is separated from logic but not all. The code base can be a lot smaller if some mixing of presentation and logic is allowed.
There are advantages and disadvantages of each hybrid and full-scale template implementations. The 2 questions under might help you determine which strategy to follow.
- Are you willing to take care of additional code to completely separate presentation from logic?
- Do you worth simplicity more than complete purity and willing to reside with a bit of ugly code?
This post is written by Samuel Jones 37. You can hire efficient virtual assistants at Myoutdesk.com.
No comments:
Post a Comment