More Simple HStringTemplate Examples

Today I pushed a few changes to the repo so that it builds properly with GHC 6.8. Beyond that, I realized, with a little prompting, that I foolishly hadn’t exported a decent function to query groups of StringTemplates. Additionally, ac on #haskell (the friendliest programming IRC channel this side of the millennium) asked for a simple function that generalized setAttribute to set a whole bunch of attributes at once. The following simple example shows the use of groups, the setManyAttrib function, and how to globally insert options for a single or multiple templates to boot.

> let foo = newSTMP "foo" :: StringTemplate String
> let bar = newSTMP "bar $foo()$" :: StringTemplate String
> toString foo
> toString bar
"bar No Template Found for: foo"
> let grp = groupStringTemplates [("foo",foo),("bar",bar)]
> toString <$> getStringTemplate "foo" grp
Just "foo"
> toString <$> getStringTemplate "fbar" grp
> toString <$> getStringTemplate "bar" grp
Just "bar foo"
> let baz = optInsertTmpl [("separator","; ")] $ newSTMP "$a$ $b$ $c$" :: StringTemplate String
> toString $ setManyAttrib [("a","first"),("b","second"),("c","third")] $ baz
"first second third"
> toString $ setAttribute "a" ([1..4]::[Int]) $ baz
"1; 2; 3; 4 "
> let grp2 = groupStringTemplates [("foo",foo),("bar",bar),("baz",baz)]
> fmap toString $ getStringTemplate "baz" . optInsertGroup [("null","nothing")] $ grp2
Just "nothing nothing nothing"

I still haven’t even touched all the nice bits of the StringTemplate grammar, particularly its recursive constructs. But that’s enough for now.



  1. Ram said

    This is great!! i know its a stretch but what would help users like me is a complete example of how to create a web app etc.. I recently tried the 20 minute wiki tutorial with turbogears. it is awesome, but i did not like the templating language and began looking at ways to use string template there.

    From my side , i can assure you that if even a draft of such a tutorial is putup, i will help review it, and will ofcourse definitely try it and learn from it.


  2. thomas hartman said

    toString getStringTemplate “foo” grp

    seems like this should be…

    toString (maybe (error “template error”) id (getStringTemplate “bar” grp) )

    perhaps the library has changed? I think I actually like the first way better…

    >ghc-pkg list | grep -i hstringtemplate

  3. sclv said

    The library hasn’t changed actually, it was just that wordpress ate some of the applicative operators above — now fixed. Thanks for the catch!

  4. more usage examples of hstringtemplate here:

    feel free to incorporate this into the documentation if you find it useful.

RSS feed for comments on this post · TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: