[stringtemplate-interest] nesting one formatted string within another formatted string

Udo Borkowski ub at abego-software.de
Sun Jan 30 08:16:49 PST 2011


Hi Eric,

instead of introducing an extra template ("jsEscape") you could also use anonymous templates. However you have to check from case to case what really is the better approach. E.g. extra templates with good names may make your templates more readable and better to maintain.

Here a complete example. 

Assume you want to render a string in lowercase, with a capital letter. The example shows two ways to do this, one with extra templates (toLower/cap), and one using anonymous templates:

toLower(s) ::= <<
$s;format="lower"$
>>

cap(s) ::= <<
$s;format="cap"$
>>

/* 
Render s in lower case, with a capitalized first letter.

Version 1: referencing "helper" templates
*/
toLowerAndCap(s) ::= <<
$cap(s=(toLower(s=s)))$
>>

/* 
Render s in lower case, with a capitalized first letter.

Version 2: "inline", with anonymous templates
*/
toLowerAndCapAnon(s) ::= <<
$({$s;format="lower"$});format="cap"$
>>

main() ::= <<
$toLower(s="abC")$
$cap(s="abC")$
$toLowerAndCap(s="abC")$
$toLowerAndCapAnon(s="abC")$
>>

This will output:

abc
AbC
Abc
Abc


Udo


On 30.01.2011, at 06:26, Eric B wrote:

> Hi All,
> 
> I'm building up an HTML string that should ultimately resolve to something like the following where someString is unknown and could contain special characters...
> <img src="someImage.gif" onClick="javascript:alert('someString');"/>
> 
> If someString really has special characters in it, it needs to be escaped for JS and then entire onClick attribute value needs to be escaped for HTML.
> 
> To format the full attribute value as HTML with StringTemplate is no problem:
> 
> escapeTest(someString) ::= <<
> <img src="someImage.gif" onClick="$"javascript:alert('" + someString + "');";format="HTML"$"/>
> >>
> 
> And to format someString as JS without formatting the HTML is no problem .  I do something like:
> escapeTest(someString) ::= <<
> <img src="someImage.gif" onClick="javascript:alert('$someString;format="JS"$');"/>
> >>
> 
> But I could not figure out a way to use the format option on someString when it's nested in a formatted expression.
> 
> The most graceful way I could figure to do this was to JS escape with another template... like so:
> jsEscape(jsString) ::= <<
> $jsString;format="JS"$
> >>
> 
> escapeTest(someString) ::= <<
> <img src="someImage.gif" onClick="$"javascript:alert('" + jsEscape(someString) + "');";format="HTML"$"/>
> >>
> 
> Does this seem the best I can do?  Or does the syntax support using an in-line format on someString?
> 
> Thanks!,
> 
> Eric
> 
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110130/521925fc/attachment-0001.html 


More information about the stringtemplate-interest mailing list