Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7071563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:42:11+00:00 2026-05-28T05:42:11+00:00

Text.Blaze has an operator ! to add attributes to html: option ! id bla

  • 0

Text.Blaze has an operator ! to add attributes to html:

option ! id "bla" ! value "1" ! selected "" $ "Hello!"

My question is how can i make attributes optional ?
Right now my code is ugly:

option ! id "bla" ! value "1" ! (if x == val then selected "" else someStupidAttribute "")  $ "Hello!"

This leads to every html option element to have unnecessary irrelevant attribute just because i have to supply one.

EDIT: I accepted hammar’s answer. I created a helper function:

(!?) :: Attributable h => h -> (Bool,Attribute) -> h
html !? (True, attr) = html ! attr
html !? _ = html

And here’s how to use it:

option ! id "bla" ! value "1" !? ((k == val), selected "") $ "Hello!"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:42:12+00:00Added an answer on May 28, 2026 at 5:42 am

    How about defining a convenience operator to apply attributes conditionally?

    (!?) :: Attributable h => h -> Maybe Attribute -> h
    html !? (Just attr) = html ! attr
    html !? Nothing = html
    

    With this, you can write your example as follows.

    option ! id "bla" ! value "1" !? toMaybe (x == val) (selected "") $ "Hello!"
    

    Here, toMaybe is just a useful helper for building Maybe values, but you can use something else if you wish.

    toMaybe :: Bool -> a -> Maybe a
    toMaybe False _ = Nothing
    toMaybe True  x = Just x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some text before the code so that the question summary isn't mangled. class Tree
A Text field ( SPFieldText ) has a limit of 255 characters. What, if
alt text http://barbarosalp.com/questions/list.png Hi, As you can see on the picture above i have
Text data-bind expressions can have only a single property. How would I pluralize some
text block how to do: text trimming for a given height vertical alignment Can
The text could be stored in the database using either markdown, bbcode, html, etc.
On an HTML page when I make a HTTP POST submission, it redirects to
Text file 1 has the following format: 'WORD': 1 'MULTIPLE WORDS': 1 'WORD': 2
Text and sample code adapted from http://www.webdeveloper.com/forum/archive/index.php/t-65078.html , may not reflect actual question: I
alt text http://img375.imageshack.us/img375/9830/combobox.png Setting the IsHitTestVisible=false has the effect of having the whole ComboBox's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.