On Skinning: default/prefix/suffix as filters

2 views
Skip to first unread message

Andreas Bolka

unread,
Sep 5, 2008, 5:31:29 PM9/5/08
to Helma NG
Fellow Helmatics!

I propose that Helma NG's macro filters make the legacy
default/prefix/suffix macro parameters redundant.

Helma 1 (as well as current Helma NG) allows a macro to take
default/prefix/suffix parameters (see [1] for more information):

<% foo default="bar" %>
<% baz prefix="qux" suffix="xuq" %>

We can easily achieve the same effect with macro filters [2] as follows:

<% foo | default "bar" %>
<% baz | prefix "qux" | suffix "xuq" %>

One effect resulting from the sequential application of filters is worth
noting: in Helma 1 it does not matter in which order the default,
prefix and suffix parameters are provided. So, assuming that `foo_macro`
returns an empty string:

<% foo prefix="-" default="0" suffix="+" %>
==> -0+

Whereas the naive translation to filters will expand as:

<% foo | prefix "-" | default "0" | suffix "+" %>
==> 0+

`prefix_filter` and `suffix_filter` prepend/append their parameter only
if the input string is not empty (or null, or undefined). So in the
latter filter chain, `prefix_filter` gets an empty input and does
therefore not prepend the "-". Then `default_filter` gets passed the
still empty string and returns its default string. This is finally
passed to `suffix_filter` as input, and as it is not empty, the
suffix is appended.

To achieve the same effect as in Helma 1, properly ordering the filters
is necessary:

<% foo | default "0" | prefix "-" | suffix "+" %> is
==> -0+

However, I think this is a somewhat degenerate case, as once you have a
default you can directly write the prefix/suffix strings as in:

-<% foo | default "0" %>+

The only case where this would not work, is when the default string is
itself the result of macro expansion (e.g. <% foo | default <%bar%> %>)
-- then you simply have to know that filters are applied sequentially (a
rather intuitive concept, in my opinion).

To conclude, I suggest dropping the special-case default/prefix/suffix
macro parameters in Helma NG and providing corresponding filters
instead. Opinions?

I've pushed a commit to implement this to the "skins" branch in my
helma-ng github repos [3], and also attached a patch for your
convenience.

[1] http://helma.org/docs/guide/framework/#macroattributes
[2] http://dev.helma.org/wiki/New+Skin+Features+in+Helma+1.6/#Macrofilters
[3] http://github.com/earl/helma-ng/commit/ef1116

--
Regards,
Andreas

drop-special-macro-parameters.patch

Hannes Wallnoefer

unread,
Sep 6, 2008, 5:25:58 AM9/6/08
to Helma NG
On Sep 5, 11:31 pm, Andreas Bolka <andreas.bo...@gmx.net> wrote:
> Fellow Helmatics!
>
> I propose that Helma NG's macro filters make the legacy
> default/prefix/suffix macro parameters redundant.

Replacing special behaviour with something more general and open is a
good idea, I think. Unless it makes the resulting code much uglier or
less readable, which isn't the case here.
Actually, in Helma 1 prefix and suffix are not applied to the default
value, so for the same result you'd have to use <% foo | prefix "-" |
suffix "+" | default "0" %>, which would render "0".

> However, I think this is a somewhat degenerate case, as once you have a
> default you can directly write the prefix/suffix strings as in:
>
>     -<% foo | default "0" %>+
>
> The only case where this would not work, is when the default string is
> itself the result of macro expansion (e.g. <% foo | default <%bar%> %>)
> -- then you simply have to know that filters are applied sequentially (a
> rather intuitive concept, in my opinion).
>
> To conclude, I suggest dropping the special-case default/prefix/suffix
> macro parameters in Helma NG and providing corresponding filters
> instead. Opinions?

I generally agree, as the additional pipe characters are not really a
problem, and it makes sense to use the filter/pipe feature whenever we
can.

What about an additional filter names "wrap" that combines prefix and
suffix?

<% code | wrap "<pre>" "</pre>" %>

Hannes

> I've pushed a commit to implement this to the "skins" branch in my
> helma-ng github repos [3], and also attached a patch for your
> convenience.
>
> [1]http://helma.org/docs/guide/framework/#macroattributes
> [2]http://dev.helma.org/wiki/New+Skin+Features+in+Helma+1.6/#Macrofilters
> [3]http://github.com/earl/helma-ng/commit/ef1116
>
> --
> Regards,
> Andreas
>
>  drop-special-macro-parameters.patch
> 3KViewDownload

Andreas Bolka

unread,
Sep 6, 2008, 10:30:26 AM9/6/08
to Helma NG
Excerpts from Hannes Wallnoefer's message of Sat Sep 06 11:25:58 +0200 2008:

> Actually, in Helma 1 prefix and suffix are not applied to the default
> value

Hmm, I'm not so sure about this. I tried the following and it rendered
to "-0+":

function foo_macro(params) {
return '';
}
function foobar_action() {
this.renderSkin(
createSkin('<% this.foo prefix="-" suffix="+" default="0" %>'));
}

I tried this because I wanted the filter behaviour to mimic the old
behaviour as closely as possible.

> I generally agree, as the additional pipe characters are not really a
> problem, and it makes sense to use the filter/pipe feature whenever we
> can.

Fine!

> What about an additional filter names "wrap" that combines prefix and
> suffix?
>
> <% code | wrap "<pre>" "</pre>" %>

Good idea, added it to my skins branch [1].

[1] http://github.com/earl/helma-ng/commit/c195f2

--
Andreas

Hannes Wallnoefer

unread,
Sep 8, 2008, 5:05:20 AM9/8/08
to Helma NG
On Sep 6, 4:30 pm, Andreas Bolka <andreas.bo...@gmx.net> wrote:
> Excerpts from Hannes Wallnoefer's message of Sat Sep 06 11:25:58 +0200 2008:
>
> > Actually, in Helma 1 prefix and suffix are not applied to the default
> > value
>
> Hmm, I'm not so sure about this. I tried the following and it rendered
> to "-0+":
>
>     function foo_macro(params) {
>       return '';
>     }
>     function foobar_action() {
>       this.renderSkin(
>           createSkin('<% this.foo prefix="-" suffix="+" default="0" %>'));
>     }
>
> I tried this because I wanted the filter behaviour to mimic the old
> behaviour as closely as possible.

Quite strange, but no time to look into this now.

> > I generally agree, as the additional pipe characters are not really a
> > problem, and it makes sense to use the filter/pipe feature whenever we
> > can.
>
> Fine!
>
> > What about an additional filter names "wrap" that combines prefix and
> > suffix?
>
> >  <% code | wrap "<pre>" "</pre>" %>
>
> Good idea, added it to my skins branch [1].
>
> [1]http://github.com/earl/helma-ng/commit/c195f2

Cool. I've merged your changes into my master branch.

Hannes

> --
> Andreas
Reply all
Reply to author
Forward
0 new messages