The code for your second use case follows below. Ersatz uses the Tset
object for destructuring statements such as "for title, body in posts".
So each post would be a Tset object (which is again represented as a list):
(let* ( (source #<<EOF
{% for title, body in posts %}
<div class="blog-post">
<h3>{{ title }}</h3>
<div class="post-body">
{{ body }}
</div>
</div>
{% endfor %}
EOF
))
(from-string source models:
`((posts . ,(Tlist
(list
(Tset (list (Tstr "Post One title") (Tstr "Post
One body")))
(Tset (list (Tstr "Post Two title") (Tstr "Post
Two body")))
))
))
)
)
On Wed, Mar 6, 2013 at 3:34 PM, Ivan Raikov <[email protected]> wrote:
> Hi Matt,
>
> Thanks for trying again to use Ersatz. The models argument is an alist
> where the key is a symbol (the name of the variable),
> and the value is of type tvalue, which is a datatype defined in
> ersatz-lib.scm. So your example could work as follows:
>
> (let* ( (source
> #<<EOF
>
> {% for p in posts %}
> <div class="blog-post">
> <h3>{{ p.title }}</h3>
> <div class="post-body">
> {{ p.body }}
> </div>
> </div>
> {% endfor %}
> EOF
> ))
> (from-string source models:
> `((posts .
> ,(Tlist
> (list (Tobj `((title .
> ,(Tstr "Post One title"))
> (body .
> ,(Tstr "Post One body"))
> ))
> (Tobj `((title .
> ,(Tstr "Post Two title"))
> (body .
> ,(Tstr "Post Two body"))
> ))
> ))
> )
> ))
>
> This will create an ersatz template variable called "posts" which contains
> a list of Tobj values. Tobj is the wrapper for objects with named fields in
> Ersatz.
> It is basically equivalent to an alist, except that the values must be
> Ersatz values (i.e. tvalue datatype).
>
> The above code can be greatly simplified of course, so if you have some
> typical patterns of template use, just let me know, and I will implement
> them.
> Thanks,
>
> -Ivan
>
>
>
> On Wed, Mar 6, 2013 at 3:07 PM, Matt Gushee <[email protected]> wrote:
>
>> Hi, folks--
>>
>> I am trying again to learn how to use Ersatz. I am trying to render pages
>> that display sequences of similar components (e.g. blog posts). The
>> template syntax includes 'for' loops, e.g.
>>
>> {% for p in posts %}
>> <div class="blog-post">
>> <h3>{{ p.title }}</h3>
>> <div class="post-body">
>> {{ p.body }}
>> </div>
>> </div>
>> {% endfor %}
>>
>> Or ... I'm pretty sure the following would work in Jinja2 (where the
>> 'posts' variable is a list in the form [[<post>, <body>] ...]):
>>
>> {% for title, body in posts %}
>> <div class="blog-post">
>> <h3>{{ title }}</h3>
>> <div class="post-body">
>> {{ body }}
>> </div>
>> </div>
>> {% endfor %}
>>
>> So, do these constructs work in ersatz? If so, what data structures do I
>> want to pass as the models: argument when rendering the templates?
>>
>> Thanks for any info!
>>
>> --
>> Matt Gushee
>>
>> _______________________________________________
>> Chicken-users mailing list
>> [email protected]
>> https://lists.nongnu.org/mailman/listinfo/chicken-users
>>
>>
>
_______________________________________________
Chicken-users mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/chicken-users