hastache-0.6.0: Haskell implementation of Mustache templates

Safe HaskellNone

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:

import Text.Hastache 
import Text.Hastache.Context 
import qualified Data.Text.Lazy.IO as TL
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) 
    TL.putStrLn res 
  where 
    template = "Hello, {{name}}!\n\nYou have {{unread}} unread messages."
    inf = Info "Haskell" 100

Synopsis

Documentation

hastacheStrSource

Arguments

:: MonadIO m 
=> MuConfig m

Configuration

-> Text

Template

-> MuContext m

Context

-> m Text 

Render Hastache template from Text

hastacheFileSource

Arguments

:: MonadIO m 
=> MuConfig m

Configuration

-> FilePath

Template file name

-> MuContext m

Context

-> m Text 

Render Hastache template from file

hastacheStrBuilderSource

Arguments

:: MonadIO m 
=> MuConfig m

Configuration

-> Text

Template

-> MuContext m

Context

-> m Builder 

Render Hastache template from Text

hastacheFileBuilderSource

Arguments

:: MonadIO m 
=> MuConfig m

Configuration

-> FilePath

Template file name

-> MuContext m

Context

-> m Builder 

Render Hastache template from file

type MuContext mSource

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) 

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 whereSource

Methods

toLText :: a -> TextSource

Convert to Lazy ByteString

isEmpty :: a -> BoolSource

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

htmlEscape :: Text -> TextSource

Escape HTML symbols

emptyEscape :: Text -> TextSource

No escape

defaultConfig :: MonadIO m => MuConfig mSource

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

encodeStr :: String -> TextSource

Convert String to Text

encodeStrLT :: String -> TextSource

Convert String to Lazy Text

decodeStr :: Text -> StringSource

Convert Text to String

decodeStrLT :: Text -> StringSource

Convert Lazy Text to String