Accessing multiple dicts at the same time?

13 views
Skip to first unread message

fgmc...@gmail.com

unread,
Aug 29, 2014, 9:45:03 PM8/29/14
to onio...@coralbits.com
Hey there, I got a simple webserver for a site running with onion, but I ran into an issue and I couldn't find a good way to solve it.

What I need to do is create a simple table with multiple columns, it would something like:

for a, b, c in dict1, dict2, dict3:
 <tr>
   <td> {{ a }} </td>
   <td> {{ b }} </td>
   <td> {{ c }} </td>
 </tr>
endfor

But that doesn't seem to work. It seens that I can't access elements by keys either... So I thought of putting all elements in a single dict, but then I couldn't add a counter to control the loop.
My best idea so for was creating a javascript that handles the table. Still not very pratical.

So... what would be the right way to create a table with n rows and 3 columns using onion + otemplate?

Thanks in advance.

David Moreno

unread,
Aug 31, 2014, 4:31:52 PM8/31/14
to fgmc...@gmail.com, onio...@coralbits.com
Hi,

check at
https://github.com/davidmoreno/onion/blob/master/examples/ofileserver/fileserver.html#L29

It does something like:

{% for file in files %}
<tr>
<td><a href="{{file.name}}{% if file.type == "dir" %}/{% endif
%}">{{file.name}}</a></td>
<td>{{file.size}}</td>
<td>{{file.owner}}</td>
</tr>
{% endfor %}

Where files is a dict with key the number, and value the attributes. I
sjson would be something like:

{ "0" : { "size" : "123", "owner" : "dmoreno", "name": "test", "type":
"png"}, "1" : {...} }

Otermplate (and onion) doesnt know (yet?) about arrays in its data
representation layer to otemplate (onion_dict), only dicts and strings.
So the for just iterates through all elements and user can use the
values. In some future version I would like to add support for full
json, maybe using some external library, maybe adding the support to
onion_dict or similar... Anyway, just know its only this: strings and
dicts.

Regards,
David.

--
David Moreno Montero

dmo...@coralbits.com <mailto:dmo...@coralbits.com>
+34 658 18 77 17
<http://www.coralbits.com/>
http://www.coralbits.com

fgmc...@gmail.com

unread,
Aug 31, 2014, 11:47:59 PM8/31/14
to onio...@coralbits.com, fgmc...@gmail.com

Oh, I needed to have added a dict inside the dict to use they keyed values.
Sorry for missing it in the example, I feel stupid now.

It works now, thanks a lot.
Reply all
Reply to author
Forward
0 new messages