StringTemplate Basics
In the previous section we rendered a template in ghci using the following command
*Main Misc View Text.StringTemplate> do templates <- directoryGroupSafer "templates" ; writeFile "output.html" ( renderTemplateGroup templates [] "templates-dont-repeat-yourself" )
Let's look more carefully at these functions.
- The directoryGroupSafer function reads in all *.st type files in a directory,
and returns an IO STGroup value, which is basically a group
of StringTemplates. It is a modification of the directoryGroup command that comes with Text.StringTemplate package,
which ignores email backup files-type files with punctuation, that cause annoyance
when using HStringTemplate functions.
*Main Misc View Text.StringTemplate> :t (directoryGroupSafer :: String -> IO (STGroup String))
The actual type of directoryGroupSafer is a little less concrete than the above, and uses type classes.
Our :t command gives directoryGroupSafer a concrete type, and since there's no error, we know it typechecks.
- renderTemplateGroup takes an STGroup, some template key/value pairs, and a named template, and renders
the template if it is found in the STGroup. If it is not found, an error is returned.
*Main Misc View Text.StringTemplate> :t renderTemplateGroup
renderTemplateGroup :: STGroup String -> [(String, String)] -> String -> String
Next, let's look at a slightly more involved example of StringTemplate usage than we've seen so far.
Try to gain an understanding of the most important features in the StringTemplate system
by getting a sense of how the my-favorite-animal page got generated.
When you're done doing that, you should have enough StringTemplate knowledge to shoot yourself in the foot :)
For a more in depth look at StringTemplate, see the following:
And now, on to macid.