[stringtemplate-interest] Output string lengths..
Rob Hill
rob.hill at blueyonder.co.uk
Wed Nov 29 14:06:42 PST 2006
Lol , nice ;)
My rather smaller frontal lobe was thinking
<str; format="%.40s"; >
Same kind of thing as the null= attribute.
Obviously your pad method is far more flexible, but is complete overkill for
what i required.
In fact after re-visiting my padded string requirement, it's far simpler to
pad the string before I stuff it in the template.
After writing a custom renderer, i couldn't see the point in actually adding
all the code to put the padding in a class, and then set the renderer, when
i could just pad the string using the same function before i insert it into
the template.
?
Infact, im not sure i see the point in custom renderers at all now i think
about it.?
I may have lost the plot though, it seems to happen frighteningly often
these days!
I do like the chars function though, and i think it would be nice addition
to the ST repertoire.
Cheers,
Rob
From: stringtemplate-interest-bounces at antlr.org
[mailto:stringtemplate-interest-bounces at antlr.org] On Behalf Of John Snyders
Sent: 29 November 2006 19:02
To: Hill, Robert; StringTemplate
Subject: Re: [stringtemplate-interest] Output string lengths..
Here is a crazy thought.
If you could iterate over the characters in a string then the template could
do the padding.
I created a function "chars" which returns an iterator for its string
argument.
For example
$s1$
$chars(s1); separator="-"$
will produce
SOMETHING
S-O-M-E-T-H-I-N-G
when s1="SOMETHING"
With these attributes added in the controller:
test.setAttribute("s1", "SOMETHING");
test.setAttribute("s2", "NOTHING");
test.setAttribute("space40", " ");
This template
|$chars(s1),chars(space40): { ch, sp | $ch;null={$sp$}$}$|
|$chars(s2),chars(space40): { ch, sp | $ch;null={$sp$}$}$|
produces this output
|SOMETHING |
|NOTHING |
(forgive the proportional font of this email)
Does the chars function have any other use? Well it could be used to do what
the CSS pseudo element ":first-letter" does. For example:
<span class="fancy">$first(chars(s1))$</span>$rest(chars(s1))$
produces:
<span class="fancy">S</span>OMETHING
This gives some stylistic control over individual letters to the template.
What do people think, is the chars function useful?
As for padding, I'm not sure I like this solution better than the pad
option.
-John
Here is the code just incase someone wants to play with this:
Added to ASTExpr.java
private static class StringIterator implements Iterator
{
private String str;
private int index;
public StringIterator(String str) {
this.str = str;
index = 0;
}
public boolean hasNext() {
return index < str.length();
}
public Object next() {
String next = String.valueOf(str.charAt(index));
index++;
return next;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
public Object chars(Object attribute) {
if ( attribute==null ) {
return null;
}
Object chars = null;
if ( attribute instanceof String ) {
String str = (String)attribute;
chars = new StringIterator(str);
}
else if ( attribute instanceof StringTemplate )
{
StringTemplate st = (StringTemplate)attribute;
String str = st.toString();
chars = new StringIterator(str);
}
return chars;
}
Add this to eval.g near trunc
| "chars" a=singleFunctionArg {value=chunk.chars(a);}
Add this to action.g near trunc
| "chars"
-----Original Message-----
From: stringtemplate-interest-bounces at antlr.org
[mailto:stringtemplate-interest-bounces at antlr.org]On Behalf Of Hill, Robert
Sent: Wednesday, November 22, 2006 2:27 AM
To: StringTemplate
Subject: [stringtemplate-interest] Output string lengths..
Is there anyway I can make each string in a list be rendered at a minimum
length?
Id like some things to line up in columns, and want to avoid any sort of
formatting occuring in the code, before I stuff the strings into the
templates.
Is this a job for a custom format renderer, or is there a way to do this
directly within string template?
Ta!
Regards,
Rob
Robert Hill
Information Engineer
E UKIMEA DWP ACU, Hallamshire Business Park, 100 Napier St, Sheffield.
S11 8HD
email: rhill03 at eds.com
Office: +44 114 291 1928
Mobile: +44 7903 185 516
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061129/eef71861/attachment.html
More information about the stringtemplate-interest
mailing list