• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Conditionally populate attributes inside div

 
Ranch Hand
Posts: 157
Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 28344
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic