[stringtemplate-interest] Spaces around template attributes
Zenaan Harkness
zen at freedbms.net
Wed Apr 29 08:09:52 PDT 2009
On Wed, Apr 29, 2009 at 09:43:30AM -0500, Steve Ebersole wrote:
> I am trying to deal with extra spaces in my stringtemplate output when
> attribute values used within the template are null/empty. I figure this
> has to be a pretty common requirement, so I thought I'd ask here how to
> accomplish this.
>
> For example, I have a template like:
> sortSpecification(sortKey,collationSpecification,orderingSpecification) ::= <<
> <sortKey> <collationSpecification> <orderingSpecification>
> >>
One way is to embed the required space into either
a) the attribute you pass in, or
b) a parent super-template which calls the sub-template -
this might be OK if you have a moderately comples template
hierarchy.
For example, if your "collateionSpecification" attribute is either null,
or "abcXYZ " for example, and your template's middle line looked like:
<sortKey> <collationSpecification><orderingSpecification>
Then this will do what you require.
Clearly, your templates contains those space characters, and of course
those are spaces in a template, therefore the template engine must
include them if they're there.
> Only 'sortKey' is required in the corresponding (SQL-based) grammar. So
> if you consider a call to this template with sortKey="a",
> collationSpecification=null, orderingSpecification="asc" the output here
> ends up being "a asc" (notice the 2 spaces between 'a' and 'asc').
>
> So how to handle this such that the extra spaces are omitted? I
> understand I *can* do something like:
> sortSpecification(sortKey,collationSpecification,orderingSpecification) ::= <<
> <sortKey><if(collationSpecification)>
> <collationSpecification><endif><if(orderingSpecification)>
> <orderingSpecification><endif>
> >>
This is another solution - if you require character-perfect output and
you can't munge the input (eg. by putting the extra space on your
template's input attribute), then your conditionals will be required.
If you are doing offline processing/ generation, the performance hit
should be irrelevant.
If using ST in an online/ dynamic website scenario, then you might be
cautious regarding performance. Or you might not - look at your
potential userbase/ load, naturally...
Regards
Zen
--
Homepage: www.SoulSound.net -- Free Australia: www.UPMART.org
Please respect the confidentiality of this email as sensibly warranted.
More information about the stringtemplate-interest
mailing list