English 中文(简体)
文本中的第html号。 Blaze
原标题:Optional html attributes in Text.Blaze
  • 时间:2012-01-14 08:37:41
  •  标签:
  • html
  • haskell

文本Blaze有经营者! 添加属性:

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!"

这导致每一个html选择要素都没有必要的无关性归属,因为我必须提供这种特性。

EDIT:我接受了hammar的答复。 我设立了助手职能:

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

在这里,如何使用:

option ! id "bla" ! value "1" !? ((k == val), selected "") $ "Hello!"
最佳回答

如何界定方便操作者有条件适用属性?

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

因此,你可以举如下例子。

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

这里,to Maybe只是建造Maybe的有用助手,但如果你愿意,你可以使用其他东西。

toMaybe :: Bool -> a -> Maybe a
toMaybe False _ = Nothing
toMaybe True  x = Just x
问题回答

我对原始条件有利的trick计是使用清单的缩略语;[foo × >将评价为[foo] [ ]。 我们能够调整这一trick子? 确实,具有适用属性清单的助手职能:

element !. options = foldr (!) element options -- (!.) = foldr (!)

现在,你可以写出与其中之一相对相当的东西(取决于信使):

option ! id "bla" ! value "1" !. [selected "" | x == val] $ "Hello!"
option !. [id "bla", value "1"] ++ [selected "" | x == val] $ "Hello!"

您可能必须添加“!;您可在格西里使用:i !,以了解您为相互协作而重新努力的工作。

我不知道这样做有没有更好的布莱泽具体办法,我知道这是可怕的,但你可以这样做。

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

这对我来说似乎很有益——我强调:

import Data.Monoid (mempty)
option ! id "bla" ! value "1" ! (if x == val then selected "" else mempty) $ "Hello!"




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签