• Post Reply Bookmark Topic Watch Topic
  • New Topic

Conditionally populate attributes inside div  RSS feed

 
vivek dhiman
Ranch Hand
Posts: 155
Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have the below snippet of code :


What i want to evaluate the condition inside the markup that is getting populate. Such that If :

Title is empty markup will generate like : <div media-title="title" />
Both are available then markup will generate like : <div media-title="title" media-width="104"/>

Any Idea how can i achieve this.

Thanks
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Choose/when is more commonly used, and least from what I've seen. c:if doesn't have a corresponding c:else tag, so this give you more flexibility.
Check out this link for tips. It's built up like so:


You can have multiple "when" statements, think of them as if-else, and the final "otherwise" is your final else.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't the code you have posted already achieve this?

You could potentially put this logic into a custom tag to make your jsp pages easier to read.
The tag would take an attribute name, and a value for that attribute, but only render the attr="value" if a non-null value was passed.
So you could get



It just cuts down on the boiler-plate that was building up there.

You could then go further and have a <custom:mediaTag properties="${properties}"/> which could be responsible for rendering the entire div tag.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Doesn't the code you have posted already achieve this?


Well, not quite. Your code has a > character in line 4 which closes the start-tag of the <div> element. The OP's doesn't, which perhaps led to the original question.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!