cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

How to add empty paragraph component check in vm file

Former Member
0 Likes
964

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, .

Accepted Solutions (0)

Answers (3)

Answers (3)

andyfletcher
Active Contributor
0 Likes

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}"
 ""
arvind-kumar_avinash
Active Contributor
0 Likes

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

andyfletcher
Active Contributor
0 Likes

I guess it depends on which version of Velocity is being used and therefore which version of Hybris. I ran it on a 6.1 install. Let me try on 1808 and feed back.

andyfletcher
Active Contributor
0 Likes

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)

arvind-kumar_avinash
Active Contributor
0 Likes

Thank you very much. It worked.

Former Member
0 Likes
 #if( $!ctx.content != "" ))
  ${ctx.content}
 #end
 
 
arvind-kumar_avinash
Active Contributor
0 Likes

You have mistyped and put an extra ! and an extra ) in your condition. Probably, you wanted to type #if( $ctx.content != "" )

andyfletcher
Active Contributor
0 Likes

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.

andyfletcher
Active Contributor
0 Likes

Probably is intended. See the linked documentation from my answer about quiet references. (Although the extra bracket is wrong)

arvind-kumar_avinash
Active Contributor
0 Likes

If that is the case, I would write it like

  #if( $!{ctx.content} != "" )
   ${ctx.content}
  #end

I thought, probably he meant

  #if( $ctx.content != "" )
   ${ctx.content}
  #end

But I will verify both of them and post my finding if both of them produce the same result or different results.

andyfletcher
Active Contributor
0 Likes

Mark them as 'quiet references'

https://velocity.apache.org/engine/1.5/user-guide.html#quietreferencenotation

e.g.

 $!{ctx.content}
Former Member
0 Likes

its not working