Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

more examples in haddock

  • Loading branch information...
commit fe881e9f9f1256591e254e1ab55057d5bc56c03d 1 parent ef3ec12
@lymar lymar authored
Showing with 100 additions and 65 deletions.
  1. +29 −4 Text/Hastache.hs
  2. +71 −61 Text/Hastache/Context.hs
View
33 Text/Hastache.hs
@@ -16,26 +16,51 @@ See homepage for examples of usage: <http://github.com/lymar/hastache>
Simplest example:
@
-module Main where
-
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}}!\"
+ 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
+@
+
-}
module Text.Hastache (
hastacheStr
View
132 Text/Hastache/Context.hs
@@ -37,7 +37,71 @@ mkStrContext f a = decodeStr a ~> f
{- |
Make Hastache context from Data.Data deriving type
+Supported field types:
+
+ * String
+
+ * Char
+
+ * Double
+
+ * Float
+
+ * Int
+
+ * Int8
+
+ * Int16
+
+ * Int32
+
+ * Int64
+
+ * Integer
+
+ * Word
+
+ * Word8
+
+ * Word16
+
+ * Word32
+
+ * Word64
+
+ * Data.ByteString.ByteString
+
+ * Data.ByteString.Lazy.ByteString
+
+ * Data.Text.Text
+
+ * Data.Text.Lazy.Text
+
+ * Bool
+
+ * Data.ByteString.ByteString -> Data.ByteString.ByteString
+
+ * String -> String
+
+ * Data.ByteString.ByteString -> Data.ByteString.Lazy.ByteString
+
+ * MonadIO m => Data.ByteString.ByteString -> m Data.ByteString.ByteString
+
+ * MonadIO m => String -> m String
+
+ * MonadIO m => Data.ByteString.ByteString -> m Data.ByteString.Lazy.ByteString
+
+Example:
+
@
+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
@@ -50,9 +114,9 @@ data Example = Example {
simpleListField :: [String],
dataListField :: [InternalData],
stringFunc :: String -> String,
- byteStringFunc :: Data.ByteString.ByteString -> Data.ByteString.ByteString,
+ byteStringFunc :: B.ByteString -> B.ByteString,
monadicStringFunc :: String -> IO String,
- monadicByteStringFunc :: Data.ByteString.ByteString -> IO Data.ByteString.ByteString
+ monadicByteStringFunc :: B.ByteString -> IO B.ByteString
} deriving (Data, Typeable)
example = hastacheStr defaultConfig (encodeStr template)
@@ -75,12 +139,12 @@ example = hastacheStr defaultConfig (encodeStr template)
context = Example { stringField = \"string value\", intField = 1,
dataField = InternalData \"val\" 123, simpleListField = [\"a\",\"b\",\"c\"],
dataListField = [InternalData \"aaa\" 1, InternalData \"bbb\" 2],
- stringFunc = map Data.Char.toUpper,
- byteStringFunc = Data.ByteString.reverse,
- monadicStringFunc = return . map Data.Char.toUpper,
- monadicByteStringFunc = return . Data.ByteString.reverse }
+ stringFunc = map toUpper,
+ byteStringFunc = B.reverse,
+ monadicStringFunc = return . map toUpper,
+ monadicByteStringFunc = return . B.reverse }
-main = example >>= Data.ByteString.Lazy.putStrLn
+main = example >>= LZ.putStrLn
@
Result:
@@ -100,60 +164,6 @@ UPPER (MONADIC)
)cidanom( esrever
@
-Supported field types:
-
- * String
-
- * Char
-
- * Double
-
- * Float
-
- * Int
-
- * Int8
-
- * Int16
-
- * Int32
-
- * Int64
-
- * Integer
-
- * Word
-
- * Word8
-
- * Word16
-
- * Word32
-
- * Word64
-
- * Data.ByteString.ByteString
-
- * Data.ByteString.Lazy.ByteString
-
- * Data.Text.Text
-
- * Data.Text.Lazy.Text
-
- * Bool
-
- * Data.ByteString.ByteString -> Data.ByteString.ByteString
-
- * String -> String
-
- * Data.ByteString.ByteString -> Data.ByteString.Lazy.ByteString
-
- * MonadIO m => Data.ByteString.ByteString -> m Data.ByteString.ByteString
-
- * MonadIO m => String -> m String
-
- * MonadIO m => Data.ByteString.ByteString -> m Data.ByteString.Lazy.ByteString
-
-}
mkGenericContext :: (Monad m, Data a, Typeable1 m) => a -> MuContext m
mkGenericContext val = toGenTemp val ~> convertGenTempToContext
Please sign in to comment.
Something went wrong with that request. Please try again.