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