Using output from a compiled splice in another compiled splice.

60 views
Skip to first unread message

Renzo Carbonara

unread,
Apr 1, 2013, 12:06:10 PM4/1/13
to snap_fr...@googlegroups.com
Hello Snap,

I'm new here and I have a question for you.

Suppose I have two *compiled* splices, one bound to `list-names`
that repeatedly runs its children with `name` and `age` bound to
splices that render some text, and a splice named `greet` that
renders some other text based on the value of a given attribute.

I want to write something this:

<ul>
<li><greet who="Hardcoded"/></li>
<list-names>
<li><greet who="Mr. ${name}"/> You are <age/> years old.</li>
</list-names>
</ul>

And have it rendered as:

<ul>
<li>Hello Hardcoded!</li>
<li>Hello Bob! You are 20 years old.</li>
<li>Hello Jon! You are 70 years old.</li>
</ul>

But it doesn't work, insted I get something like:

<ul>
<li>Hello Hardcoded!</li>
<li>Hello Mr. ${name}! You are 20 years old.</li>
<li>Hello Mr. ${name}! You are 70 years old.</li>
</ul>

I can see that the problem might be related to the fact that `name`
is not bound at the time the `greet` splice is compiled. ¿Can I
modify my compiled splice so that it resolves that value during
runtime, or is it impossible using compiled splices? I feel like I'm
misunderstanding something really basic here.

Here's the code for my splices:

getPeople :: Monad m => m [(T.Text, Int)]
getPeople = return [("Bob", 20), ("Jon", 70)]
-- ^suppose these values, here hardcoded, can only be
-- obtained during runtime

listNamesSplice :: Monad m => C.Splice m
listNamesSplice = do
C.manyWithSplices C.runChildren splices getPeople
where
splices = C.pureSplices . C.textSplices $
[ ("name", fst)
, ("age", T.pack . show . snd)
]

greetSplice :: Monad m => C.Splice m
greetSplice = do
input <- getParamNode
let Just who = X.getAttribute "who" input
greetings = return $ "Hello " <> who <> "!"
return $ C.yieldRuntimeText greetings

I then add those splices to my `HeistConfig`'s `hcCompiledSplices` as:

[ ("list-names", listNamesSplice), ("greet", greetSplice) ]


Thanks for your precious time.


Regards,

Renzo Carbonara.

Renzo Carbonara

unread,
Apr 1, 2013, 12:12:21 PM4/1/13
to snap_framework
On Mon, Apr 1, 2013 at 1:06 PM, Renzo Carbonara <gnuk...@gmail.com> wrote:
> And have it rendered as:
>
> <ul>
> <li>Hello Hardcoded!</li>
> <li>Hello Bob! You are 20 years old.</li>
> <li>Hello Jon! You are 70 years old.</li>
> </ul>

I made a mistake, I forgot the “Mr. ” text, I meant:

<ul>
<li>Hello Hardcoded!</li>
<li>Hello Mr. Bob! You are 20 years old.</li>
<li>Hello Mr. Jon! You are 70 years old.</li>
</ul>


Regards,

Renzo Carbonara.

MightyByte

unread,
Apr 1, 2013, 12:22:57 PM4/1/13
to snap_fr...@googlegroups.com
In greetSplice, try calling runAttributesRaw (http://hackage.haskell.org/packages/archive/heist/0.11.1/doc/html/Heist-Compiled.html#v:runAttributesRaw) on your input attributes before using them.



--

---
You received this message because you are subscribed to the Google Groups "Snap Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to snap_framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Renzo Carbonara

unread,
Apr 1, 2013, 1:20:29 PM4/1/13
to snap_fr...@googlegroups.com
Thanks for your response.

I just tried `runAttributesRaw` but the result is still the same, I'm
not sure if I'm making it right though, since I couldn't find much
related documentation nor examples. Here's the new code:

greetSplice :: Monad m => C.Splice m
greetSplice = do
rattrs <- C.runAttributesRaw . X.elementAttrs =<< getParamNode
return . C.yieldRuntimeText $ do
Just who <- lookup "who" `liftM` rattrs
return $ "Hello " <> who <> "!"

¿What am I missing?


Regards,

Renzo Carbonara.

Renzo Carbonara

unread,
Apr 3, 2013, 9:28:59 AM4/3/13
to snap_fr...@googlegroups.com
I haven't yet been able to solve this.

The following interpreted splices render as expected. So, rephrasing my question: ¿How can I reproduce the following behaviour using compiled splices?

    getPeople :: Handler b App [(T.Text,Int)]
    getPeople = return [("Bob",20), ("Jon",70)]
                   -- ^suppose these values, here hardcoded, can only be 
                   --  obtained during runtime 

    listNamesSpliceI :: I.Splice (Handler b App)
    listNamesSpliceI = I.mapSplices sp =<< lift getPeople
      where sp (name,age) = I.runChildrenWithText
                 [("name", name), ("age" , T.pack . show $ age)]

    greetSpliceI :: Monad m => I.Splice m
    greetSpliceI = do
        Just who <- X.getAttribute "who" `liftM` getParamNode
        return [X.TextNode $ "Hello " <> who <> "!"]


I then add those splices to my `HeistConfig`'s `hcInterpretedSplices` as: 

     [ ("list-names", listNamesSpliceI), ("greet", greetSpliceI) ] 


The template is the same as before:

    <ul>
      <li><greet who="Hardcoded"/></li>
      <list-names>
        <li><greet who="Mr. ${name}"/> You are <age/> years old.</li>
      </list-names>
    </ul>


And the result, as expected:

    <ul>
        <li>Hello Hardcoded!</li>
        <li>Hello Mr. Bob! You are 20 years old.</li>
        <li>Hello Mr. Jon! You are 70 years old.</li>
    </ul>

I'm using Heist-0.11.1.


Regards,

Renzo.
Reply all
Reply to author
Forward
0 new messages