| |||||||||||||||||
| |||||||||||||||||
| |||||||||||||||||
Description | |||||||||||||||||
A simple string substitution library that supports "$"-based substitution. Substitution uses the following rules:
Any other apperance of "$" in the string will result in an error being raised. Here is an example of a simple substitution: import qualified Data.ByteString.Lazy.Char8 as B import Text.Template context = Map.fromList . map packPair where packPair (x, y) = (B.pack x, B.pack y) helloTemplate = B.pack "Hello, $name! Want some ${fruit}s?" helloContext = context [("name", "Johan"), ("fruit", "banana")] main = B.putStrLn $ substitute helloTemplate helloContext If you render the same template multiple times it's faster to first convert it to a more efficient representation using template and then rendering it using render. In fact, all that substitute does is to combine these two steps. | |||||||||||||||||
Synopsis | |||||||||||||||||
| |||||||||||||||||
The Template type. | |||||||||||||||||
data Template | |||||||||||||||||
| |||||||||||||||||
The Context type. | |||||||||||||||||
type Context = Map ByteString ByteString | |||||||||||||||||
A mapping with keys that match the placeholders in the template. | |||||||||||||||||
Basic interface | |||||||||||||||||
template :: ByteString -> Template | |||||||||||||||||
Creates a template from a template string. | |||||||||||||||||
render :: Template -> Context -> ByteString | |||||||||||||||||
Performs the template substitution, returning a new ByteString. If a key is not found in the context an error is raised. | |||||||||||||||||
substitute :: ByteString -> Context -> ByteString | |||||||||||||||||
Performs the template substitution, returning a new ByteString. Note that substitute tmpl ctx == render (template tmpl) ctx If a key is not found in the context an error is raised. | |||||||||||||||||
showTemplate :: Template -> ByteString | |||||||||||||||||
Shows the template string. | |||||||||||||||||
I/O with Templates | |||||||||||||||||
Files | |||||||||||||||||
readTemplate :: FilePath -> IO Template | |||||||||||||||||
Reads a template from a file lazily. Use 'text mode' on Windows to interpret newlines | |||||||||||||||||
renderToFile :: FilePath -> Template -> Context -> IO () | |||||||||||||||||
Renders and writes a template to a file. This is more efficient than first rendering the template to a ByteString and then writing it to a file using writeFile. | |||||||||||||||||
I/O with Handles | |||||||||||||||||
hRender :: Handle -> Template -> Context -> IO () | |||||||||||||||||
Renders and writes a template to a Handle. This is more efficient than first rendering the template to a ByteString and then writing it to a Handle using hPutStr. | |||||||||||||||||
Produced by Haddock version 0.8 |