### 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
_{UserCount 42}