JSON loading in template

377 views
Skip to first unread message

Christian Gloddy

unread,
Feb 21, 2014, 2:25:21 PM2/21/14
to nunj...@googlegroups.com
I'm trying to use nunjucks to transform one piece of json to another. I'd like a single file to be able to declare it's sources and then distribute the values.

Here's what I imagine (Warning: fake code ahead.):
{% set story = request('http://url-that-returns-json.com') %}
{
  "story": {
    "title": "{{ story.title }}"
  }
}

What's my best route for accomplishing this? Do I need to write a custom extension or is there an easier way?

Thanks,
Christian

James Long

unread,
Feb 24, 2014, 2:38:14 PM2/24/14
to Christian Gloddy, nunj...@googlegroups.com
Christian,

You won't be able to do that with nunjucks. You'd have to change the
syntax do something like:

{
{% load_json story title:"story title %}
}

Extensions only allow you do define custom tags. I think for what you
want to do, you should just work with JSON files directly and write
some custom node code to expand it out how you want.

- James
> --
> You received this message because you are subscribed to the Google Groups
> "Nunjucks" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to nunjucks+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
Reply all
Reply to author
Forward
0 new messages