Something struck me 2 days ago, but I didn't want to present it until I was fairly sure it was worth talking about. It appears that the use of mini-languages to generate views is a new concept.
For example, people have been taking advantage of the MVC paradigm for more than 20 years. But during my schooling days we used Smalltalk and C primarily and whenever we did MVC, all 3 components were written in the same language. In fact, for the things we did (e.g., race car games, ray tracing, ATM machines), it would've been nearly impossible to embed display logic in a view template.
Which brings me to my point: the use of mini-languages within view templates seems to occupy a very small and recent slice in the history of MVC frameworks, motivated primarily by the need to dynamically generate HTML. They are immensely popular in Perl.
- Could someone familiar with other languages (I'm especially curious about Ruby, PHP, Python) comment on how popular the mini-language approach to the view is in these languages? I think in Java that JSP is the most popular approach to mixing code with the view-template.
- Does the use of mini-languages strike you as a natural and productive step in MVC view programming?
Perl-related resources
- Template
- This module allows one to embed a
mini-language in one's view with optional escapes to pure Perl. It has
a number of possible output formats and a number of plugins available
for accessing the model in a convenient fashion.
- HTML::Mason
- Even though this module allows one to mix
Perl and HTML, it can be considered a mini-language of Perl because
once one uses Mason to do programming, the object-oriented inheritance
model available is more limited and rigid than Perl. Also, passing
arguments to a component does not use standard Perl syntax.
- Petal
- This is Perl implementation of a Zope/PHP mini-language
known as TAL
There has been talk of merging Petal with the Template Toolkit since they offer a similar approach via a similar language.
- HTML::Seamstress
- This module, written by me, offers a
non-embedded approach to HTML templating. It is inspired by XMLC, a Java framework which compiles
an HTML document to a Java class
accessible via the DOM API. Instead of XML::DOM, my module is
based on HTML::TreeBuilder and the new version with similar
compiler is in development. With the new API, you can take HTML like
this:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
<p>Hello, my name is <span klass=content id="name">ah, Clem</span>.
+
<p>Today's date is <span klass=content id="date">Oct 6, 2001</span>
+.
</body>
</html>
And after compiling hello_world.html into a Perl module named html::hello_world process it like this:
use html::hello_world;
my $tree = html::hello_world->new;
$tree->name('terrence brannon')->date('5/11/1969')->as_HTML;
- HTML::Template by samtregar
- A popular, robust, powerful mini-language templating system which also does just fine on normal text files. It has burners for the Bricolage content management system. It offers unmatched efficiency features such as JIT compilation.
- XML::LibXML
- another non-embedded approach to X/HTML templating. It was written by Matts and is part of the XML delivery toolkit, cpan://AxKit].
- Text::Template
- was the recommendation of Andy Wardley for those who felt restricted by mini-languages. While I have tried to present a balanced approach to this issue, I am sure that it is clear that I have a viewpoint on this issue, I will close with a quote from the Text::Template docs because it echoes my personal sentiments 110 percent:
When people make a template module like this one, they almost always start by inventing a special syntax for substitutions. For example, they build it so that a string like %%VAR%% is replaced with the value of $VAR. Then they realize the need extra formatting, so they put in some special syntax for formatting. Then they need a loop, so they invent a loop syntax. Pretty soon they have a new little template language.
This approach has two problems: First, their little language is crippled. If you need to do something the author hasn't thought of, you lose. Second: Who wants to learn another language? You already know Perl, so why not use it?
The "compilation" of the HTML file
Just in case you're curious here is the package that was generated via the file hello_world.html
package html::hello_world;
use strict;
use warnings;
use base qw(HTML::Seamstress);
my $tree;
my ($name,$date);
sub new {
$tree = __PACKAGE__->new_from_file('/home/terry/perl/hax/HTML-Seamstre
+ss-2.6/t/\
html/hello_world.html');
# content_accessors
$name = $tree->look_down(id => q/name/);
$date = $tree->look_down(id => q/date/);
# highlander_accessors
;
$tree;
}
# content subs
sub name {
my $self = shift;
my $content = shift;
if (defined($content)) {
$name->content_handler(name => $content);
return $tree
} else {
return $name
}
}
sub date {
my $self = shift;
my $content = shift;
if (defined($content)) {
$date->content_handler(date => $content);
return $tree
} else {
return $date
}
}
Related nodes
2004-12-27 Janitored by Arunbear - added readmore tags, as per Monastery guidelines
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|