chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Ersatz question


From: Ivan Raikov
Subject: Re: [Chicken-users] Ersatz question
Date: Wed, 6 Mar 2013 15:41:00 +0900


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 <address@hidden> 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 <address@hidden> 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
address@hidden
https://lists.nongnu.org/mailman/listinfo/chicken-users




reply via email to

[Prev in Thread] Current Thread [Next in Thread]