Leon Messerschmidt wrote:
> Only inserted values should be scanned for entities. A template like this:
>
> #set ($sometext=" < ")
> <text>$sometext</text>
>
> needs to be rendered like this:
>
> <text><</text>
>
> The only point where I can determine whether I need to replace entities is
> at "render time".
Not really. The only place to find out what to escape is in the template
where it is being generated. Therefore use a velocimacro, see below.
Jose Alberto Fernandez wrote:
> I asked for this same type of functionality from the first time I tried to
> use and for my XML stuff. Writing $tools.escapeEntities($sometext) on every
> substitution in the template makes the template unreadable.
Why not use:
#macro( xenc $sometext )$tools.escapeEntities($sometext)#end
and use it as:
#set( $sometext = " < " )
<text>#xenc($sometext)</text>
Another trick would be to create an encoding utility that takes the
context as a constructor parameter and only implements a method:
public String get(String key)
{
Object obj = context.get(key)
return (obj != null) ? Escape.getText( obj.toString() ) : "";
}
Put it into the context under "xenc". Then you can do:
<text>$xenc.sometext</text>
Alternatively you can implement your own context which always
applies the encoding to any string returned. This would be the
NonPlusUltraXmlContext. Be carefull to avoid rendering the
output of method calls directly (which could return objects or
strings wich might need encoding), place them first into the
context with a #set() directive and the use that:
#set( $sometext = $jdomElement.getText() )
<text>$sometext</text>
Hope this helps (let me know),
:) Christoph