# HSX This `ihp-hsx` package provides `[hsx|

Hello World

|]` syntax to IHP projects. To use it outside of IHP, add this package to your cabal file and use the modules like this: ```haskell import IHP.HSX.QQ -- <- The main thing import IHP.HSX.ConvertibleStrings () -- <- Helper instancess import IHP.HSX.ToHtml () -- <- More helper instances import Text.Blaze.Html5 -- The blaze html library, IHP HSX is built on top of that view :: Html view = [hsx|

Hello World

|] -- To render the above view: import Text.Blaze.Html.Renderer.Text (renderHtml) rendered :: Text rendered = renderHtml view ``` ## Introduction HSX can be written pretty much like normal HTML. You can write an HSX expression inside your Haskell code by wrapping it with [`[hsx|YOUR HSX CODE|]`](https://ihp.digitallyinduced.com/api-docs/IHP-ViewPrelude.html#v:hsx). HSX expressions are just a syntax for [BlazeHtml](https://jaspervdj.be/blaze/) and thus are automatically escaped as described in the blaze documentation. Because the HSX is parsed, you will get a syntax error when you type in invalid HTML. ### Inline Haskell HSX can access Haskell variables wrapped with `{}` like this: ```haskell let x :: Text = "World" in [hsx|Hello {x}!|] ``` **If the variable is another HSX expression, a blaze HTML element, a text or string**: it is just included as you would expect. **If the variable is any other custom Haskell data structure**: it will first be converted to a string representation by calling [`show`](https://ihp.digitallyinduced.com/api-docs/IHP-Prelude.html#v:show) on it. You can add a custom [`ToHtml`](https://ihp.digitallyinduced.com/api-docs/IHP-HSX-ToHtml.html#t:ToHtml) (import it from `IHP.HSX.ToHtml`) instance, to customize rendering a data structure. You can also write more complex code like: ```haskell let items :: [Int] = [ 0, 1, 2 ] renderItem n = [hsx|Hello {n}!|] in [hsx|Complex demo: {forEach items renderItem}!|] ``` As the HSX expressions are compiled to Haskell code at compile-time, type errors inside these `{}` expressions will be reported to you by the compiler. ### Dynamic Attributes The variable syntax can also be used in attribute values: ```haskell let inputValue = "Hello World" :: Text in [hsx||] ``` #### Boolean Attribute Values HSX has special handling for Boolean values to make it easy to deal with HTML Boolean attributes like `disabled`, `readonly`, `checked`, etc. You can write ```haskell ``` as a short form for: ```haskell ``` Writing `False`: ```haskell ``` This will not render the attribute, [as specified in the HTML standard](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes): ```haskell ``` #### Boolean data attributes This behavior of omiting a attribute when it's set to `False` does not apply to `data-` attributes. You can write ```haskell
``` and it will render like this: ```html ``` When set to `False`, like this: ```haskell ``` the output HTML will keep the attribute and set it to `"false"`: ```html ``` #### Maybe Attribute Values / Optional Attributes HSX has special handling for Maybe values to make it easy to deal with optional attributes. You can write ```haskell let target :: Maybe Text target = Just "_blank" in [hsx||] ``` and it will render to: ```haskell ``` Using `Nothing` results in the `target` attribute not being in the output HTML: ```haskell let target :: Maybe Text target = Nothing in [hsx||] ``` This will render to: ```haskell ``` ### Spread Values For dynamic use cases you can use `{...attributeList}`: ```haskell
tshow userId, tshow userFirstname) ] } />
``` Note the `<>` concatenation operator. ### Special Elements: ` ``` Inside those tags using a Haskell expression will not work: ```haskell ``` This will just literally output the string `{myHaskellExpr}` without evaluating the Haskell expression itself. This is because JavaScript usually uses `{}` for object expressions like `{ a: "hello" }`. The same applies to inline CSS inside `