Skip to content
Browse files

examples

  • Loading branch information...
1 parent fe881e9 commit faad5efdbdd9ce83ae9a1b1191f25c9963dbb4ac @lymar lymar committed Nov 14, 2011
View
53 example/HastacheContextExample.hs
@@ -0,0 +1,53 @@
+#!/usr/local/bin/runhaskell
+{-# LANGUAGE DeriveDataTypeable #-}
+import Text.Hastache
+import Text.Hastache.Context
+import qualified Data.ByteString as B
+import qualified Data.ByteString.Lazy as LZ
+import Data.Data
+import Data.Generics
+import Data.Char
+
+data InternalData = InternalData {
+ someField :: String,
+ anotherField :: Int
+ } deriving (Data, Typeable, Show)
+
+data Example = Example {
+ stringField :: String,
+ intField :: Int,
+ dataField :: InternalData,
+ simpleListField :: [String],
+ dataListField :: [InternalData],
+ stringFunc :: String -> String,
+ byteStringFunc :: B.ByteString -> B.ByteString,
+ monadicStringFunc :: String -> IO String,
+ monadicByteStringFunc :: B.ByteString -> IO B.ByteString
+ } deriving (Data, Typeable)
+
+example = hastacheStr defaultConfig (encodeStr template)
+ (mkGenericContext context)
+ where
+ template = concat $ map (++ "\n") [
+ "string: {{stringField}}",
+ "int: {{intField}}",
+ "data: {{dataField.someField}}, {{dataField.anotherField}}",
+ "data: {{#dataField}}{{someField}}, {{anotherField}}{{/dataField}}",
+ "simple list: {{#simpleListField}}{{.}} {{/simpleListField}}",
+ "data list:",
+ "{{#dataListField}}",
+ " * {{someField}}, {{anotherField}}. top level var: {{intField}}",
+ "{{/dataListField}}",
+ "{{#stringFunc}}upper{{/stringFunc}}",
+ "{{#byteStringFunc}}reverse{{/byteStringFunc}}",
+ "{{#monadicStringFunc}}upper (monadic){{/monadicStringFunc}}",
+ "{{#monadicByteStringFunc}}reverse (monadic){{/monadicByteStringFunc}}"]
+ context = Example { stringField = "string value", intField = 1,
+ dataField = InternalData "val" 123, simpleListField = ["a","b","c"],
+ dataListField = [InternalData "aaa" 1, InternalData "bbb" 2],
+ stringFunc = map toUpper,
+ byteStringFunc = B.reverse,
+ monadicStringFunc = return . map toUpper,
+ monadicByteStringFunc = return . B.reverse }
+
+main = example >>= LZ.putStrLn
View
12 example/function.hs
@@ -0,0 +1,12 @@
+#!/usr/local/bin/runhaskell
+import Text.Hastache
+import Text.Hastache.Context
+import qualified Data.ByteString.Lazy as LZ
+
+main = hastacheStr defaultConfig (encodeStr template) (mkStrContext context)
+ >>= LZ.putStrLn
+
+-- begin example
+template = "Hello, {{#reverse}}world{{/reverse}}!"
+
+context "reverse" = MuLambda (reverse . decodeStr)
View
47 example/genericsBig.hs
@@ -0,0 +1,47 @@
+#!/usr/local/bin/runhaskell
+{-# LANGUAGE DeriveDataTypeable #-}
+import Text.Hastache
+import Text.Hastache.Context
+import qualified Data.ByteString.Lazy as LZ
+import Data.Data
+import Data.Generics
+
+main = hastacheStr defaultConfig (encodeStr template) context >>= LZ.putStrLn
+
+-- begin example
+data Book = Book {
+ title :: String,
+ publicationYear :: Integer
+ } deriving (Data, Typeable)
+
+data Life = Life {
+ born :: Integer,
+ died :: Integer
+ } deriving (Data, Typeable)
+
+data Writer = Writer {
+ name :: String,
+ life :: Life,
+ books :: [Book]
+ } deriving (Data, Typeable)
+
+template = concat [
+ "Name: {{name}} ({{life.born}} - {{life.died}})\n",
+ "{{#life}}\n",
+ "Born: {{born}}\n",
+ "Died: {{died}}\n",
+ "{{/life}}\n",
+ "Bibliography:\n",
+ "{{#books}}\n",
+ " {{title}} ({{publicationYear}})\n",
+ "{{/books}}\n"
+ ]
+
+context = mkGenericContext Writer {
+ name = "Mikhail Bulgakov",
+ life = Life 1891 1940,
+ books = [
+ Book "Heart of a Dog" 1987,
+ Book "Notes of a country doctor" 1926,
+ Book "The Master and Margarita" 1967]
+ }
View
18 example/lists.hs
@@ -0,0 +1,18 @@
+#!/usr/local/bin/runhaskell
+import Text.Hastache
+import Text.Hastache.Context
+import qualified Data.ByteString.Lazy as LZ
+
+main = hastacheStr defaultConfig (encodeStr template) (mkStrContext context)
+ >>= LZ.putStrLn
+
+-- begin example
+template = concat [
+ "{{#heroes}}\n",
+ "* {{name}} \n",
+ "{{/heroes}}\n"]
+
+context "heroes" = MuList $ map (mkStrContext . mkListContext)
+ ["Nameless","Long Sky","Flying Snow","Broken Sword","Qin Shi Huang"]
+ where
+ mkListContext name = \"name" -> MuVariable name
View
21 example/listsGeneric.2.hs
@@ -0,0 +1,21 @@
+#!/usr/local/bin/runhaskell
+{-# LANGUAGE DeriveDataTypeable #-}
+import Text.Hastache
+import Text.Hastache.Context
+import qualified Data.ByteString.Lazy as LZ
+import Data.Data
+import Data.Generics
+
+main = hastacheStr defaultConfig (encodeStr template) context
+ >>= LZ.putStrLn
+
+-- begin example
+data Heroes = Heroes { heroes :: [String] } deriving (Data, Typeable)
+
+template = concat [
+ "{{#heroes}}\n",
+ "* {{.}} \n",
+ "{{/heroes}}\n"]
+
+context = mkGenericContext $ Heroes ["Nameless","Long Sky","Flying Snow",
+ "Broken Sword","Qin Shi Huang"]
View
22 example/listsGeneric.hs
@@ -0,0 +1,22 @@
+#!/usr/local/bin/runhaskell
+{-# LANGUAGE DeriveDataTypeable #-}
+import Text.Hastache
+import Text.Hastache.Context
+import qualified Data.ByteString.Lazy as LZ
+import Data.Data
+import Data.Generics
+
+main = hastacheStr defaultConfig (encodeStr template) context
+ >>= LZ.putStrLn
+
+-- begin example
+data Hero = Hero { name :: String } deriving (Data, Typeable)
+data Heroes = Heroes { heroes :: [Hero] } deriving (Data, Typeable)
+
+template = concat [
+ "{{#heroes}}\n",
+ "* {{name}} \n",
+ "{{/heroes}}\n"]
+
+context = mkGenericContext $ Heroes $ map Hero ["Nameless","Long Sky",
+ "Flying Snow","Broken Sword","Qin Shi Huang"]
View
26 example/monadicFunction.hs
@@ -0,0 +1,26 @@
+#!/usr/local/bin/runhaskell
+-- begin example
+{-# LANGUAGE FlexibleContexts #-}
+import Text.Hastache
+import Text.Hastache.Context
+import qualified Data.ByteString.Lazy as LZ
+import Control.Monad.State
+
+main = run >>= LZ.putStrLn
+
+run = evalStateT stateFunc ""
+
+stateFunc :: StateT String IO LZ.ByteString
+stateFunc =
+ hastacheStr defaultConfig (encodeStr template) (mkStrContext context)
+
+template = "{{#arg}}aaa{{/arg}} {{#arg}}bbb{{/arg}} {{#arg}}ccc{{/arg}}"
+
+context "arg" = MuLambdaM $ arg . decodeStr
+
+arg :: MonadState String m => String -> m String
+arg a = do
+ v <- get
+ let nv = v ++ a
+ put nv
+ return nv
View
13 example/variables.hs
@@ -0,0 +1,13 @@
+#!/usr/local/bin/runhaskell
+-- begin example
+import Text.Hastache
+import Text.Hastache.Context
+import qualified Data.ByteString.Lazy as LZ
+
+main = hastacheStr defaultConfig (encodeStr template) (mkStrContext context)
+ >>= LZ.putStrLn
+
+template = "Hello, {{name}}!\n\nYou have {{unread}} unread messages."
+
+context "name" = MuVariable "Haskell"
+context "unread" = MuVariable (100 :: Int)
View
19 example/variablesGeneric.hs
@@ -0,0 +1,19 @@
+#!/usr/local/bin/runhaskell
+-- begin example
+{-# LANGUAGE DeriveDataTypeable #-}
+import Text.Hastache
+import Text.Hastache.Context
+import qualified Data.ByteString.Lazy as LZ
+import Data.Data
+import Data.Generics
+
+main = hastacheStr defaultConfig (encodeStr template) context
+ >>= LZ.putStrLn
+
+data Info = Info {
+ name :: String,
+ unread :: Int
+ } deriving (Data, Typeable)
+
+template = "Hello, {{name}}!\n\nYou have {{unread}} unread messages."
+context = mkGenericContext $ Info "Haskell" 100

0 comments on commit faad5ef

Please sign in to comment.
Something went wrong with that request. Please try again.