using heist for templating emails - and ensuing confusion

13 views
Skip to first unread message

Sönke Hahn

unread,
Feb 3, 2012, 8:54:48 AM2/3/12
to sn...@snapframework.com
Hi all!

I started using snap some days ago and I really start to like it. So first
off: Many thanks to the people working on it.

I have a problem, though: I want to use the heist snaplet to render
templates for sending emails. For websites, I would use 'renderWithSplices'
or something, but how do get heist to render a template and return the
result as a String?

I looked at 'Text.Templating.Heist.renderWithArgs' and thought I could
combine it with 'Snap.Snaplet.Heist.withHeistTS', but I got a little
confused in the process. (In Snap.Snaplet.Heist and Text.Templating.Heist
there sure are a lot of different types for states and monads -- and
therefore a lot of lifting functions. I found it hard to get an overview.)

Just now, I managed to write a function, that does, what I want:

returnRenderedTemplate :: HasHeist a =>
ByteString -> [(T.Text, T.Text)] -> Handler a a ByteString
returnRenderedTemplate templateName args = do
m <- join $ withHeistTS $ \ heistState ->
renderWithArgs args heistState templateName
return $ maybe
(error ("template not found: " ++ toString templateName))
(toByteString . fst)
m

, but that seems quite complicated. Am I missing some easy way to achieve
this? If not, should something like this be included in Snap.Snaplet.Heist?
(Should I write a patch?)

Thanks,
Sönke


MightyByte

unread,
Feb 3, 2012, 9:41:00 AM2/3/12
to Sönke Hahn, sn...@snapframework.com
Hi Sönke,

The Heist snaplet was designed to make it easy to use Heist with the
Snap web server to generate HTTP responses. Your use case of
generating email templates is not really within the scope of that
goal. If our API made it impossible to do this, then I would
definitely agree that there's a problem. But while it might be more
complicated than you would like, your returnRenderedTemplate looks
close to what I expected. Since this is not really the main use case
for the Heist snaplet, and since this is the first time anyone has
asked for a feature like this, I don't think it is something that
should go in the Snap.Snaplet.Heist API.

Thanks for the feedback.

Doug Beardsley

Ozgun Ataman

unread,
Feb 3, 2012, 10:26:53 AM2/3/12
to MightyByte, Sönke Hahn, sn...@snapframework.com
Whether this becomes a part of the Snaplet.Heist API or some other module, I too think that it would be cool to have support for emailing using Heist templates. Step 1 would be something to the effect of your function there (probably broken down to primitives, with more general splice versions, etc.), followed by some integration with an emailing library. I know several other popular frameworks do this - Rails being the one I am most familiar with and they use erb templates for both page rendering and emailing.

In response to your Handler function there - I agree with Doug that it is not all that bad in terms of length/complexity. Heist types definitely take some getting used to, but it is not that bad once you're there.

Oz

Sönke Hahn

unread,
Feb 3, 2012, 10:33:40 AM2/3/12
to MightyByte, sn...@snapframework.com
Hi!

MightyByte wrote:
> But while it might be more
> complicated than you would like, your returnRenderedTemplate looks
> close to what I expected.

Oh, ok. Very good, then.

> Since this is not really the main use case
> for the Heist snaplet, and since this is the first time anyone has
> asked for a feature like this, I don't think it is something that
> should go in the Snap.Snaplet.Heist API.

That's a good point.

> Thanks for the feedback.

Thanks for your helpful answer.

Cheers,
Sönke

signature.asc

Sönke Hahn

unread,
Feb 3, 2012, 10:42:46 AM2/3/12
to Ozgun Ataman, MightyByte, sn...@snapframework.com
On 02/03/2012 04:26 PM, Ozgun Ataman wrote:
> Whether this becomes a part of the Snaplet.Heist API or some other
> module, I too think that it would be cool to have support for emailing
> using Heist templates.

There could be a whole other package on hackage that integrates with the
heist snaplet. Maybe that would be best.

> Step 1 would be something to the effect of your
> function there (probably broken down to primitives, with more general
> splice versions, etc.),

Yes, I thought about more general types for splices. I don't need that
right now, though.

> followed by some integration with an emailing
> library.

I am using Network.Email.Sendmail from MissingH right now. Which works,
but it's rather hackish inside, I guess. 'email' (from hackage) seems a
bit bitrotten.

> I know several other popular frameworks do this - Rails being
> the one I am most familiar with and they use erb templates for both page
> rendering and emailing.

Yes. I remember that from django, I think.

> In response to your Handler function there - I agree with Doug that it
> is not all that bad in terms of length/complexity. Heist types
> definitely take some getting used to, but it is not that bad once you're
> there.

Looking forward to that. :)

Sönke

signature.asc

MightyByte

unread,
Feb 3, 2012, 11:06:56 AM2/3/12
to sh...@cs.tu-berlin.de, Ozgun Ataman, sn...@snapframework.com

Looks like you're doing pretty well already. The main thing to
remember is that the heist package itself provides templating
independent of any web framework. The Heist snaplet included with the
snap package handles boilerplate and glue associated with using Heist
with Snap. The tool for bridging the gap between the two in monadic
code is the liftHeist function.

Reply all
Reply to author
Forward
0 new messages