### Lucius Runtime: Dynamic CSS Generation in Haskell Source: https://context7.com/yesodweb/shakespeare/llms.txt Explains how to use `luciusRT` and `luciusRTMixin` for parsing and rendering Lucius templates at runtime. This allows for dynamic CSS generation with variable substitution and the inclusion of mixins, returning either the generated CSS or an error message. ```haskell import Text.Lucius import qualified Data.Text as T import qualified Data.Text.Lazy as TL -- Runtime template parsing generateCSS :: T.Text -> T.Text -> Either String TL.Text generateCSS primaryColor fontSize = luciusRT template variables where template = TL.pack "body { color: #{primary}; font-size: #{size}; }" variables = [ (T.pack "primary", primaryColor) , (T.pack "size", fontSize) ] -- With mixins at runtime generateWithMixin :: Mixin -> Either String TL.Text generateWithMixin theMixin = luciusRTMixin (TL.pack ".styled { ^{mixin} }") True -- minify output [(T.pack "mixin", RTVMixin theMixin)] main :: IO () main = case generateCSS (T.pack "#333") (T.pack "16px") of Left err -> putStrLn $ "Error: " ++ err Right css -> TL.putStrLn css -- Output: "body {\n color: #333;\n font-size: 16px;\n}\n" ``` -------------------------------- ### Lucius: CSS with Variables and Nesting in Haskell Source: https://context7.com/yesodweb/shakespeare/llms.txt Demonstrates the Lucius quasi-quoter for writing CSS with variable interpolation and nesting directly within Haskell code. It requires the QuasiQuotes language extension and imports from Text.Lucius. The output is rendered using renderCssUrl. ```haskell {-# LANGUAGE QuasiQuotes #-} import Text.Lucius import Data.Text (Text, pack) import qualified Data.Text.Lazy as TL data MyRoute = StaticR String renderUrl :: MyRoute -> [(Text, Text)] -> Text renderUrl (StaticR path) _ = pack $ "/static/" ++ path -- CSS with variables and nesting styles :: CssUrl MyRoute styles = [lucius| @primaryColor: #3498db; @secondaryColor: #2ecc71; body { font-family: sans-serif; background: #{colorBlack}; } .container { max-width: 1200px; margin: 0 auto; .header { background: #{primaryColor}; color: white; a { color: #{secondaryColor}; &:hover { text-decoration: underline; } } } } @media (max-width: 768px) { .container { padding: 10px; } } |] main :: IO () main = TL.putStrLn $ renderCssUrl renderUrl styles ``` -------------------------------- ### Hamlet Maybe Handling: $maybe/$nothing Source: https://context7.com/yesodweb/shakespeare/llms.txt Renders a welcome message for a logged-in user or a login prompt if the user is not present. It utilizes the $maybe and $nothing directives to handle `Maybe` types. ```haskell -- Maybe handling with $maybe/$nothing renderProfile :: Maybe User -> String renderProfile muser = renderHtml [shamlet| $maybe user <- muser
Welcome back, #{userName user}! $nothing
Please log in. |] ``` -------------------------------- ### CSS Utilities with Lucius in Haskell Source: https://context7.com/yesodweb/shakespeare/llms.txt Illustrates the use of Lucius for type-safe CSS generation in Haskell. It showcases built-in color types and functions for creating size-based CSS properties, ensuring type correctness and preventing common CSS errors at compile time. ```haskell #!/usr/bin/env runhaskell {-# LANGUAGE QuasiQuotes #-} import Text.Lucius -- Built-in Color type -- Color constructor takes RGB values (0-255) styles :: CssUrl url styles = [lucius| .success { color: #{colorRed}; /* #F00 */ background: #{colorBlack}; /* #000 */ border-color: #{Color 127 100 5}; /* #7F6405 */ } | ] -- Size utilities sizedStyles :: CssUrl url sizedStyles = [lucius| .box { width: #{mkSize 100 "px"}; height: #{absoluteSize 50 Centimeter}; padding: #{EmSize 2}; margin: #{percentageSize 10}; font-size: #{PixelSize 16}; } | ] -- Available size types: -- PixelSize Int -> "16px" -- EmSize Double -> "1.5em" -- ExSize Double -> "2ex" -- PercentageSize Int -> "50%" -- AbsoluteSize Double AbsoluteUnit -> "10cm" -- AbsoluteUnit = Centimeter | Millimeter | Inch | Point | Pica ``` -------------------------------- ### Hamlet Loops: $forall Source: https://context7.com/yesodweb/shakespeare/llms.txt Generates an unordered list of user names and ages using the $forall directive for iteration. Each user in the input list is rendered as a list item. ```haskell -- Looping with $forall renderUsers :: [User] -> String renderUsers users = renderHtml [shamlet|