There are three main reasons for this suggestion: People in this situation will almost certainly be using the system Perl anyway. And the system Perl will already have pre-packaged modules installed alongside it.
And installing modules using cpanminus alongside pre-packaged modules in the same library installation is a recipe for disaster. One of the most common requests I see is for GD or one of its related modules. Your package manager will know about the underlying requirement for libgd.
The user is more likely to be used to using their package manager. Teaching them about the CPAN ecosystem can come later. What do you think? Are there obvious errors in my thinking? Share this: Tweet. Email Print. View all of Dave Cross's posts. Most templates use a hash object of data to access values while rendering.
You can specify this data in a file or with a hash reference. This method allows you to specify the directories that are searched to find templates. You can specify this as a string containing a single directory, an array ref of strings containing directory names, or as a string containing multiple directories separated by ':'. The process method is used to actually process a template. The error method returns the error message on a failure.
To install Template::Toolkit::Simple, copy and paste the appropriate command in to your terminal. For more information on module installation, please visit the detailed CPAN module installation guide. Takes no parameters. It does the exact same thing. Everything else is fixed text that passes through the processor untouched.
A template processor takes the template, together with a list of the variable data to be included in the letter, and produces a finished letter. The Template Toolkit provides tpage for doing just that from the command line. Pass the name of the template file to tpage as a command-line option, along with any number of --define options to provide values for variables. If the preceding template is stored in the destruction.
The output this generates is shown in Example Process the same template a few thousand times with different sets of data and you have the entire basis of the junk-mail industry. Or a Vogon Constructor Fleet. This book is a good example of a more complex template. They all have similar sets of front matter title page, publication information, table of contents, and preface , followed by the actual chapters, some optional appendices, an index, and finally the colophon.
Templates that define the look of all of these parts are defined in the publication system, and the data for a particular book is formatted to conform to those rules. If someone decides to change the font used for the chapter titles in forthcoming books, he need only change the setting in the template definition.
Another way to look at a template processor is as a tool for separating processing from presentation. For example, a company sales report is probably created from data stored in a database.
One way to create the report would be to extract the required data into a spreadsheet and then do calculations on the data to produce the information required. The spreadsheet could then be printed out or emailed to the required recipients.
Although you can use templates to generate any kind of text document, the most common use is to generate HTML pages for web content. The whole genre of template processing systems has matured rapidly in less than a decade, particularly within the Perl community, in response to the demands of people struggling to build and maintain ever more complex content and applications for their web sites.
Templates help in a number of ways. The most obvious benefit is that they can be used to apply a consistent look and feel to all the pages in a web site to achieve a common branding. You can use a template to add standard headers, footers, menus, and other user interface components as easily as the Hyperspace Planning Council ruthlessly adds a few lines of Vogon poetry to every planet destruction order, just to rub salt into the wounds.
This is just the tip of the iceberg. In addition to the use of variables, the Template Toolkit provides a number of other directives that instruct it to perform more complex processing actions, such as including another template, repeating a section of markup for different pieces of data, or choosing a section to process based on a particular condition. Example illustrates some of these directives in action. We explain the purpose of these directives later in this chapter, and show examples of the different ways they can be used throughout the rest of the book.
For now, you can probably work out what they do from their names. The Template Toolkit is just one example of a template processor.
The presentation language that it provides is intentionally simple, regular, and easy to understand and use. This makes it simple for web designers and other nonprogrammers to use it without first having to get to grips with Perl. The Template Toolkit provides language features and off-the-shelf plugin modules that allow you to perform many common tasks, including CGI programming, manipulating XML files, and accessing SQL databases.
It allows you to concentrate on one thing at a time, be it the HTML presentation or the Perl application, without having the other aspects in your face and under your feet.
It makes both your HTML templates and Perl code more portable and reusable, and easier to read, write, and maintain. At least half a dozen mature and respected templating systems are available for Perl. The best-known and best-supported template processors include the following:.
Text::Template is a library for generating form letters, building HTML pages, or filling in templates generally. A template is a piece of text that has little Perl programs embedded in it here and there. When you fill in a template, you evaluate the little programs and replace them with their values. If you want a variable interpolated, you write it the way you would in Perl.
If you need to make a loop, you can use any of the Perl loop constructions. All the Perl built-in functions are available. It extends standard HTML with a few HTML-like tags, and enforces the divide between design and programming by restricting what a template is capable of doing. By limiting the programmer to using just simple variables and loops in the HTML, the template remains accessible to designers and other non-Perl people.
The use of HTML-like syntax goes further to make the format understandable to others. Mason allows web pages and sites to be constructed from shared, reusable building blocks called components. Components contain a mix of Perl and HTML, and can call each other and pass values back and forth like subroutines. Components increase modularity and eliminate repetitive work: common design elements headers, footers, menus, logos can be extracted into their own components where they need be changed only once to affect the whole site.
Embperl gives you the power to embed Perl code in your HTML documents, and the ability to build your web site out of small reusable objects in an object-oriented style. You can also take advantage of all the usual Perl modules including DBI for database access , use their functionality, and easily include their output in your web pages.
Apache::ASP provides an Active Server Pages port to the Apache web server with Perl scripting only, and enables development of dynamic web applications with session management and embedded Perl code.
The Template Toolkit attempts to offer the best features of these modules, including separation of Perl from templates and applicability beyond HTML. The Template Toolkit is a generic template processing system that will process any kind of document for use in any environment or application.
Many other template systems were designed specifically to create HTML pages for web content. In some cases, that is all the system can be used for. In others, it is possible with varying degrees of difficulty to use the system in a non-web environment.
The Template Toolkit was originally designed to help Andy create his web site, but he was careful to ensure that it was just as usable outside of that environment. It is equally at home creating any other kind of data. Some modules for example, HTML::Template enforce a very strict interpretation of template processing that intentionally limits what can be done in a template to accessing variables and using simple conditional or looping constructs.
The Template Toolkit gives you the best of both worlds. It has a powerful data engine the Stash that does all the hard work of mapping complex data structures from your Perl code, configuration files, SQL databases, XML files, and so on, into template variables that are accessed by a simple and uniform dotted notation e. You can use this to keep your templates simple without limiting the complexity or functionality of the systems that put data into the templates.
At the opposite end of the spectrum, the Template Toolkit also allows you to embed Perl code directly in your templates. We look at how to set options later in this chapter.
Template Toolkit also lets you work between the two extremes. It provides a rich set of language features directives that allow you to add complex functionality to your templates without requiring you to embed Perl code. It also has a powerful plugin mechanism that allows you to load and use Perl modules to extend the functionality in any way you can imagine. Sometimes you want to build a complex and highly structured system to run a web site. Other times you just want to roll up a quick all-in-one template to generate a report from a database.
The Template Toolkit encourages whatever approach is most appropriate to the task at hand. The dotted notation makes accessing variables far less daunting than in Perl. For example:. This hides the underlying implementation details from the template designer. The Perl code requires a different syntax but the Template Toolkit code stays the same.
This lets you change the underlying implementation at any time without having to change the templates. As long as the data is laid out in the same way i. This uniform syntax also means that your template designers can remain blissfully ignorant of the difference between a hash array and an object. The general rule is to use Perl for programming and the Template Toolkit for presentation.
The Template Toolkit is designed to be easy to extend. The object-oriented architecture of the Template Toolkit makes this process relatively straightforward, and there are programming hooks throughout the system to give you as much flexibility as possible.
A number of plugins exist for the Template Toolkit, and we cover them in Chapter 6. At any one time you can download from the Web at least two possible versions of the Template Toolkit: a stable version and a developer version. The stable version has a version number such as 2. The developer versions have version numbers such as 2. Generally, you should install the latest stable release. In addition, a web site is dedicated to the Template Toolkit.
You can also get developer versions of the Template Toolkit from the web site. If you want to add functionality to the Template Toolkit or have found a bug that you can fix, and you want your patch to be accepted by Template Toolkit developers, you should make your changes against the current CVS HEAD.
Installing the Template Toolkit is like installing any other Perl module see perlmodinstall 1 for platform-specific details. The basic idea is as follows:. A few optional modules and pages of documentation come with the Template Toolkit, and how much of that gets installed is controlled by arguments to perl Makefile.
Run perl Makefile. Over 2, tests are provided with the Template Toolkit to ensure that everything works as expected, and to let you know about any problems that you might have. It takes no more than a minute or so to run the tests, and they can save you a great deal of debugging time in the unlikely event that something is wrong with your installation. More often than not, errors raised in the test suite come from plugin modules whose external Perl modules are not installed on your system or are the wrong version.
The Template::Tutorial is also available as an introductory guide to using the Template Toolkit. The new constructor method implemented by the Template::Base base class instantiates a new Template object. A reference to a hash array of configuration items may be passed as a parameter. A reference to a new Template object is returned, or undef on error. For convenience, configuration items may also be specified as a list of items instead of a hash array reference. These are automatically folded into a hash array by the constructor.
The process method is called to process a template. The first parameter indicates the input template as one of:. A reference to a hash array may be passed as the second parameter, containing definitions of template variables. The process method then returns 1 to indicate success. A third parameter may be passed to the process method to specify a different output location.
This value may be one of:. IO::Handle , Apache::Request , etc. After the optional third output argument can come an optional reference to a hash or a list of name, value pairs providing further options for the output.
0コメント