### 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!
``` -------------------------------- ### Simple Void Element Example Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/complete-element-attribute-list.md Demonstrates creating a void element like `` with attributes. ```haskell img_ [src_ "photo.jpg", alt_ "Photo"] ``` -------------------------------- ### HTML Form Example Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/quick-start.md Demonstrates creating an HTML form with input fields and labels using Lucid. ```Haskell import Lucid loginForm :: Html () loginForm = form_ [method_ "post", action_ "/login"] (do label_ [for_ "username"] "Username:" input_ [id_ "username", name_ "username", type_ "text"] label_ [for_ "password"] "Password:" input_ [id_ "password", name_ "password", type_ "password"] button_ [type_ "submit"] "Login") ``` -------------------------------- ### Nested Elements Example Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/complete-element-attribute-list.md Demonstrates how to create nested elements, such as an unordered list with list items. ```haskell ul_ (do li_ "Item 1" li_ "Item 2" li_ "Item 3") ``` -------------------------------- ### Example: Text → Attribute Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/class-reference.md Demonstrates using a Text value as an Attribute, specifically for a class attribute. ```haskell import Lucid -- Used as attribute attr = class_ "intro" element = p_ [attr] "Hello" ``` -------------------------------- ### Unordered List Example Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/quick-start.md Shows how to create an unordered HTML list (ul) with list items (li) using Lucid. ```Haskell import Lucid shoppingList :: Html () shoppingList = ul_ (do li_ "Milk" li_ "Eggs" li_ "Bread" li_ "Cheese") ``` -------------------------------- ### HTML Table Example Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/quick-start.md Demonstrates creating an HTML table with a header (thead) and body (tbody) using Lucid. ```Haskell import Lucid scoreTable :: Html () scoreTable = table_ (do thead_ (tr_ (do th_ "Name" th_ "Score")) tbody_ (do tr_ (do td_ "Alice" td_ "95") tr_ (do td_ "Bob" td_ "87"))) ``` -------------------------------- ### GHCi Setup for Lucid Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/configuration.md Configures the GHCi environment with necessary language extensions and imports for interactive Lucid use. ```haskell :set -XOverloadedStrings -XExtendedDefaultRules import Lucid ``` -------------------------------- ### Attribute Creation and Usage Example Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/types.md Shows how to create an Attribute using makeAttribute and then use it to define an HTML element. Requires importing Lucid. ```haskell import Lucid attr = makeAttribute "id" "main" element = div_ [attr] "Content" ``` -------------------------------- ### Example: Attributes → Children → Element Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/class-reference.md Constructs a paragraph element with specified attributes and content. ```haskell import Lucid -- First argument: list of attributes -- Return value: function accepting children element = p_ [class_ "intro", id_ "p1"] "Hello" ``` -------------------------------- ### Simple HTML Document Structure Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/quick-start.md An example of creating a complete HTML document with head and body sections using Lucid. ```Haskell import Lucid document :: Html () document = doctypehtml_ (do head_ (do meta_ [charset_ "utf-8"] title_ "My Page") body_ (do h1_ "Welcome" p_ "Content goes here")) main = renderToFile "index.html" document ``` -------------------------------- ### Write Your First Lucid Page Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/quick-start.md A simple example of creating an HTML page using Lucid, including a main function to display it. ```Haskell page :: Html () page = do h1_ "Hello, World!" p_ "This is my first Lucid page" main :: IO () main = putStrLn $ show page ``` -------------------------------- ### Bootstrap Grid Layout Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/quick-start.md An example of using Lucid with the Lucid.Bootstrap library to create a basic Bootstrap grid layout. ```Haskell import Lucid import Lucid.Bootstrap layout :: Html () layout = containerFluid_ (do row_ (do span3_ (p_ "Sidebar") span9_ (p_ "Main Content"))) ``` -------------------------------- ### Example: Adding Attributes to Content-less Elements Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/class-reference.md Shows how to use the 'with' function to add attributes to void elements. ```haskell import Lucid element = br_ `with` [class_ "spacer"] ``` -------------------------------- ### Example: Constructing HTML Lists with Lucid Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-html5.md Demonstrates how to use Lucid functions to create an unordered list with two items and an ordered list with two steps. Requires importing Lucid. ```haskell import Lucid page = do ul_ (do li_ "First item" li_ "Second item") ol_ (do li_ "First step" li_ "Second step") ``` -------------------------------- ### Example: Constructing an HTML Table with Lucid Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-html5.md Shows how to build an HTML table with a header row and two data rows using Lucid functions. Requires importing Lucid. ```haskell import Lucid table_ (do thead_ (tr_ (do th_ "Name" th_ "Age")) tbody_ (do tr_ (do td_ "Alice" td_ "30") tr_ (do td_ "Bob" td_ "25"))) ``` -------------------------------- ### Interactive Lucid HTML Generation in GHCi Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/configuration.md Demonstrates using Lucid to generate HTML directly in the GHCi prompt after setup. ```haskell λ> p_ "Hello"

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

``` ``` -------------------------------- ### Generating DOCTYPE and HTML Element Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-html5.md Combines the HTML5 DOCTYPE declaration with the root html_ element. This is a common way to start an HTML5 document. ```haskell import Lucid page = doctypehtml_ (body_ (p_ "Content")) ``` -------------------------------- ### Create HTML Element with makeElement Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-base.md Use makeElement to create standard HTML elements with start and end tags. It takes the element name and its children as arguments. This is the base for common combinators like p_ and div_. ```haskell makeElement :: Functor m => Text -> HtmlT m a -> HtmlT m a ``` ```haskell import Lucid customElement = makeElement "section" (p_ "Content") -- Produces:

Content

``` -------------------------------- ### Boolean Attributes Example Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/complete-element-attribute-list.md Demonstrates the use of boolean attributes like `checked_` and `disabled_` on elements. ```haskell input_ [type_ "checkbox", checked_] button_ [disabled_] "Click me" ``` -------------------------------- ### Execute HtmlT and Extract Builder Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-base.md Use execHtmlT to run an HtmlT computation and get the resulting Blaze Builder. The return value of the computation is discarded. This is analogous to execState. ```haskell execHtmlT :: Monad m => HtmlT m a -> m Builder ``` ```haskell import Lucid import qualified Blaze.ByteString.Builder as Blaze html = p_ "Test" builder <- execHtmlT html let bs = Blaze.toLazyByteString builder ``` -------------------------------- ### Enable Compiler Optimizations (Command Line) Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/configuration.md Build your project with GHC optimizations enabled directly from the command line. ```bash cabal build --ghc-options="-O2" ``` -------------------------------- ### Accessing Low-Level Builder for Control Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/patterns-and-advanced.md Illustrates how to access Lucid's underlying Blaze Builder for maximum control over HTML generation and efficient writing to files. ```haskell import Lucid import Lucid.Base import qualified Blaze.ByteString.Builder as Blaze import qualified Data.ByteString.Lazy as BL html = div_ "Content" -- Get the Builder builder <- execHtmlT html -- Write to file efficiently BL.writeFile "out.html" $ Blaze.toLazyByteString builder ``` -------------------------------- ### doctypehtml_ Source: https://github.com/chrisdone/lucid/blob/master/_autodocs/api-reference/lucid-html5.md Combines the DOCTYPE declaration with the root `` element wrapper. It simplifies the initial setup of an HTML document. ```APIDOC ## doctypehtml_ ### Description Combines DOCTYPE and `html_` element wrapper. Simplifies the initial setup of an HTML document. ### Method N/A (Function call) ### Endpoint N/A (Function call) ### Parameters - `arg` (HtmlT m a): The content to be placed within the `` tags. ### Response Outputs `` followed by the wrapped HTML content. ### Example ```haskell import Lucid page = doctypehtml_ (body_ (p_ "Content")) -- Produces:

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 () ``` ```html

Lucid 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: ```