hastache-0.6.1: Haskell implementation of Mustache templates

Safe Haskell | None
Language | Haskell98

Text.Hastache

Description

Haskell implementation of Mustache templates

See homepage for examples of usage: http://github.com/lymar/hastache

Simplest example:

    import           Text.Hastache
    import           Text.Hastache.Context
    import qualified Data.Text.Lazy.IO as TL

    main = do
        res <- hastacheStr defaultConfig (encodeStr template)
            (mkStrContext context)
        TL.putStrLn res
      where
        template = "Hello, {{name}}!\n\nYou have {{unread}} unread messages."
        context "name" = MuVariable "Haskell"
        context "unread" = MuVariable (100 :: Int)

Result:

    Hello, Haskell!

    You have 100 unread messages.

Using Generics:

    {-# LANGUAGE DeriveDataTypeable, OverloadedStrings #-}

    import           Text.Hastache
    import           Text.Hastache.Context
    import qualified Data.Text.Lazy.IO as TL
    import           Data.Data

    data Info = Info {
        name    :: String,
        unread  :: Int
        } deriving (Data, Typeable)

    main = do
        res <- hastacheStr defaultConfig template
            (mkGenericContext inf)
        TL.putStrLn res
      where
        template = "Hello, {{name}}!\n\nYou have {{unread}} unread messages."
        inf = Info "Haskell" 100

Synopsis

- hastacheStr :: MonadIO m => MuConfig m -> Text -> MuContext m -> m Text
- hastacheFile :: MonadIO m => MuConfig m -> FilePath -> MuContext m -> m Text
- hastacheStrBuilder :: MonadIO m => MuConfig m -> Text -> MuContext m -> m Builder
- hastacheFileBuilder :: MonadIO m => MuConfig m -> FilePath -> MuContext m -> m Builder
- type MuContext m = Text -> m (MuType m)
- data MuType m
  - = forall a . MuVar a => MuVariable a
  - | MuList [MuContext m]
  - | MuBool Bool
  - | forall a . MuVar a => MuLambda (Text -> a)
  - | forall a . MuVar a => MuLambdaM (Text -> m a)
  - | MuNothing
- data MuConfig m = MuConfig {
  - muEscapeFunc :: Text -> Text
  - muTemplateFileDir :: Maybe FilePath
  - muTemplateFileExt :: Maybe String
  - muTemplateRead :: FilePath -> m (Maybe Text)
  }
- class Show a => MuVar a where
  - toLText :: a -> Text
  - isEmpty :: a -> Bool
- composeCtx :: Monad m => MuContext m -> MuContext m -> MuContext m
- htmlEscape :: Text -> Text
- emptyEscape :: Text -> Text
- defaultConfig :: MonadIO m => MuConfig m
- encodeStr :: String -> Text
- encodeStrLT :: String -> Text
- decodeStr :: Text -> String
- decodeStrLT :: Text -> String

# Documentation

hastacheStr Source

Arguments

:: MonadIO m |
=> MuConfig m | Configuration
-> Text | Template
-> MuContext m | Context
-> m Text |

Render Hastache template from `Text`

hastacheFile Source

Arguments

:: MonadIO m |
=> MuConfig m | Configuration
-> FilePath | Template file name
-> MuContext m | Context
-> m Text |

Render Hastache template from file

hastacheStrBuilder Source

Arguments

:: MonadIO m |
=> MuConfig m | Configuration
-> Text | Template
-> MuContext m | Context
-> m Builder |

Render Hastache template from `Text`

hastacheFileBuilder Source

Arguments

:: MonadIO m |
=> MuConfig m | Configuration
-> FilePath | Template file name
-> MuContext m | Context
-> m Builder |

Render Hastache template from file

type MuContext m Source

Arguments

= Text | Variable name
-> m (MuType m) | Value

Data for Hastache variable

data MuType m Source

Constructors

forall a . MuVar a => MuVariable a |
MuList [MuContext m] |
MuBool Bool |
forall a . MuVar a => MuLambda (Text -> a) |
forall a . MuVar a => MuLambdaM (Text -> m a) |
MuNothing |

Instances

Show (MuType m) Source |
Monad m => Monoid (MuContext m) Source |

data MuConfig m Source

Constructors

MuConfig |

Fields

muEscapeFunc :: Text -> Text

  Escape function (`htmlEscape`, `emptyEscape` etc.)

muTemplateFileDir :: Maybe FilePath

  Directory for search partial templates (`{{> templateName}}`)

muTemplateFileExt :: Maybe String

  Partial template files extension

muTemplateRead :: FilePath -> m (Maybe Text)

  Template retrieval function. `Nothing` indicates that the template could not be found.

class Show a => MuVar a where Source

Minimal complete definition

toLText

Methods

toLText :: a -> Text Source

Convert to lazy `Text`

isEmpty :: a -> Bool Source

Is empty variable (empty string, zero number etc.)

Instances

MuVar Char Source |
MuVar Double Source |
MuVar Float Source |
MuVar Int Source |
MuVar Int8 Source |
MuVar Int16 Source |
MuVar Int32 Source |
MuVar Int64 Source |
MuVar Integer Source |
MuVar Word Source |
MuVar Word8 Source |
MuVar Word16 Source |
MuVar Word32 Source |
MuVar Word64 Source |
MuVar () Source |
MuVar Version Source |
MuVar ByteString Source |
MuVar ByteString Source |
MuVar Text Source |
MuVar Text Source |
MuVar [Char] Source |
MuVar a => MuVar [a] Source |
MuVar a => MuVar (Maybe a) Source |
(MuVar a, MuVar b) => MuVar (Either a b) Source |

composeCtx :: Monad m => MuContext m -> MuContext m -> MuContext m Source

Left-leaning compoistion of contexts. Given contexts `c1` and `c2`, the behaviour of `(c1 <> c2) x` is following: if `c1 x` produces `MuNothing`, then the result is `c2 x`. Otherwise the result is `c1 x`. Even if `c1 x` is `MuNothing`, the monadic effects of `c1` are still to take place.

htmlEscape :: Text -> Text Source

Escape HTML symbols

emptyEscape :: Text -> Text Source

No escape

defaultConfig :: MonadIO m => MuConfig m Source

Default config: HTML escape function, current directory as template directory, template file extension not specified

encodeStr :: String -> Text Source

Convert `String` to `Text`

encodeStrLT :: String -> Text Source

Convert `String` to Lazy `Text`

decodeStr :: Text -> String Source

Convert `Text` to `String`

decodeStrLT :: Text -> String Source

Convert Lazy `Text` to `String`