HStringTemplate

From HaskellWiki
Revision as of 21:25, 1 July 2009 by Spookylukey (talk | contribs)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.


HStringTemplate is a Haskell-ish port of the Java StringTemplate library written by Terrence Parr. It can be used for any templating purpose, but is often used for dynamically generated web pages.

For news of HStringTemplate and blog items, see Sterling Clover's blog, and for downloads and API docs see hackage.

Additional helper functions for HStringTemplate can be found in the HStringTemplateHelpers package

This is a stub page, which aims to supplement the API docs with tutorial style documentation.

Getting started

Assuming you have installed the library, try the following at a GHCi prompt:


Prelude> :m + Text.StringTemplate
Prelude Text.StringTemplate> let t = newSTMP "Hello $name$" :: StringTemplate String

This has created a 'String' based StringTemplate. StringTemplates can be based around any 'Stringable' type, allowing you to use ByteString's or any other type if you write the Stringable instance. The template has a single 'hole' in it called 'name', delimited by dollar signs.

We can now fill in the hole using 'setAttribute', and render it to its base type (String in this case):

Prelude Text.StringTemplate> render $ setAttribute "name" "Joe" t
"Hello Joe"

Instead of "Joe", we can use anything that has a ToSElem instance.

There are short cuts for long attribute chains, such as setManyAttributes and renderf


Supported syntax

This section follows http://www.antlr.org/wiki/display/ST/Expressions for structure, adapting as appropriate.

The most common thing in a template besides plain text is a simple named attribute reference such as:

Your email: $email$

When the template is rendered, it will lookup "email" in its environment and convert it to the type of the underlying StringTemplate. Usually this occurs via a conversion to String, but this can be avoided using setNativeAttribute (e.g. if you have a StringTemplate ByteString you can use setNativeAttribute with ByteString objects to avoid the round trip to Strings. This also avoids the encoding function that has been set on the template.

If "email" does not exist in the template environment, the above will render as if "email" was set to the empty string.

The Maybe data structure can be used as the value of an attribute, and will render as the empty string if 'Nothing', otherwise it will render just like the data contained in the 'Just' structure.

If the attribute is a list, the elements are rendered one after the other. To use a separator in between items, use the separator option:

$values; separator=", "$

If this is rendered with "values" set to [1..4] the result will be:

1, 2, 3, 4

If the "values" is set to [Just 1, Nothing, Just 3] the result will be:

1, , 3

This follows from the treatment of Nothing described above. Use catMaybes to remove the 'Nothing's. Or, to emit a special value for each Nothing element in the list, use the null option:

$values; null="-1"; separator=", "$

This would render the previous example as:

1, -1, 3

Note the difference between this and StringTemplate -- the 'null' option and the 'separator' option have a semicolon (;) between them, and not a comma (,). (Is this a bug?)

Using data structures and generics

Using GenericStandard

Using GenericWithClass =

Loading templates

You will probably want to load templates from the file system instead of embedding them into Haskell source files. To do this:

  • Create a directory to store the templates
  • Create a template in it with the extension ".st" e.g "mytemplate.st"
  • Load the template like the following example code:
import Data.ByteString.Lazy (ByteString)
import Text.StringTemplate
import Maybe (fromJust)
 
main = do
  templates <- directoryGroup "/path/to/your/templates/" :: IO (STGroup ByteString)
  let t = fromJust $ getStringTemplate "mytemplate" templates
  print $ render t

If you have IO errors or the template does not exist, you will get an exception — error handling code is left as an excercise for the reader...

The type of directoryGroup result has to be specified, otherwise it does not know what type of StringTemplate to return. We have specified ByteString because the file system really stores byte streams, and we want to load the file uninterpreted (Haskell libraries do not just "do the right thing" with encodings, since in general that is not possible).

Encoder functions

(TODO. NB - can avoid use of encoder function by setting ByteString attributes (?))

Template groups

(TODO. Including strategies for template re-use)