Catalyst-View-ByCode
view release on metacpan or search on metacpan
lib/Catalyst/View/ByCode.pm view on Meta::CPAN
# 3) create a simple template eg 'root/bycode/hello.pl'
# REMARK:
# use 'c' instead of '$c'
# prefer 'stash->{...}' to 'c->stash->{...}'
template {
html {
head {
title { stash->{title} };
load Js => 'site.js';
load Css => 'site.css';
};
body {
div header.noprint {
ul.topnav {
li { 'home' };
li { 'surprise' };
};
};
div content {
h1 { stash->{title} };
div { 'hello.pl is running!' };
img(src => '/static/images/catalyst_logo.png');
};
};
};
};
# 274 characters without white space
# 4) expect to get this HTML generated:
<html>
<head>
<title>Hello ByCode!</title>
<script src="http://localhost:3000/js/site.js"
type="text/javascript">
</script>
<link rel="stylesheet"
href="http://localhost:3000/css/site.css"
type="text/css" />
</head>
<body>
<div id="header" style="noprint">
<ul class="topnav">
<li>home</li>
<li>surprise</li>
</ul>
</div>
<div class="content">
<h1>Hello ByCode!</h1>
<div>hello.pl is running!</div>
<img src="/static/images/catalyst_logo.png" />
</div>
</body>
</html>
# 453 characters without white space
=head1 DESCRIPTION
C<Catalyst::View::ByCode> tries to offer an efficient, fast and robust
solution for generating HTML and XHTML markup using standard perl code
encapsulating all nesting into code blocks.
Instead of typing opening and closing HTML-Tags we simply call a
sub named like the tag we want to generate:
div { 'hello' }
generates:
<div>hello</div>
There is no templating language you will have to learn, no quirks with
different syntax rules your editor might not correctly follow and no
indentation problems.
The whole markup is initially constructed as a huge tree-like
structure in memory keeping every reference as long as possible to
allow greatest flexibility and enable deferred construction of every
building block until the markup is actially requested.
Every part of the markup can use almost every type of data with some
reasonable behavior during markup generation.
=head2 Tags
Every tag known in HTML (or defined in L<HTML::Tagset> to be precise) gets
exported to a template's namespace during its compilation and can be used as
expected. However, there are some exceptions which would collide with CORE
subs or operators
=over 12
=item choice
generates a E<lt>selectE<gt> tag
=item link_tag
generates a E<lt>linkE<gt> tag
=item trow
generates a E<lt>trE<gt> tag
=item tcol
generates a E<lt>tdE<gt> tag
=item subscript
generates a E<lt>subE<gt> tag
=item superscript
generates a E<lt>supE<gt> tag
=item meta_tag
generates a E<lt>metaE<gt> tag
lib/Catalyst/View/ByCode.pm view on Meta::CPAN
li {
id $id if ($id);
a(href => $href || 'http://foo.bar') {
block_content;
};
};
};
# use the block like a tag
navitem some_id (href => 'http://bar.baz') {
# this gets rendered by block_content() -- see above
'some text or other content';
}
# will generate:
<li id="some_id">
<a href="http://bar.baz">some text or other content</a>
</li>
=item block_content
a simple shortcut to render the content of the block at a given point. See
example above.
=item c
holds the content of the C<$c> variable. Simple write C<<< c->some_method >>>
instead of C<<< $c->some_method >>>.
=item class
provides a shortcut for defining class names. All examples below will generate
the same markup:
div { class 'class_name'; };
div { attr class => 'class_name'; };
div { attr('class', 'class_name'); };
div.class_name {};
Using the C<class()> subroutine allows to prefix a class name with a C<+> or
C<-> sign. Every class name written after a C<+> sign will get appended to the
class, each name written after a C<-> sign will be erased from the class.
=item doctype
a very simple way to generate a DOCTYPE declatation. Without any arguments, a
HTML 5 doctype declaration will be generated. The arguments (if any) will
consist of either of the words C<html> or C<xhtml> optionally followed by one
or more version digits. The doctypes used are taken from
L<http://hsivonen.iki.fi/doctype/>.
some examples:
doctype; # HTML 5
doctype 'html'; # HTML 5
doctype html => 4; # HTML 4.01
doctype 'html 4'; # HTML 4.01
doctype 'html 4s'; # HTML 4.01 strict
doctype 'html 4strict'; # HTML 4.01 strict
doctype 'xhtml'; # XHTML 1.0
doctype 'xhtml 1 1'; # XHTML 1.1
=item id
provides a shortcut for defining id names. All examples here will generate the
same markup:
div { id 'id_name'; };
div { attr id => 'id_name'; };
div { attr('id', 'id_name'); };
div id_name {};
=item load
an easy way to include assets into a page. Assets currently are JavaScript or
CSS. The first argument to this sub specifies the kind of asset, the second
argument is the URI to load the asset from.
Some examples will clearify:
load js => '/static/js/jquery.js';
load css => '/static/css/site.css';
If you plan to develop your JavaScript or CSS files as multiple files and like
to combine them at request-time (with caching of course...), you might like to
use L<Catalyst::Controller::Combine>. If your controllers are named C<Js> and
C<Css>, this will work as well:
load Js => 'name_of_combined.js';
=item on
provides a syntactic sugar for generating inline JavaScript handlers.
a(href => '#') {
on click => q{alert('you clicked me'); return false};
};
=item params
generates a series of C<param> tags.
applet ( ... ) {
params(
quality => 'foo',
speed => 'slow',
);
};
=item stash
is a shortcut for C<<< c->stash >>>.
=item template
essentially generates a sub named C<RUN> as the main starting point of every
template file. Both constructs will be identical:
sub RUN {
div { ... };
}
( run in 1.167 second using v1.01-cache-2.11-cpan-119454b85a5 )