on ‎2018 Oct 17 4:57 PM
Hi Experts,
If there is no content defined in paragraph component then it is displaying ${ctx.content} in email. Can anyone please help me on how to avoid this in email if there is empty component.
Regards, .
Request clarification before answering.
There seems to be some confusion around the quiet reference operator.
Try this Groovy from the hAC to see how the ! works with a variety of null and non null data
import org.apache.velocity.*
import org.apache.velocity.app.*
ctx = new VelocityContext()
ctx.put('rhubarb', 'custard')
ctx.put('foo', null)
ctx.put('map', [one: '1', two: '2', empty: null])
tmpl = '''\
"$rhubarb"
"$!rhubarb"
"$foo"
"$!foo"
"${map.one}"
"$!{map.one}"
"${map.empty}"
"$!{map.empty}"
'''
Velocity.evaluate(ctx, out, 'test', tmpl)
println ''
You should see
"custard"
"custard"
"$foo"
""
"1"
"1"
"${map.empty}"
""
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks, for clarifying my doubt. I understood the concept from the output that you have mentioned.
Just FYI: when I tried executing the code in hAC (v1808), I got some error which I have put in the attached file. If you already know the resolution, please mention that; otherwise, I will look into it later and try to make it work in hAC. link text
On 6.1 Groovy seemed to automatically wrap the out PrintStream in a PrintWriter.
This doesn't appear to be happening, and even Velocity.evaluate(ctx, new PrintWriter(out), 'test', tmpl) doesn't work
Just change the last 2 lines to this:
writer = new StringWriter()
Velocity.evaluate(ctx, writer, 'test', tmpl)
writer.toString()
It writes to an intermediate StringWriter and then just returns that (on the Results tab rather than Output)
#if( $!ctx.content != "" ))
${ctx.content}
#end
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm not entirely clear how this is useful. If $ctx.content is blank then surely it doesn't matter if you render it? In fact what you've done is added 2 extra carriage returns and some whitespace to the output. This probably isn't an issue if you're rendering to html but may be if your template is building some other format.
Mark them as 'quiet references'
https://velocity.apache.org/engine/1.5/user-guide.html#quietreferencenotation
e.g.
$!{ctx.content}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.