| The purpose of this article is to demonstrate web | | | | < h1 >Our Company< /h1 > |
| templates and to quickly write one to experience | | | | {{content}} |
| how template toolkits work at the most basic level. | | | | <small>Copyright Foo Bar</small> |
| The goal is to help beginners understand the basic | | | | </body> |
| principles.What is a Web Template? | | | | </html> |
| The most common purpose of standard template | | | | -------------------- |
| toolkits is to separate presentation from logic. | | | | Note that we have defined a template variable |
| Presentation is usually referred to by markup | | | | above enclosed in {{}} as {{content}}. We will parse |
| languages like HTML, XHTML. Logic is the part handled | | | | this template variable in our Perl code in index.pl. |
| by programming languages like PHP, Perl, Python, | | | | -------------------- |
| Ruby, etc.Standard Web Template Toolkits | | | | #!/usr/bin/perl |
| There are many open source web template toolkits | | | | # file: index.pl |
| available. For example, | | | | use CGI qw/:standard/; |
| | | | print header;my $content = "Welcome to our |
| Perl: Template.pm module | | | | website. This is the main content area...blah blah, yada |
| PHP: Smarty | | | | yada yada";open(FH, 'template.inc') || die "Can't open |
| Python: Cheetah | | | | file; $!n"; |
| Ruby: Web::Template module | | | | while(my $line=) {$line =~ s/{{content}}/$content |
| The feature sets of these standard template | | | | ;print $line; |
| toolkits can be fairly large. There are many built-in | | | | } |
| functions that can completely separate presentation | | | | close FH; |
| from logic.Let's Write Our Own Web Template | | | | -------------------- |
| Toolkit | | | | Let's analyze the code further: |
| We will create two files: | | | | #!/usr/bin/perl |
| | | | use CGI qw/:standard/; |
| index.pl: will contain Perl codes | | | | print header; |
| template.inc: will contain HTML codes | | | | The above block initializes the perl script, uses the |
| Let's start easy and write the template.inc file: | | | | CGI module and sends some basic headers to the |
| -------------------- | | | | browser. |
| # file: template.inc | | | | my $content = "Welcome to our website. This is |
| <html> | | | | the main content area...blah blah, yada yada yada"; |
| <head></head> | | | | This declares what the content of the site will be. |
| <body> | | | | open(FH, 'template. |