hastache-0.3.3: Haskell implementation of Mustache templates

Safe Haskell | None

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.ByteString.Lazy as LZ

    main = do
        res <- hastacheStr defaultConfig (encodeStr template)
            (mkStrContext context)
        LZ.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:

    import Text.Hastache
    import Text.Hastache.Context
    import qualified Data.ByteString.Lazy as LZ
    import Data.Data
    import Data.Generics

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

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

Synopsis

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

# Documentation

hastacheStr Source

Arguments

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

Render Hastache template from ByteString

hastacheFile Source

Arguments

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

Render Hastache template from file

hastacheStrBuilder Source

Arguments

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

Render Hastache template from ByteString

hastacheFileBuilder Source

Arguments

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

Render Hastache template from file

type MuContext mSource

Arguments

= ByteString | Variable name
-> 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 (ByteString -> a) |
forall a . MuVar a => MuLambdaM (ByteString -> m a) |
MuNothing |

Instances

Show (MuType m) |

data MuConfig Source

Constructors

MuConfig |

Fields

muEscapeFunc :: ByteString -> ByteString

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

muTemplateFileDir :: Maybe FilePath

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

muTemplateFileExt :: Maybe String

  Partial template files extension

class Show a => MuVar a where Source

Methods

toLByteString :: a -> ByteString Source

Convert to Lazy ByteString

isEmpty :: a -> Bool Source

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

Instances

MuVar Char |
MuVar Double |
MuVar Float |
MuVar Int |
MuVar Int8 |
MuVar Int16 |
MuVar Int32 |
MuVar Int64 |
MuVar Integer |
MuVar Word |
MuVar Word8 |
MuVar Word16 |
MuVar Word32 |
MuVar Word64 |
MuVar Text |
MuVar Text |
MuVar ByteString |
MuVar ByteString |
MuVar [Char] |
MuVar a => MuVar [a] |

htmlEscape :: ByteString -> ByteString Source

Escape HTML symbols

emptyEscape :: ByteString -> ByteString Source

No escape

defaultConfig :: MuConfig Source

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

encodeStr :: String -> ByteString Source

Convert String to UTF-8 Bytestring

encodeStrLBS :: String -> ByteString Source

Convert String to UTF-8 Lazy Bytestring

decodeStr :: ByteString -> String Source

Convert UTF-8 Bytestring to String

decodeStrLBS :: ByteString -> String Source

Convert UTF-8 Lazy Bytestring to String