Yes, I believe this implementation will cause the files to be read
multiple times. There are several ways you can deal with this. Both
of them involve using code that was added to the Heist git repository
within the last 3 days. Hopefully within the next week or so this
code should be available from hackage.
The first option (which has been around for awhile) is to use the static tag.
<static>
<p><name/><email/></p>
<p><name/><email/></p>
</static>
This ensures that the contents of the static tag are only evaluated
once when you start your application. Obviously this isn't
appropriate for dynamic data. In that case you could use the <cache>
tag in the same way. I added code implementing this tag a few days
ago. <cache ttl="5d"> will only reload the contents if it's been more
than 5 days since it was last reloaded. See the code comments in
Text.Templating.Heist.Splices.Cache for more detail.
Neither of these two solutions prevent the files from being read
multiple times on the first load. To do that, you need to formulate
your splices a little differently. I would suggest something as
follows.
<withContactInfo>
<p><name/><email/></p>
<p><name/><email/></p>
</withContactInfo>
withContactInfoSplice :: Splice Application
withContactInfoSplice = do
name <- liftIO getName
email <- liftIO getEmail
runChildrenWithText [("name", name), ("email", email)]
runChildrenWithText is a new function committed on Friday that run's
the splice's child nodes in a new "environment" that has the name and
email splices bound. I would guess that this "inversion of control"
pattern will end up being one of the most common patterns of using
splices. It keeps all of the business logic in the code and all of
the views in the templates. In my opinion it actually makes coding
HTML somewhat enjoyable (syntax objections of the lisp crowd
notwithstanding).
On Mon, Mar 21, 2011 at 11:46 AM, LIN Sumang <[email protected]> wrote:
> Please consider the scenario below:
> A template like this
>
> <p> <NAME/> <EMAIL/> ............ </p>
> ....
>
> <p> <NAME/> <EMAIL/> ............</p>
>
> ......
>
> NAME & EMAIL are splices implemented in Haskell source like
> name = do read fileA
> parse & get the name attribute
>
> email = do read fileA
> parse & get the email attribute
>
> So, when Heist render the template, how many times we need to read fileA?
> If it need four, how to ease the problem?
>
> _______________________________________________
> Snap mailing list
> [email protected]
> http://mailman-mail5.webfaction.com/listinfo/snap
>
>
_______________________________________________
Snap mailing list
[email protected]
http://mailman-mail5.webfaction.com/listinfo/snap