AxKit::XSP::ObjectTaglib::Demo - A demo taglib using ObjectTaglib
#httpd.conf AxAddXSPTaglib AxKit::XSP::ObjectTaglib::Demo #XSP page <?xml version="1.0" encoding="UTF-8"?> <xsp:page xmlns:xsp="http://apache.org/xsp/core/v1" xmlns:demo="http://today.icantfocus.com/CPAN/AxKit/XSP/ObjectTaglib/Demo" > <body> <demo:courses> <course> <name><demo:name/></name> <code><demo:code/></code> <summary><demo:summary/></summary> <description><demo:description/></description> <demo:prerequisites> <prerequisite> <name><demo:name/></name> <code><demo:code/></code> </prerequisite> </demo:prerequisites> <demo:presentations> <presentation> <size><demo:size/></size> </presentation> </demo:presentations> <demo:resources> <resource><demo:name/></resource> </demo:resources> </course> </demo:courses> </body> </xsp:page>
This taglib demonstrates how to use the ObjectTaglib to map XSP tags to a set of object oriented classes based on the original examples in Apache::AxKit::Language::XSP::ObjectTaglib.
When the topmost <demo:courses> start tag is encountered, the start_course method is called. The most common implementations would probably use the start/end methods to load and map the topmost tags to the root objects being used. After that, the relationships in @specification will be used to generate the necessary code.
start_course
@specification
Let's break it down. First, we declare the sub and catch the XSP SAX model, the tag, and the attributes passed from within the <demo:courses> tag:
sub start_courses { my ($e, $tag, %attr) = @_;
Next, let's turn off the XSP text output managing for a moment. I don't know why, but it had to be done to work. :-)
$e->manage_text(0);
Now we'll create a new variable containing the code to insert into the XSP innards. First we'll load the Courses module:
my $out = ' use AxKit::XSP::ObjectTaglib::Demo::Courses;
Now we'll create an array and load all of the Course objects into it:
my @_xsp_axkit_xsp_objecttaglib_demo_courses = AxKit::XSP::ObjectTaglib::Demo::Courses->load;
Take a closer look at the variable name: @_xsp_axkit_xsp_objecttaglib_demo_courses. ObjectTaglib expects certain variable names when it builds it's code for looping and calls methods on objects using the method and objects names specified in @specification. axkit_xsp_objecttaglib_demo is a safe version of the current taglib name AxKit::XSP::ObjectTaglib::Demo. courses is the root courses object declared within the @specification as:
@_xsp_axkit_xsp_objecttaglib_demo_courses
axkit_xsp_objecttaglib_demo
AxKit::XSP::ObjectTaglib::Demo
courses
tag => 'courses', type => 'special', start => \&start_courses, end => \&end_courses
If we declared a count tag with a target of courses
count
target
tag => 'count', target => 'courses'
target is appended to the base variable name, and it's method count is called. That looks something like this $_xsp_axkit_xsp_objecttaglib_demo_courses->count.
$_xsp_axkit_xsp_objecttaglib_demo_courses->count
But I digress, back to our program. Next, we setup a looping mechanism wrapped around all of our other tags:
for my $_xsp_axkit_xsp_objecttaglib_demo_course (@_xsp_axkit_xsp_objecttaglib_demo_courses) { ';
We left of the second half of that loop closure. We'll add that when end_courses is called after everything else is processed.
end_courses
return $out; };
When </demo:courses> is encountered, end_courses is called. There isn't anything too exciting here. Like before, we declare the sub and catch the tag info:
sub end_courses { my ($e, $tag, %attr) = @_;
Again, we'll turn of text management just because.
Lastly, we'll write out the main courses loop closure and return it.
my $out = ' }; '; return $out; };
Apache::AxKit::Language::XSP::ObjectTaglib
Christopher H. Laco CPAN ID: CLACO claco@chrislaco.com http://today.icantfocus.com/blog/
To install Apache::AxKit::Language::XSP::ObjectTaglib, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Apache::AxKit::Language::XSP::ObjectTaglib
CPAN shell
perl -MCPAN -e shell install Apache::AxKit::Language::XSP::ObjectTaglib
For more information on module installation, please visit the detailed CPAN module installation guide.