Personal tools

HStringTemplate

From HaskellWiki

(Difference between revisions)
Jump to: navigation, search
 
Line 12: Line 12:
 
== Getting started ==
 
== Getting started ==
   
  +
Assuming you have installed the library, try the following at a GHCi prompt:
  +
  +
  +
<haskell>
  +
Prelude> :m + Text.StringTemplate
  +
Prelude Text.StringTemplate> let t = newSTMP "Hello $name$" :: StringTemplate String
  +
</haskell>
  +
  +
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):
  +
  +
<haskell>
  +
Prelude Text.StringTemplate> render $ setAttribute "name" "Joe" t
  +
"Hello Joe"
  +
</haskell>
  +
  +
Instead of "Joe", we can use anything that has a ToSElem instance
   
 
== Using data structures and generics ==
 
== Using data structures and generics ==
Line 20: Line 38:
   
 
== Supported syntax ==
 
== Supported syntax ==
  +
  +
== Loading templates ==

Revision as of 12:23, 1 July 2009


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.

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

Contents

1 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

2 Using data structures and generics

2.1 Using GenericStandard

2.2 Using GenericWithClass

3 Supported syntax

4 Loading templates