[Maypole] list macro - possible improvement
Carl Hayter
hayter@usc.edu
Tue, 22 Jun 2004 17:03:58 -0700
--bpVaumkpfGNUagdU
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
On Wed, Jun 23, 2004 at 12:30:48AM +0100, Aaron Trevena wrote:
>=20
> in case you want to link to other mod_perl handlers or hardcoded links
> (like adding a homepage to the navbar)
>=20
> [%
> MACRO link(table, command, additional, label) BLOCK;
> '<A HREF=3D"' _ base; table _ "/" IF table; command _ "/" IF command=
; additional _ '">';
> label;
> "</A>";
> END;
> %]
>=20
> Adding the conditional concatonation means you won't get
> 'file error - /: absolute paths are not allowed (set ABSOLUTE option)'
> messages anymore when passing empty values to the link macro
>=20
> Might not be that useful but when you use url mapping tis nice to handle
> non-maypole links in a similar way.
a bit more evil... i dislike trailing slashes on my uri_base and some
of the templates also use the [%base%]/[%table%]/[%action%] idiom
leaving multiple slashes in the urls.
so i made uri_base without the trailing slash and perlified
the macros a bit to join with '/'. also moved the label
to the front and made the rest of the arguments an array,
i couldn't figure out how to do ($label,@args) =3D @_ in
Template Toolkit and settled on constructing the array with []
[% link( 'edit', [ table, 'edit', obj.id ] ) %]
i'd like to make that just ('edit',table,'edit',obj.id) if anybody
knows how...
[%# table, command, additional, label -> label, args -%]
[% MACRO link(label, args) BLOCK;
path =3D [ base ];
CALL path.splice( path.size, 0, args ) IF args.size;
'<a href=3D"'; path.join('/'); '">'; label; '</a>';
END; -%]
[% DEFAULT base =3D 'http://here/foo' -%]
[% link('change this thing', ['thing', 'edit', 'thing.id']) %]
[% link('add a thing', ['thing', 'create']) %]
[% link('default things', ['thing']) %]
[% link('home',[]) %]
[% MACRO maybe_link_view(object) BLOCK;
IF object.table;
link(object, [ object.table, "view", object.id ]);
ELSE;
object;
END;
END;
%]
[% obj =3D { table =3D> 'thing' id =3D> 1 }; maybe_link_view(obj); %]
[% obj =3D "not"; maybe_link_view(obj); %]
[% MACRO button(obj, action) BLOCK;
path =3D [ base, obj.table, action, obj.id ];
%]
<td>
<form method=3D"post" action=3D"[%path.join('/')%]">
<input type=3D"submit" name=3D"[%action%]" value=3D"[%action%]">
</form>
</td>
[% END %]
[% obj =3D { table =3D> 'thing' id =3D> 1 }; button(obj, "doit"); %]
[%# XXX templates using these macros: list navbar pager %]
not thoroughly tested, i'm still using an uglier version in
production.
and it might be usefull to make a macro just to make urls.
[% MACRO url(args) BLOCK;
path =3D [ base ];
CALL path.splice( path.size, 0, args ) IF args.size;
path.join('/');
END; %]
<a href=3D"[%url(request.path)%]">blah blah</a>
----
Carl
off to get v1.5 woot!
--bpVaumkpfGNUagdU
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (SunOS)
iD8DBQFA2MjtjSUgp1gR7V8RAh49AJ9sY57d4V1ZRlSaaWNOA6/AjncsQwCeKUL5
6gAlCx50u4LHav05UACqjoc=
=G336
-----END PGP SIGNATURE-----
--bpVaumkpfGNUagdU--