[Maypole] Re: [Maypole-dev] [PATCH] Split the guts of the view template into a macro

David Baird dave@riverside-cms.co.uk
Thu, 09 Dec 2004 01:24:56 +0000


Dagfinn Ilmari Manns=E5ker wrote:
> It's me again :)
>=20
> I needed to replace just the related part of the view template for some
> of my tables, so instead of duplicating most of it, I split the guts of=
f
> into a view_item macro.

I've applied this (and the button patch) to the MasonX::Maypole templates.

d.

>=20
> Index: templates/factory/macros
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> --- templates/factory/macros	(revision 304)
> +++ templates/factory/macros	(working copy)
> @@ -117,3 +117,57 @@
>      END;
>  END;
>  %]
> +[%#
> +
> +=3Dhead2 view_item
> +
> +This takes an object and and displays its properties in a table.=20
> +
> +=3Dcut
> +
> +#%]
> +[% MACRO view_item(item) BLOCK; %]
> +    [% SET string =3D item.stringify_column %]
> +    <div id=3D"title"> [% item.$string %]</div>
> +    [% INCLUDE navbar %]
> +    <table class=3D"view">
> +        <tr>
> +            <td class=3D"field">[% classmetadata.colnames.$string %]</=
td>
> +            <td>[% item.$string %]</td>
> +        </tr>
> +        [% FOR col =3D classmetadata.columns.list;
> +            NEXT IF col =3D=3D "id" OR col =3D=3D string;
> +            NEXT UNLESS item.$col;
> +        %]
> +[%#=20
> +
> +=3Dfor doc
> +
> +It gets the displayable form of a column's name from the hash returned
> +from the C<column_names> method:
> +
> +#%]
> +            <tr>
> +                <td class=3D"field">[% classmetadata.colnames.$col; %]=
</td>
> +                <td>
> +                    [% IF col =3D=3D "url" && item.url;  # Possibly to=
o much magic.
> +                        '<a href=3D"'; item.url; '"> '; item.url; '</a=
>';
> +                    ELSE;
> +                        maybe_link_view(item.$col);=20
> +                    END; %]
> +[%#
> +
> +This tests whether or not the returned value is an object, and if so,
> +creates a link to a page viewing that object; if not, it just displays
> +the text as normal. The object is linked using its stringified name;
> +by default this calls the C<name> method, or returns the object's ID
> +if there is no C<name> method or other stringification method defined.
> +
> +=3Dcut
> +
> +#%]=20
> +                </td>
> +            </tr>
> +        [% END; %]
> +    </table>
> +[% END %]
> Index: templates/factory/view
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> --- templates/factory/view	(revision 303)
> +++ templates/factory/view	(working copy)
> @@ -11,51 +11,9 @@
>  [% PROCESS macros %]
>  [% INCLUDE header %]
>  [% FOR item =3D objects %]
> -    [% SET string =3D item.stringify_column %]
> -    <div id=3D"title"> [% item.$string %]</div>
> -    [% INCLUDE navbar %]
> -    <table class=3D"view">
> -        <tr>
> -            <td class=3D"field">[% classmetadata.colnames.$string %]</=
td>
> -            <td>[% item.$string %]</td>
> -        </tr>
> -        [% FOR col =3D classmetadata.columns.list;
> -            NEXT IF col =3D=3D "id" OR col =3D=3D string;
> -            NEXT UNLESS item.$col;
> -        %]
> -[%#=20
> -
> -=3Dfor doc
> -
> -It gets the displayable form of a column's name from the hash returned
> -from the C<column_names> method:
> -
> -#%]
> -            <tr>
> -                <td class=3D"field">[% classmetadata.colnames.$col; %]=
</td>
> -                <td>
> -                    [% IF col =3D=3D "url" && item.url;  # Possibly to=
o much magic.
> -                        '<a href=3D"'; item.url; '"> '; item.url; '</a=
>';
> -                    ELSE;
> -                        maybe_link_view(item.$col);=20
> -                    END; %]
> +[% view_item(item); %]
>  [%#
> =20
> -This tests whether or not the returned value is an object, and if so,
> -creates a link to a page viewing that object; if not, it just displays
> -the text as normal. The object is linked using its stringified name;
> -by default this calls the C<name> method, or returns the object's ID
> -if there is no C<name> method or other stringification method defined.
> -
> -=3Dcut
> -
> -#%]=20
> -                </td>
> -            </tr>
> -        [% END; %]
> -    </table>
> -[%#
> -
>  =3Dfor doc
> =20
>  The C<view> template also displays a list of other objects related to =
the first
>=20
>=20