digideskio / zotonic

Automatically exported from code.google.com/p/zotonic
0 stars 0 forks source link

to_json flter #154

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
This filter could be especially useful for predominatly Ajax sites.

Problem: we have a list of values, we need to create a JSON/Javascript array:

Current solution:
    [
      {% for el in list %}
        {{ el }}{% if not forloop.last %},{% endif %}
      {% endfor %}
    ]

Proposed solution:
{{ list|to_json }}

I believe such a filter is as simple as:

-module(filter_to_json).
-export([to_json/2]).

as_atom(V, _Context) ->
    mochiweb_json2:encode(V).

Original issue reported on code.google.com by dmitr...@gmail.com on 7 Mar 2011 at 12:08

GoogleCodeExporter commented 8 years ago
Fixed in revision a1a7fe8984

I matched the JSON conversion call to how it is currently already used in the 
REST API controller (resource_api); z_convert:to_json is quite powerful and can 
even convert dates into strings.

Original comment by scherpenisse on 10 Mar 2011 at 8:12