An Introduction to Application Development with Catalyst and Perl
Template::Toolkit Views
One of the most common needs of a Web application is a templating system for rendering content. Templates probably are the best all-around approach to rendering text-based content, especially for markup like HTML.
Catalyst can use multiple Perl template systems, but the most popular is Template::Toolkit—a powerful, general-purpose template-processing system that is fast and feature-rich. It has a versatile and robust syntax that is simple and easy to use, but it also supports advanced capabilities, such as control structures, stream processing and extendability. Template::Toolkit is a whole programming language in its own right.
Catalyst provides a drop-in interface to Template::Toolkit via the view/component class Catalyst::View::TT. You can create a view within your application that extends this class using the helper script. Run this to create a new view named "HTML":
script/killerapp_create.pl view HTML TT
The new view is fully functional out of the box. As a general-purpose wrapper around Template::Toolkit, it provides a simple API to select templates and supply input data. The rest of the view-specific code goes in the templates themselves, which are stored within "root" in your application directory.
Here is an example of a simple Template::Toolkit template to render an HTML page:
<html><head>
<h3>[% title %]</h3>
</head><body>
<h1>Message: [% message %]</h1>
</body>
</html>
The character sequences within [% %] are "directives"—snippets of code
that get replaced when the template is processed. The directives above are
simple variable substitutions, which are the most basic kind. In this case,
the values supplied for title and
message will be inserted when the
template is rendered.
If you saved the above template in a file named main.tt within root/templates, for example, you could use it with an action like this:
sub myaction :Path('/some/place') :Args(0) {
my ( $self, $c ) = @_;
$c->stash->{template} = 'templates/main.tt';
$c->stash->{data}->{title} = 'TT rendered page';
$c->stash->{data}->{message} = 'A cool message!';
$c->forward( $c->view('HTML') );
}
The stash object above is another built-in feature of Catalyst that I
haven't covered so far. It isn't very complicated; it's simply a hashref
within the context object. It provides a standard per-request place to
share data across components, similar to request and response, but for
general use.
Catalyst::View::TT-based views use the content of the stash to determine
operation. The value of template identifies the
template to call, and the
stash as a whole is used as the input data—each key in the stash becomes a
variable in the template. The content generated from processing the
template is used to set the body of the response.
The data in a real application probably will be more complex than the simple key/values in the previous example. One of Template::Toolkit's great features is its ability to handle Perl data structures directly. Consider the following action:
sub myaction :Path('/some/place') :Args(0) {
my ( $self, $c ) = @_;
$c->stash->{template} = 'templates/main.tt';
$c->stash->{data} = {
title => 'TT rendered page',
subhash => {
alpha => 'foo',
bravo => 'abdc',
charlie => 'xyz'
},
thinglist => [
'Thing 1',
'Thing 2',
'Big thing',
'Small thing'
]
};
$c->forward( $c->view('HTML') );
}
This would work with a template like this:
<html>
<h3>[% data.title %]</h3>
</head><body>
<b>Alpha:</b> [% data.subhash.alpha %]<br>
<b>Bravo:</b> [% data.subhash.bravo %]<br>
<b>Charlie:</b> [% data.subhash.charlie %]<br>
<br>
<b>List of Things:</b><br>
[% FOREACH item IN data.thinglist %]
[% item %]<br>
[% END %]
</body>
</html>
Objects also can be supplied and accessed in the same manner as hashes. In
fact, the context object is supplied automatically in "c". For example, if
you want to display the client's IP address, instead of separately putting
$c->request->address in the stash, you can just access it directly within
the template like this:
[% c.request.address %]
Template::Toolkit has many more features and abilities, including wrappers, conditional statements, filters, function calls and so on. Catalyst::View::TT also has additional defaults and configuration options that I didn't cover here (see the documentation for more details).
It is totally up to you how to balance logic between the templates and the rest of your application. Depending on what you are trying to achieve, your application easily could be written more in Template::Toolkit than in Perl!
DBIx::Class Models
One of the other most common needs of an application is a database. DBIx::Class (often shortened to DBIC) has emerged as the most popular ORM (Object Relational Mapper) library available for Perl. It is an exceptionally powerful, robust, object-oriented interface to many relational database servers (including MySQL, PostgreSQL, Oracle, MSSQL and many others).
Like Template::Toolkit, but to an even greater degree, Catalyst provides refined, drop-in component wrappers to interface with DBIx::Class (Catalyst::Model::DBIC::Schema).
Using DBIx::Class is a whole topic in and of itself that I don't have space to cover here, but it is a must-have if you plan to integrate your application with a database. See Resources for information on where to go to start learning about this wonderful library.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
Web Development News
Developer Poll
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- A Topic for Discussion - Open Source Feature-Richness?
- What's the tweeting protocol?
- Dart: a New Web Programming Experience
- Developer Poll
- Trying to Tame the Tablet
- Reply to comment | Linux Journal
2 hours 25 min ago - Reply to comment | Linux Journal
4 hours 58 min ago - Reply to comment | Linux Journal
6 hours 15 min ago - great post
6 hours 50 min ago - Google Docs
7 hours 12 min ago - Reply to comment | Linux Journal
12 hours 1 min ago - Reply to comment | Linux Journal
12 hours 48 min ago - Web Hosting IQ
14 hours 21 min ago - Thanks for taking the time to
15 hours 58 min ago - Linux is good
17 hours 56 min ago







Comments
Error in template?
In page 3 of this article:
$c->stash->{data}->{title} = 'TT rendered page';
Shouldn't the template have this:
[% data.title %]
Instead of what is in the article:
[% title %]
This was an effective review for me, thank you!
[% data.message %] too
I should have also mentioned that given:
$c->stash->{data}->{message} = 'A cool message!';
The following in the template:
[% message %]
needs to be:
[% data.message %]
Good work
I like post a complete catalyst tutorial, i want to know if reference for it article, tks
Really nice article! Catalyst
Really nice article! Catalyst is great framework with great community behind it
typo
In this text:
The alias for
/static/above tells Apache to serve the files directly in this directory (images, CSS, JavaScript files and so on). This is more efficient than serving these files through the application, which isn't necessary.The <code> and </code> HTML tags surrounding /static/ somehow got through.
Good Job!
Nice work! I hope you'll continue with another MVC framework from Perl, a more lighter one, Perl Dancer.
Daniel
Excellent!
With so many people still thinking of perl as "that legacy language that you run from the cgi-bin directory", it's good to see the modern stuff get some coverage. Nice one!
Good work, looking for more!
Only LinuxJournal could have come up with such article. Good work!
Try to post a complete catalyst tutorial, there's only one single tutorial available (CPAN's tutorial for catalyst, the best one).