Snap Example: Compiled Splice Example

I think I asked a similar question a while ago, but it was not answered due to the unstable API. So I waited for 0.13 to skip. I'm not sure if it is right to bring up a similar question ...?

What is the alternative to interpreted runChildrenWith(Text)and mapSplicescompiled splices in the world? (this combination seems to be the most common) I would appreciate some code examples if possible.

If I understand correctly, we have collected all the bundles of applications, and then add them to heistInit. Can anyone show how to do this?

Should the binding tag be unique for the entire application?

Is there a complete project for binding utilisingnew APIs and compiled splices so I can read and see how to learn?

Thanks.

- UPDATE -

Great answer below. But, unfortunately, some parts (those with lenses) confused me even more. If I understand correctly, this is an easy way to splic a string:

mySplice = "testSplice" ## testSplice
  where testSplice = return $ C.yieldRuntimeText $ do
          return "text to be spliced"

If I need to run a spliced ​​string several times, say in 5 table raws, I would do it like this:

mySplices = C.manyWithSplices C.runChildren mySplice

Is it correct?

I get a bunch of errors trying to add splices to the heist configuration.

addConfig h $ mempty
 {
   hcCompiledSplices = "mySplice" ## mySplice -- or mySplices
 }

Where am I mistaken? Sorry for the slowness.

All I really need (just now to understand) is to splic and display a simple string that I get from the database.

- UPDATE 2 -

Thanks to the huge result of Daniel's answer, I can finally do something.

So far I am using both versions of the code.

, Daniel

stringSplice :: Monad n => C.Splice n
stringSplice = C.manyWithSplices C.runChildren splicefuncs (return ["aa","bb","cc"])
  where
    splicefuncs = "string" ## (C.pureSplice . C.textSplice $ id)

secod

testSplice :: C.Splice (Handler App App)
testSplice = return $ C.yieldRuntimeText $ return "text to be spliced"

(C.pureSplice . C.textSplice $ id)

return $ C.yieldRuntimeText $ return "text to be spliced"

? , ? , .

lib deferMany, , , mapSplices lib. C.manyWithSplices C.runChildren?

+4
1

, , (, , snap init.)

_persons.tpl

<body>
    <person>
        <div>
            <h1><name>dummy name</name></h1>
            <p><age>77</age></p> 
            <p><location>jauja</location></p> 
        </div>
    </person>
</body>

person, name, age location - , .

Snaplet,

data Foo = Foo
    {
        _persons :: [Person]
    }

makeLenses ''Foo

data Person = Person
    {
        _name :: Text
    ,   _age :: Int
    ,   _location :: Text
    }   

makeLenses ''Person

App:

data App = App
    { _heist :: Snaplet (Heist App)
    , _sess :: Snaplet SessionManager
    , _auth :: Snaplet (AuthManager App)
    , _foo :: Snaplet Foo
    }

f <- nestSnaplet "foo" foo $ makeSnaplet "foo" "Foo Snaplet" Nothing $ return $ Foo $ 
        [ Person "Ricardo" 33 "Los Cantones" 
        , Person "Luis" 38 "Montealto" 
        ]

...

return $ App h s a f

, ( view Control.Lens):

personH :: SnapletLens b Foo -> Handler b b [Person] 
personH l = withTop l $ view persons <$> get 

a RuntimeSplice, . RuntimeSplice , , :

personSplice :: Monad n => RuntimeSplice n [Person] -> C.Splice n
personSplice = C.manyWithSplices C.runChildren splicefuncs 
    where
    splicefuncs = mconcat  
        [ "name" ## (C.pureSplice . C.textSplice $ view name)
        , "age" ## (C.pureSplice . C.textSplice $ T.pack . show . view age)
        , "location" ## (C.pureSplice . C.textSplice $ view location)
        ]

. , Handler a RuntimeSplice:

addPersonSplices :: HasHeist b => Snaplet (Heist b) -> 
                                  SnapletLens b Foo -> 
                                  Initializer b v ()
addPersonSplices h l = addConfig h $ mempty 
   {
      hcCompiledSplices = "person" ## (personSplice . lift $ personH l) 
   } 

:

addPersonSplices h foo

:

("/persons",  cRender "_persons")

, http://127.0.0.1:8000/persons, .

UPDATE

( , ), .

:

<body>
    <strings>
        <p><string>dummy value</string></p>
    </strings>
</body>

:

stringSplice :: Monad n => C.Splice n
stringSplice = C.manyWithSplices C.runChildren splicefuncs (return ["aa","bb","cc"])
    where
    splicefuncs = "string" ## (C.pureSplice . C.textSplice $ id)

, " , , , , , string".

, manyWithSplices (##) RuntimeSplice n Text -> Splice n. id Text -> Text. C.TextSplice Text -> Builder, C.pureSplice RuntimeSplice n Text -> Splice n.

(return ["aa","bb","cc"]) , .

:

addStringSplices :: HasHeist b => Snaplet (Heist b) -> Initializer b v ()
addStringSplices h = addConfig h $ mempty 
    {
          hcCompiledSplices = "strings" ## stringSplice
    }  
+6

All Articles