[stringtemplate-interest] String manipulations

John Snyders jjsnyders at rcn.com
Sun Oct 1 18:28:02 PDT 2006


coments inline below
-John

> -----Original Message-----
> From: Zenaan Harkness [mailto:zen at freedbms.net]
> Sent: Sunday, October 01, 2006 4:28 AM
> To: John Snyders
> Subject: Re: [stringtemplate-interest] String manipulations
>
>
> > > > > $names:{$i$. <b>$it$</b>}$; format="upperCase"$
> > > > >
> > > > > Here, the entire list of crap would be uppercased
> including the $i$
> > > > > numbers.  Right?
> > > >
> > > > Yes I think that makes sense.
> > >
> > > 'separator' applies "between item pairs". Intuitively I would
> > > expect 'format' to apply "to each item".
> > >
> > > What if format="initialCaps"? In this case I would expect format
> > > to be passed each item one by one.
> ...
>
> > I think that format should apply to the whole template
> expression result. It
> > is consistent with the intent of format which is to control
> rendering. There
> > is no loss of control since you can do
> >   $names:{Hello $it$;format="toUpper"}$
> > or
> >   $names:{Hello $it$};format="toUpper"$
> >
> > with names having Bob, Sue the first will output
> >   Hello BOB Hello SUE
> > The second will output
> >   HELLO BOB HELLO SUE
>
> I think it should be semantically similar to other options, such as
> the separator option. The separator option applies kind of to each
> item separately. It would therefore be consistent for format to apply
> separately to each item as well, yes?
>
> It feels to me like when we are operating or 'iterating' over a list
> of items, each operator (such as the format 'operator') should apply
> to each item. That feels natural to me from an algorithmic perspective.

I don't feel too strongly one way or the other as long as there is a way to
acomplish either format each item in a collection or format the whole
template.

If format behaved such that it formated each item then I think you could
accomplish formating the whole thing with

${$names:{Hello $it$};separator=", "$};format="toUpper"$

The outer unnamed template causes the output of the iteration to be taken as
a whole and the format would apply to the whole thing.

> In the case of toUpper, it is conceivable that the long term average
> 'most likely' need is toUpper the entire output, however, if we expand
> our perspective into for example uppercaseInitialLetterOfItem, then
> it would be most intuitive that we would want to uppercase the initial
> letter of each item iterated, not simply uppercase the first letter of
> the entire output.

I agree that initialCaps makes most sense most of the time to apply to each
iterated item.
Perhaps toUpper does too because if you want to upper case literal text in
the template just type it in in all caps. Why do this
$"aaa";format="toUpper"$ when you can just do AAA.

> > The other question was how does ;format interact with
> ;separator. Or more
> > general how do all the options interact. I'm not sure but I
> think the order
> > should be ;null, ;separator, ;format. I don't know where wrap
> and anchor fit
> > in.
> >
> >
> > > How about these:
> > >
> > > /* add separator after each name: */
> > > $names:separator=","$
> >
> > No the : operator is used to apply templates so this would be confusing.
>
> Could not 'separator' (in the context of a separator after EACH
> item), be written simply as a template anyway, in which case this
> is semantically consistent? (not that I can imagine a general use
> for separator AFTER EACH item anyway... but I'm just thinking of
> semantic consistency here)

If separator was a template the syntax would look like
 $names:separator()$
what is not clear is how to pass in the separator text. Possibly like this
 $names:separator(sep=",")$

But if separator is a special template then it polutes the namespace - the
template writer can't create a template with the name separator.

You can always create your own separator template
separator() ::= "$it$,"

and use it as above.

> > Also it is not needed because one can always add the extra , at the end
> > $names;separator=","$,
> > If there is a chance names could be empty then use
> > $names;separator=","$$if(names)$,$endif$
> >
> > > /* add separator after all but last name: */
> > > $names;separator=","$
> >
> > This is what we already have. It puts the separator between the
> elements.
> >
> > >
> > > $names:{$i$. <b>$it$</b>}:format="upperCase"$
> > > ...
> > > $(names:{$i$. <b>$it$</b>});format="initialCaps"$
> > >
> > > ??
> >
> > Again the :format syntax is confusing.
> > The ;format syntax is something I am in favor of as long as the format
> > string (upperCase) for example specifies a method to call and
> not a string
> > to pass to a method.
>
> If this is to be the outcome, then does it make most sense to
> remove the double quotes? eg. ;format=toUpper ??

The syntax is not ;option="value" but ;option=expr

;format=toUpper means to take the name of the format from an attribute
called toUpper

> > The value of the format options "initialCaps", "upperCase" etc.
> should have
> > no effect on how format behaves.
>
> Agreed. (And to repeat my point above, I do think the more common
> expectation is that when iterating, an operator would apply to
> each item iterated over, not to the aggregate result of the iteration.)
>
> Thanks
> Zen
>
> --
> Free Australia - www.UPMART.org
> Please respect the confidentiality of this email as sensibly warranted.


More information about the stringtemplate-interest mailing list