1. new in this version
  2. build a web 2.0 app in happstack
  3. why happstack is cool
  4. getting started with happstack
  5. prerequisites
  6. cabal install me
  7. first shot at happstack
  8. url handling
  9. basic HTML inclusion
  10. templates
  11. stringtemplate basics
  12. debugging
  13. form data: get and post
  14. form data: file uploads
  15. cookies
  16. introduction to macid
  17. first steps with macid
  18. scaling with multimaster
  19. using macid safely
  20. macid dummy data
  21. changing the data model
  22. macid stress test
  23. limitations of macid
  24. foreign characters
  25. IxSets
  26. cron jobs
  27. thanks
  28. 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 ([]::[(String,String)]) "templatesdontrepeatyourself" )

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 debugging.