### Fuller Lucid Example with Attributes Source: https://github.com/chrisdone/lucid/blob/master/lucid2/README.md A comprehensive example demonstrating nested elements with various attributes, including colspan and style. ```haskell table_ [rows_ "2"] (tr_ (do td_ [class_ "top",colspan_ "2",style_ "color:red"] (p_ "Hello, attributes!") td_ "yay!")) ``` ```html
|
Hello, attributes! |
yay! | |
Hello
``` -------------------------------- ### Example: Adding Attributes to Content-ful Elements Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/class-reference.md Demonstrates using the 'with' function to add attributes to elements that contain content. ```haskell import Lucid element = div_ `with` [id_ "main"] (p_ "Content") ``` -------------------------------- ### HtmlT Example with Reader Monad Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/types.md Demonstrates using HtmlT with the Reader monad to generate HTML that incorporates environment data. Requires importing Lucid and Control.Monad.Reader. ```haskell import Lucid import Control.Monad.Reader html :: HtmlT (Reader String) () html = do name <- lift ask p_ (toHtml name) result = runReader (renderTextT html) "Alice" ``` -------------------------------- ### Overload Resolution Examples Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/class-reference.md Demonstrates how the compiler resolves the correct Term instance based on the provided arguments and expected type. ```haskell import Lucid -- Instance 2: Children only p_ "text" -- HtmlT m () -> HtmlT m () -- Instance 1: Attributes, then children p_ [class_ "c"] "text" -- HtmlT m () -> HtmlT m () -- Instance 3: As attribute [p_ "intro"] -- [Attribute] because attribute list is expected -- Chained: first creates element function, then applies content div_ [id_ "x"] (p_ "hello") -- Attributes → Children → HtmlT m () ``` -------------------------------- ### Data Attributes Example Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/complete-element-attribute-list.md Illustrates how to add custom data attributes to an element using the `data_` prefix. ```haskell div_ [data_ "user-id" "123", data_ "section" "profile"] "Content" ``` -------------------------------- ### Using MonadState with HtmlT Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/class-reference.md Shows how to use the MonadState instance within HtmlT to manage and update state, demonstrated with a counter example. ```haskell import Lucid import Control.Monad.State counter :: HtmlT (State Int) () counter = do n <- get -- MonadState put (n + 1) li_ (toHtml $ show n) ``` -------------------------------- ### Lucid HTML5 Sectioning Elements Example Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-html5.md Demonstrates the usage of sectioning elements like header, nav, article, and footer in a basic page structure. ```haskell import Lucid page = body_ (do header_ (h1_ "Site Title") nav_ (ul_ (li_ (a_ [href_ "/"] "Home"))) article_ (p_ "Content") footer_ "© 2024") ``` -------------------------------- ### Basic Html Usage Example Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-base.md Demonstrates the basic usage of the `Html` type for creating simple HTML structures like a div containing a paragraph. ```haskell import Lucid page :: Html () page = div_ (p_ "Hello, World!") ``` -------------------------------- ### Lucid HTML5 Heading Elements Example Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-html5.md Shows how to use heading elements from h1_ to h6_ to structure content with different levels of importance. ```haskell import Lucid page = do h1_ "Main Heading" h2_ "Subheading" h3_ "Sub-subheading" ``` -------------------------------- ### Example of Creating a Form with Lucid Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-html5.md Demonstrates how to construct an HTML form using Lucid, including labels, input fields, a textarea, and a submit button. Attributes like `method`, `action`, `for`, `id`, `name`, and `type` are shown. ```haskell import Lucid form_ [method_ "post", action_ "/submit"] (do label_ [for_ "name"] "Name:" input_ [id_ "name", name_ "name", type_ "text"] label_ [for_ "message"] "Message:" textarea_ [id_ "message", name_ "message"] "" button_ [type_ "submit"] "Submit") ``` -------------------------------- ### Example: Children Only → Element Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/class-reference.md Constructs paragraph elements with only text content, including multi-line content. ```haskell import Lucid -- No attributes, just children element1 = p_ "Hello" element2 = p_ (do "First" "Second") ``` -------------------------------- ### ToHtml Examples: Escaped and Raw Output Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/types.md Demonstrates the difference between toHtml (which escapes special characters) and toHtmlRaw (which outputs content as-is). ```haskell import Lucid escaped = toHtml ("" :: String) -- Output: <script>alert(1)</script> raw = toHtmlRaw "Bold" -- Output: Bold ``` -------------------------------- ### toHtml Method Example (Escaping) Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/class-reference.md Demonstrates converting a string with special HTML characters to HTML using `toHtml`, which automatically escapes them. ```haskell import Lucid p_ (toHtml ("" :: String)) -- Output:<script>alert('xss')</script>
``` -------------------------------- ### Lucid HTML5 Text Content Elements Example Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-html5.md Illustrates the use of paragraph, preformatted text, and blockquote elements for text content. ```haskell import Lucid page = do p_ "Normal paragraph" pre_ "Code example with\nspecific formatting" blockquote_ "A famous quote" ``` -------------------------------- ### Defining Custom ToHtml Instance for User Type Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/class-reference.md Provides an example of defining a custom ToHtml instance for a User data type, specifying how it should be rendered as HTML. ```haskell import Lucid data User = User { name :: String, age :: Int } instance ToHtml User where toHtml user = do p_ (toHtml $ name user) p_ (toHtml $ "Age: " ++ show (age user)) toHtmlRaw = toHtml -- Use escaping version ``` -------------------------------- ### toHtmlRaw Method Example (No Escaping) Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/class-reference.md Demonstrates converting a string to HTML using `toHtmlRaw`, which outputs the content without any character escaping. ```haskell import Lucid p_ (toHtmlRaw "Bold") -- Output:Bold
``` -------------------------------- ### HTML Generation with Logging using Writer Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/patterns-and-advanced.md Integrate the Writer monad to log messages during HTML generation. This example logs section rendering events. ```haskell import Lucid import Control.Monad.Writer renderSection :: String -> Html () -> HtmlT (Writer [String]) () renderSection title content = do tell ["Rendering section: " ++ title] section_ (do h2_ (toHtml title) toHtml content) page :: HtmlT (Writer [String]) () page = do renderSection "Intro" (p_ "Hello") renderSection "Content" (p_ "Body") renderSection "Footer" (p_ "Bye") -- Usage: main = do let (html, logs) = runWriter (renderBST page) mapM_ putStrLn logs putStrLn $ show html ``` -------------------------------- ### HTML with IO Monad Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/configuration.md This snippet shows how to generate HTML that interacts with the IO monad, for example, reading input. Import Control.Monad.IO.Class. ```haskell import Control.Monad.IO.Class page :: HtmlT IO () page = do msg <- liftIO getLine p_ (toHtml msg) output = renderTextT page ``` -------------------------------- ### Explicit Type Annotations in Lucid Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/quick-start.md Provides examples of explicit type annotations for Lucid HTML generation, both for simple and monadic structures. ```haskell import Lucid -- Explicit type annotation myPage :: Html () myPage = div_ "Content" -- Monadic version myPage' :: Html () myPage' = do h1_ "Title" p_ "Text" ``` -------------------------------- ### Example of Using Inline Text Elements in Lucid Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-html5.md Demonstrates how to combine various inline text elements like `strong_` and `em_` within a paragraph (`p_`) using Lucid's monadic syntax. ```haskell import Lucid page = p_ (do "This is " strong_ "important" " and this is " em_ "emphasized" ".") ``` -------------------------------- ### Bootstrap Span Element with Classes Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-bootstrap.md Example of creating a Bootstrap span element with custom CSS classes and data attributes using Lucid.Bootstrap. ```haskell import Lucid import Lucid.Bootstrap page = container_ ( row_ ( span6_ [class_ "highlighted", data_ "section" "main"] ( p_ "Content with additional classes"))) ``` -------------------------------- ### makeElement Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-base.md Creates an HTML element with specified start and end tags, serving as a base for other element combinators. It takes the element's tag name and its children content. ```APIDOC ## makeElement ### Description Creates an HTML element with start and end tags. This is the foundation for element combinators like `p_` and `div_`. ### Method ```haskell makeElement :: Functor m => Text -> HtmlT m a -> HtmlT m a ``` ### Parameters #### Path Parameters - **name** (Text) - Required - Element tag name - **children** (HtmlT m a) - Required - Child content ### Response #### Success Response - **HtmlT m a** - The rendered element ### Request Example ```haskell import Lucid customElement = makeElement "section" (p_ "Content") -- Produces:Content
Content
Content
``` ``` -------------------------------- ### Render HTML to a File Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/quick-start.md Shows how to render a Lucid Html structure directly to an HTML file. ```Haskell import Lucid page :: Html () page = h1_ "Hello" -- To file main = renderToFile "output.html" page ``` -------------------------------- ### container_ Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-bootstrap.md Creates a fixed-width grid container by applying the `container` class. ```APIDOC ## container_ ### Description A fixed-width grid container. Applies the `container` class. ### Returns Container div with `class="container"` ### Example ```haskell import Lucid import Lucid.Bootstrap page = container_ (do row_ (do span6_ "Left column" span6_ "Right column")) ``` ``` -------------------------------- ### Element with Attributes and Content Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-html5.md Demonstrates how to apply global attributes like id, class, and data attributes to an element. Ensure the Lucid library is imported. ```haskell import Lucid element = div_ [id_ "main", class_ "container active", data_ "custom" "value"] "Content" ``` -------------------------------- ### Get Blaze Builder from HtmlT Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/configuration.md This snippet shows how to obtain a Blaze ByteString Builder from an HtmlT computation for advanced rendering scenarios. ```haskell import Lucid.Base (execHtmlT) import qualified Blaze.ByteString.Builder as Blaze html = div_ "Content" builder <- execHtmlT html ``` -------------------------------- ### Basic Elements Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/quick-start.md Demonstrates the basic syntax for creating HTML elements in Lucid using a postfix underscore. ```Haskell p_ "Hello" div_ "Content" h1_ "Title" ``` -------------------------------- ### Responsive Grid Layout with Lucid Bootstrap Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-bootstrap.md Demonstrates a responsive grid layout using span6_ for half-width columns on larger screens and full-width on mobile, and span12_ for a full-width row. ```haskell import Lucid import Lucid.Bootstrap responsive = containerFluid_ (do row_ (do span6_ "Mobile: full width / Desktop: half" span6_ "Mobile: full width / Desktop: half") row_ (do span12_ "Full width on all sizes")) ``` -------------------------------- ### Basic Lucid.Html5 Usage Patterns Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-html5.md Demonstrates various ways to use Lucid.Html5 combinators for elements and attributes, including elements with children, attributes, or both, as well as monadic composition. ```haskell p_ "Hello" ``` ```haskell p_ [class_ "intro"] "Hello" ``` ```haskell br_ [class_ "spacer"] ``` ```haskell class_ "intro" ``` ```haskell div_ (do p_ "First" p_ "Second") ``` -------------------------------- ### Creating the Body Element Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-html5.md Generates the element, which contains all the visible content of an HTML page. This example shows a simple body with an H1 heading. ```haskell import Lucid page = body_ (h1_ "Title") ``` -------------------------------- ### Modular Page Composition with Lucid Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/patterns-and-advanced.md Demonstrates breaking down a page into reusable components like headers, sidebars, main content, and footers for modularity. ```haskell import Lucid header :: Html () header = header_ (do nav_ (ul_ (li_ (a_ [href_ "/"] "Home"))) h1_ "Site Title") sidebar :: Html () sidebar = aside_ (do h2_ "Categories" ul_ (li_ "News")) mainContent :: Html () mainContent = main_ (p_ "Page content") footer :: Html () footer = footer_ (p_ "© 2024") page :: Html () page = doctypehtml_ (do head_ (title_ "My Site") body_ (do header div_ [class_ "container"] (do sidebar mainContent) footer) ``` -------------------------------- ### Import Lucid for HTML5 Projects Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/configuration.md Use this import for standard HTML5 projects. ```haskell {-# LANGUAGE OverloadedStrings, ExtendedDefaultRules #-} import Lucid import Lucid.Html5 ``` -------------------------------- ### Setting Attributes with Argument List Source: https://github.com/chrisdone/lucid/blob/master/lucid2/README.md Shows how to set attributes for an HTML element by providing a list of attribute-value pairs. ```haskell λ> p_ [class_ "brand"] "Lucid Inc" :: Html () ``` ```htmlLucid Inc
``` -------------------------------- ### Hspec Test Setup for Lucid HTML Rendering Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/configuration.md Set up Hspec tests to verify Lucid's HTML rendering and content escaping capabilities. ```haskell import Lucid import Test.Hspec main :: IO () main = hspec $ do describe "HTML rendering" $ do it "renders simple elements" $ do renderText (p_ "hello") `shouldBe` "hello
" it "escapes content" $ do renderText (p_ " -- Not escaped even though it contains special characters style_ "body { color: red; }" -- Produces: ```