Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Conditionally populate attributes inside div

 
vivek dhiman
Ranch Hand
Posts: 153
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: 1822
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: 21892
36
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.
 
Let's go to the waterfront with this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!