Templating Engines
TEMPLATE ENGINES
·
Template View -- the look
and feel of the site
·
Servlet
Controller -- who is it? what do they want?
·
Java
Model -- ordinary business logic objects, JavaBeans
Template
engines take pure markup language as input and generate Java methods. This is a different program flow compared to
JSP. Instead JSPs calls logic from the
application and the application server (i.e., Enhydra)
uses methods to affect the values of the page.
Using
templating engines, however, the designer can make changes as often as needed
and resupply the markup page or encapsulate HTML. These get compiled for you into Template objects, which generate HTML
dynamically, using data provided by your servlets.
By using a
template engine intended for this exact use instead of the general-purpose JSP,
the underlying design can be cleaner, the syntax clearer, the error messages
more meaningful, and the tool more customizable. Several companies have built
such engines, the most popular probably being WebMacro
(http://webmacro.org,
from Semiotek) whose engine is free.
Using a dedicated template engine instead of
JSP offers several technical advantages that developers should be aware of:
WebMacro:
·
WebMacro is a 100% Java open-source servlet
framework that enables programmers and designers to work together while
promoting the Model View Controller
pattern. WebMacro is a viable alternative to
Java Server Pages, PHP, and ASP. It's the fundamental page technology behind
several major websites such as AltaVista.
FREE
Apache Velocity:
“Best
Practices” http://dcb.sun.com/practices/profiles/velocity.jsp
“You make the
decision” JSP vs. Velocity http://jakarta.apache.org/velocity/ymtd/
FREE
Java Templating Engines not
reviewed:
http://freemarker.sourceforge.net/
ClearSilver:
http://www.clearsilver.net/
ClearSilver is a Cross-language templating system.
FREE
Other Templating Systems:
Python: AHTS,
htmltmpl, SkunkWeb, wt
Perl: Mason,
Embperl, HTML::Template
PHP: Smarty,
FastTemplate
Apache Turbine:
http://jakarta.apache.org/turbine/

Opinions about JSPs and
Templates (from newsgroups):
[Colleen] So
JSP is a bad idea and JSP is a part of J2EE… so the best way to maximize the
benefits of using Java and maintain the Model-View-Controller is through Java templating systems (Webmacro, Velocity,
clearsilver)?
[JSP fan] I could not disagree more! JSP is *not* a bad idea. In fact it is a very *good* idea. J2EE (most notably EJB) is *not* a bad idea
in *all case*, though EJB is often overused.
I have used JSP on the last 4 projects I have worked on, and it has been
excellent to work with every time.
[JSP fan] At the same time, I would recommend one of the JSP
frameworks out there. Two that I've been
meaning to use are Struts and WebWork.
[JSP fan] http://jakarta.apache.org/struts/
[JSP fan] http://sourceforge.net/projects/webwork
[JSP fan] Struts is more mature, though.
[JSP fan] Another topic to follow is the standard JSP tag
library. Sun is working on a set of
standard JSP tags that all servlet engines must provide. These tags provide basic functionality like
iteration and i18l support. You can get
a glimpse of what these will be like on the Jakarta Taglibs
project:
[JSP fan] http://jakarta.apache.org/taglibs/i
[Templates/Clearsilver fan] I've read
about the promises of Struts and JSP tags to help you clean up and keep JSP
organized. However, I like real world examples. While I can find many examples
of people writing sites in JSP, and even PHP, I've not heard those people extolling
its flexibility.
[Templates/Clearsilver fan] Here are a few
things we were able to achieve with Clearsilver's
cousin CS/HDF at eGroups.com and Yahoo:
- easily mixed site pages between full-C, full Python, or
Python/C while sharing the same page
template parts. (same
goes for any language)
- easily had pages which render partially in fast shared C
code and partially in slow script code (Python).
- easily converted pages or parts of pages from Python (slow)
to C (screaming fast) without changing page templates.
- Changed 90%
of the look and feel from egroups.com to Yahoo Groups, with one programmer in under 14 days.
He didn't
need to change the application code at all to implement the new look, and he
did NOT break the old look and feel. We then had a few programmers implement
the new required functionality and complete the project. It took longer to
DESIGN the new UI than it did to implement it. Our QA team much appreciated the
fact that few bugs were created in the process because the application logic was
untouched.
- Shiped Yahoo Groups-US while the remaining internationals
ran the old eGroups UI....with the same code.
- Kept eGroups
[Templates/Clearsilver fan] This great
code/presentation separation was the reason all this was possible. Moving
forward without this would have required code branches and committing and QAing the same code to two lines of development because we
had to make significant backend changes to support integration into Yahoo.
[Templates/Clearsilver fan] There are many
other benefits of using template systems like Clearsilver
or Velocity which JSP simply does not provide, such as:
1) Template systems create a far simpler development
model where you first develop your code to output just a raw dataset (which is viewed
as a text list of hierarchical key/value pairs). Then you drop in the template
and plug in your values. This allows you to separately debug your code from
your HTML and then rapidly go from a static HTML mockup to a working page, with
just some simple cutting and pasting.
2) At any time you can look at the contents of the
raw-dataset (just by dropping in an extra URL parameter). This makes it much
easier to debug and fix broken HTML or oddly operating pages. It also makes it
possible for QA level people to get access to the raw data from a page to help
qualify problems.
3) Using Clearsilver, the
entire page is rendered before it is output to the user. This reduces load on
your database by allowing transactions to finish faster, and it also allows you
to get real-time data about page render performance. Without this, any page
performance becomes dependent on the user's download speed.
This makes it terribly difficult to debug page speed
problems.
4) Using C/Clearsilver to
render pages is 'really' fast, as fast as using straight C. Using Python/PyApache/Clearsilver can be faster than Java/JSP by a good
margin depending on the operations in the page.
5) Code is decoupled from the template. There is no
"object model" which must remain in place for the template to run.
The code can be optimized at will, or completely rewritten, because it is
merely required to output the same dataset as it did before for the template to
run. You can get this property with well written PHP or JSP, but it's really
hard to avoid all the ways you can break this. In real template systems, this
is the only way to write the page, so it can't be broken.
[JSP fan] Ok,
I'm gonna be blunt.
There is *no* benefit to a templating engine
over JSP. I think one of the biggest
issues of template engines is finding developers. You'll have a much easier time trying to find
an experienced JSP developer than an experience Velocity developer. This has nothing to do with the technology,
but due to the fact that there are more people using JSP than templating engines.
[Colleen]
One of the benefits of JSP, it seems,
is that it allows designers to pick up the technology fast. While this can be detrimental, in some ways
it's beneficial. We have a lot of designers
here and only one technical person, is it very
difficult to teach non-techies to use a template
engine?
[JSP fan] I
would guess that JSP, PHP, and templating engines
would all have similar learning curves for graphic designers. When it comes down to it, they are all
programming languages and you're going to end up teaching a non-technical
person how to program. If you need to do
server side programming, hire a programmer!
A designer *will* make a mess of it, no matter which language or
technology you choose. Let designers do
what they do best and programmers do what they do best. Forcing one or the
other into the other role is asking for trouble.
[Templates/Clearsilver
fan] I agree that a dynamic application is
basically an application no matter how you slice it. However, I find that even
programmers will easily break other programmers’ pages written in PHP or JSP.
Using a template language isolates ALL the application logic from the template.
A programmer might break the form by incorrectly putting elements into the new
page, but they won't break the underlying code because they don't have to touch
it.
JSPs vs. Templates – Opinions
on Session Caching
[Templates/Clearsilver fan] The REALITY (imo) is that [centralized session caching] creates a whole
lot of bad architecture which is impossible to performance optimize. You really
don't want "centralized" app server which keeps persistent state and
can crash. You really shouldn't use a centralized session caching server because
it does not scale. If you want to cache something small, put it in a cookie, if
you want to caching something big, put it in a caching
server which can be segmented by some kind of key.
[Colleen] Has anyone else on the list had some
worthwhile experiences using App servers?
[JSP fan] I have something to add and I would say the exact
opposite in regards to centralized session caching. Servlets do session tracking and provide you an place to store session-related information. This is a great place to put transient
information and it *does* scale. Any
servlet engine worth its salt should provide some means of distributed session
management.
[Templates/Clearsilver fan] The largest
sites on the web (Yahoo, Amazon, MSN, About, eBay, AmericanGreetings, Weather
Channel, Google) do not use server-side session management for a reason.
Centralized Session servers/caching use more hardware to render pages slower.
[Templates/Clearsilver fan] For example,
let me explain a little bit about the network architecture for Yahoo Groups,
where I used to run the engineering team.
[Templates/Clearsilver fan] There is a big
router/load balancer at the top, which routes every page request into one of 60
webservers. There is no webserver affinity. We found
that using server affinity caused load distribution problems, which resulted in
users getting slow pages from overloaded webservers.
[Templates/Clearsilver fan] 50-80% of page renders do not touch the database. The
information from the database which is required to render most pages (i.e.
group title, custom colors, which features are available at your access level) are cached into a "group cookie" on the client.
That way, when you are paging around the message archives, the webserver only needs to contact one external server, where
the message data is stored.
[Templates/Clearsilver fan] If there were
central session serving where information were cached instead of in cookies,
then
(a) You'll
need twice as much hardware as if you were using
cookies.
One might
argue that you don't use session serving very often.
However,
as you have seen in my example above, cookies are
used on _every_ page to avoid hitting the database, thus
every
page would be hitting your session server. Everyone of those
page hits would have to talk to your session servers, and
thus
they'll need to handle a similar load as your webservers.
(b) Pages
would be slower. Webservers would need
to contact an
external server to retrieve session information. It's not
possible to store session caches on the webservers
themselves
because load-balancer server affinity does not work very
well.
The
"upload bandwidth" required for the clients to send
you the cookie cache data is largely unused on websites, so
it's "for free".
[Templates/Clearsilver fan] There are some
scenarios where session caching makes sense, but they are rare. Most websites
can easily avoid them and the performance problems they cause.
[Templates/Clearsilver fan] As an aside, these same huge websites all use
native C code for the most popular pages, because Perl, Python, and even Java,
are too slow when you are large. However, I admit that most sites don't need to
worry about this as they won't get this big.
Languages
In the end, the key is all
about a good design. Separating your
application code from the UI is a good practice to get into, even if you're not
doing web applications.
JSP:
http://java.sun.com/products/jsp/index.html
out.println() paradigm, without requiring the developer to write
this code. And it allows you to insert variables into the page that are
not interpreted until runtime.“What is Wrong With Java Server Pages”
http://php.weblogs.com/jsp
“JSP
technology -- friend or foe?”
http://www-106.ibm.com/developerworks/library/w-friend.html?dwzone=web
“Intro to JSP”
http://hotwired.lycos.com/webmonkey/01/22/index3a_page5.html?tw=programming
Servlets have made great
strides in the area of persistence. When a servlet is loaded into the server's
memory, it generally remains there as a very compact Java object. When a server
using servlets receives a request, there are no interpreters to spawn or
variables to instantiate (beyond the first time). This means servlets are efficient: They stand vigilantly like the
Marines, always prepared, ready to serve. Also, in addition to their
efficiency, servlets are very tightly integrated with the server, which enables
more sophisticated interactions with the server than is possible with CGI.
JSP brings
all of the power of the Java language to bear on the client/server interaction.
Portability, multithreading, extensive
class libraries, object-oriented code, strong safety features, robust security
measures, elegance, and extensibility are just a few of Java's advantages.
For further evidence, look at how the ColdFusion people have added the JRun
application server in their product to interact with JSP, servlets, and other
Java technologies.
Unlike
other scripting languages, servlets involve no platform-specific consideration
or modifications; they are Java
application components that are downloaded, on demand, to the part of the
system that needs them. Together, JSP technology and servlets provide an
attractive alternative to other types of dynamic web scripting/programming that
offers platform independence, enhanced
performance, and separation of logic from display, ease of administration,
extensibility into the enterprise and most importantly, ease of use.
Thanks to its foundation in Java and
its use of Java threads to handle incoming requests, JSP is a great deal more
efficient than many other scripting languages, such as CGI. Its threading model and error
handling also help prevent server hangs and crashes. And when the underlying
Java Virtual Machine (JVM) makes use of Just In Time
(JIT) compilation, the performance of well-written JSP can approach that of
C++.

“Has J2EE Hit a Fork In the Road?”
http://www.sdtimes.com/news/045/story1.htm
Recent decisions by major J2EE app server vendors to extend J2EE in
proprietary directions have made official what has been known for a long
time—the promise of Java is now dead.
Opinions about J2EE:
Benefits: lots of people use
it, lots of products available
Drawbacks: slow architecture
(especially J2EE), JSP is messy
Java Servlets:
http://www.webmacro.org/JavaServlets
The Java Servlet API is one of the APIs which form the
Java2EnterpriseEdition (J2EE). Servlets play a key part in almost any Java
application which has a web interface. The
role of servlets in a web application is to interact with the HTTP protocol,
handling requests from browsers, decoding any supplied data, performing any
required processing using the facilities provided by the rest of the web
application, and supplying the results back to the browser.
Opinions
about Servlets:
A
Java-based templating engine, like Velocity, is
usually implemented as a Java servlet.
More on Servlets:
However,
if misused the servlet API can actually increase the time required for
development versus a popular tool like PERL or PHP.
The correct approach is to spend a small effort generating something that is
just good enough to give the client a better idea. Then allow frequent changes
as the project moves forward. Your servlet design must accommodate this.
JavaBeans:
http://java.sun.com/products/javabeans/faq/faq.general.html#Q1
http://developer.java.sun.com/developer/onlineTraining/Beans/Beans1/bean-definition.html
JavaBeans is a portable, platform-independent component model written
in the Java programming language. JavaBeans components, or Beans, are reusable software components that can
be manipulated visually in a builder tool. Beans can be combined to create
traditional applications, or their smaller web-oriented brethren, applets. In
addition, applets can be designed to work as reusable Beans.
Having
artists and developers working on the same file wasn't ideal, and having Java
inside HTML proved almost as awkward as having HTML inside Java. It could
easily create a hard to read mess. So people matured in their use of JSP and
started to rely more on JavaBeans. Beans were written to contain business
logic code needed by the JSP page.
Individual Beans will function quite
differently, but typical unifying features that distinguish a Bean are:
·
Introspection: enables a builder tool to analyze how a Bean works
·
Customization: enables a developer to use an app builder tool to customize the
appearance and behavior of a Bean
·
Events: enables Beans to communicate and connect together
·
Properties: enable developers to customize and program with Beans
·
Persistence: enables developers to customize Beans in an app builder, and then
retrieve those Beans, with customized features intact, for future use.
JavaBeans are reusable software components written
in the Java programming language, designed to be manipulated visually by a
software development environment, like JBuilder or VisualAge for Java. They are similar to Microsoft's ActiveX
components, but designed to be platform-neutral; running anywhere there is a
Java Virtual Machine (JVM). http://jguru.com/faq/view.jsp?EID=13625
ColdFusion: http://www.cfm-resources.com/cf_asp_jsp.cfm
ColdFusion Studio tries to
automate the coding process as much as possible. Since it features automatic tag completion, expression
builders, tag choosers, and wizards, the amount of stuff you actually have
to key in is minimal. CF Studio gives you almost instant access to every
command, method, property, constant, function, and variable available. This
minimizes the amount of time you spend looking for information and maximizes
the time you spend using it.
Once you've got your
environment set up, you need a way to debug your code. CF Studio obliges with
an excellent built-in debugger. It
allows you to set breakpoints and watches and keeps track of the tag stack,
variables, recordsets, and CF output generated during
execution. With this information, you can step through an entire application
and locate errors with ease.
Perhaps my favorite thing about
ColdFusion is the way it makes use of custom
tags. Any ColdFusion page you create can be called as a custom tag.
PHP:
PHP isn't just a database-centric language.
It does so much more: dynamic graphics
generation, IMAP, SNMP, LDAP, XML — they're all there.
According
to PC Magazine, PHP is more mature than either ASP or JSP. PHP can interface with Sybase, Oracle,
Informix, Solid, Postgres, and even MSSQL.
PHP has been criticized for its lack of session handling. But this was overcome
previously by the use of the PHPLIB library, and now session support has been
built into PHP4. And ISAPI support, which had been lacking, is also now covered
by PHP4 (plus PHP contains a generalized server API library to make it easily
adaptable to other Web server APIs).
With PHP,
each source of page information is essentially treated equally in that each
becomes a normal variable within your script.
PHP is an established
server-side scripting language for creating dynamic Web pages. As a language
that has been designed expressly for the Web, it brings many features that
commercial entities are looking for:
“Advantages of PHP Over
Java”
http://zend.com/zend/art/php-over-java.php
JSP's performance also leaves much to be
desired: a recent survey in ZDnet's eWeek online publication found that PHP is as much
as 3.5 times faster than JSP. In the Web environment, this makes JSP a
significantly worse solution – because it can perform fewer transactions per
second, and features considerably slower response times.
Opinions about PHP:
> There are some articles comparing
JSPs and PHP and it says that PHP pages load 3.5 times faster.
Maybe for the first hit after you put the
web server up, but not after that. The
JSP becomes a compiled Java class and it is run out of byte-code (and then out
of machine code if your JIT is working well) while a PHP page is interpreted
each time. PHP will be slower in the
long run because of this, IMHO.
PHP is GREAT for small projects
If you are just hacking out two or three
pages, just use PHP. If two or three pages is going to turn into 20, and more
than one person is going to work on it, then a Model-View-Controller style
template system works best.
Python:
http://www.python.org/
Rocketmail, which became Yahoo Mail, was at one point entirely
written in Python.
eGroups.com, which became Yahoo Groups, was at one
point entirely written in Python.
Python is just as fast as, and easier to use than,
Perl, although it is certainly less popular.
http://www.bagley.org/~doug/shootout/craps.shtml
Python doesn't have anywhere
near the kind of following Java does.
.NET Framework:
“Microsoft .NET vs. J2EE: How Do They
Stack Up?” http://java.oreilly.com/news/farley_0800.html
·
C#, a
"new" language for writing classes and components, which integrates
elements of C, C++, and Java, and adds additional features, like metadata tags,
related to component development.
·
A
"common language runtime", which runs bytecodes
in an Internal Language (IL) format. Code and objects written in one language
can, ostensibly, be compiled into the IL runtime, once an IL compiler is
developed for the language.
·
JDEE
vs .NET: NET is a sign of a subtle but significant shift in Microsoft's
strategy to evangelize their platforms. They have been fighting alternative
frameworks and platforms at the management level pretty well, touting the usual
questionable "statistics" about cost of ownership and seamless
integration. Now they are fighting Java and open source initiatives on their
own terms, putting their own spin on "open" and attempting to
directly address the needs of developers, two things that they have been faulted
for not doing very well in the past.
Opinions about .NET:
This is not a framework we
want to consider. Server side Java is
leaps and bounds more mature than .NET at this point. Unless you absolutely need to move to .NET
for something (aka you're at an all MS shop with
developers who will only use MS products or you have existing ASP code you want
to use), I would steer clear of .NET until it proves itself.
What about XML?
http://www.w3.org/TR/1998/REC-xml-19980210
Most database servers are on a collision course
with XML. The move to XML is a natural tie-in with a new approach to computing—deploying database-enabled Web applications
running on application servers in place of standalone Windows programs. XML
is causing database vendors to rethink their direction from the ground up, and
the SQL language itself could well be on the way out in a few years,
potentially to be replaced by an XML-based language called XML Query,
now in development.
FREE
Opinions about
XML:
[Colleen] Just like you can use Velocity or Clearsilver
to output HTML or WAP, you can use them to output XML.
[Templating/Clearsilver fan] If you use XML at the start, you are going to use
XSLT (the XML transformation language) to convert it into HTML for your
website. However, you will ALSO use XSLT to transform it into different looking
XML data for another service such as voice... If you find it more convenient to
start with XML just so you can again transform and dump into a different XML,
then perhaps you would find using XML from the start easier. However, I
personally find XML/XSLT too complicated for my needs in templating.
[Templating/Clearsilver fan] In short,
Velocity and Clearsilver are much simpler and nicer,
and you can still output XML when you want to.
[Templating/Clearsilver fan] As an example,
using Clearsilver's cousin CS/HDF, we setup the Yahoo
Groups website simultaneously running WAP, the old eGroups.com UI in Japanese,
and the new Yahoo Groups UI in several languages... it ran like that for over
six months through many releases. I've heard people talk about stuff like that
in XML/XSLT, but I've never heard of anyone DO it.
[Colleen]
> If we plan to deploy content for various mediums
(browser, wap,
> voice?) isn't XML the best
choice? If we decide not to go that
route
> (XML) now, would it be difficult to implement
later on?
[JSP fan] I do not think XML is the best choice. IMO, a wise choice would be to write your
software with a good MVC design. Then
write different "views" tailored to the specific medium. Done right, your views will be very easy to
write. I don't think XML/XSLT really
lives up to its promise, here. And if
you go this route and want/need XML and XSLT in the future, XML is just another
view!
XSL:
http://www.w3.org/Style/XSL/
·
XSL is a language for expressing stylesheets.
An XSL style sheet is, like with CSS, a file that describes how to display an
XML document of a given type.
· It consists of three parts: XSL Transformations (XSLT): a language for transforming XML documents, the XML Path Language (XPath), an expression language used by XSLT to access or refer to parts of an XML document, and XSL Formattin