1. happs intro
  2. the missing happs documentation
  3. getting started with happs
  4. prerequisites
  5. cabal install me
  6. main
  7. url handling
  8. templates
  9. stringtemplate basics
  10. debugging
  11. form data: get and post
  12. form data: file uploads
  13. cookies
  14. introduction to macid
  15. using macid safely
  16. macid dummy data
  17. macid updates and queries
  18. changing the data model
  19. macid stress test
  20. limitations of macid
  21. foreign characters
  22. cron jobs
  23. thanks
  24. appendix (floundering in ghci)

StringTemplate Basics

In the previous section we rendered a template in ghci using the following command

*Main Misc View Text.StringTemplate> do templates <- directoryGroup "templates" ; writeFile "output.html" ( renderTemplateGroup templates [] "templates-dont-repeat-yourself" )

Let's look more carefully at these functions.

  • The directoryGroup function reads in all *.st type files in a directory, and returns an IO STGroup value, which is basically a group of StringTemplates.
    *Main Misc View Text.StringTemplate> :t (directoryGroup :: String -> IO (STGroup String))
    The actual type of directoryGroup is a little less concrete than the above, and uses type classes.
    Our :t command gives directoryGroup 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.