On Mon, Jun 28, 2010 at 6:13 AM, Kevin Jardine <[email protected]> wrote:
> I would like to add a list of nodes to a template state.
>
> Specifically, I have a set of language strings that *may* contain embedded
> nodes. This is for creating multilingual sites.
>
> The file format being parsed is of the format:
>
> <lang id="en">
> <s set="mystring">
> <t id="title">This is an example title</t>
> <t id="example">This is an example string with an embedded <mytitle /></t>
> ...
> many more string definitions would go here
> ...
> </s>
> </lang>
>
> The idea is that parsing this file using renderTemplate calls a splice
> function that generates templates of the name "lang:en:mystring:example".
This was certainly not one of the anticipated uses of renderTemplate. :)
> These templates should be saved in the template state and then be used during
> a following renderTemplate that renders a page shell.
>
> The page shell or subsequently called splices would contain nodes of the form:
>
> <l mytitle="Hello">mystring:template</l> which would look up the current
> language and return the appropriate template node, eg.
> <lang:en:mystring:example mytitle="Hello"/>
>
> which would return the previously defined template. I would introduce a few
> optimisations, but that is the basic idea.
>
> So in essence, I am writing a splice function that has the side effect of
> generating new templates that are used later on.
>
> I am abstracting the new template by using code like this (for the "t" tag):
>
> ts <- getTS
> stopRecursion
> input <- getParamNode
> let mynodes = getChildren input
> putTS $ addTemplateFunction "lang:en:mystring:example" mynodes
>
> The addTemplate function appears to be ideal for addTemplateFunction, except
> that it takes an internal template instead of a list of nodes.
>
> I notice that there is a toInternalTemplate function but it is not public and
> it is not clear to me how the docType parameter works there in any case.
This is a bug. When I refactored the code to use InternalTemplate I
didn't see that addTemplate was exported with InternalTemplate as a
parameter. It's fixed now.
> Is there a more straightforward way to do this?
>
> Basically I am trying to define a large number of templates within one file
> rather than using the default Heist approach of one template per file, which
> is inefficient for defining language strings.
My current recommendation would be to use a different approach. A
post-run hook seems like the simplest way to do this. You might do
something like the following:
postRun :: Monad m => Map String String -> Template -> m Template
postRun stringLangs t = return $ map (mapAllTags translate) t
where
translate str = fromMaybe str $ Map.lookup str stringLangs
...and then use (addPostRunHook postRun) when you initialize your TemplateState.
If you don't want to incur the (small?) performance penalty of the
post-run hook, you could use an on-load hook and then make sure all
your dynamic splices generate strings that are translated correctly.
Does this make sense?
_______________________________________________
Snap mailing list
[email protected]
http://mailman-mail5.webfaction.com/listinfo/snap