locomotivecms/engine — Issue #285: Locomotive can't generate json/xml (response type is always text/html)
Author: kikito
Created: 2012-01-24 12:32:46+00:00
State: open

Hi there!

I've got two sites - let's call it "news" and "corporate".

I need the "corporate" site to consume news from the news site.

I have tried generating json/xml pages for the corporate site to consume. They look like this:

{
"elements":[
{% for news in contents.news %}
{ "title":"{{ news.title | strip_newlines | replace: '"', '\\"' }}",
"date":"{{ news.date | format_date: '%d-%m-%Y' }}"
}{% unless forloop.last %},{% endunless %}
{% endfor %}
]
}

The generated JSON code is valid, but unfortunately the response type is "text/html". This field is (used by HTTParty to determine which parser to use)[http://rubydoc.info/github/jnunemaker/httparty/master/HTTParty/Parser]. Instead of using a "json" or "xml" parser, it always uses the "html parser", which doesn't even support the .collect method - so an error is raised.

I see two possible solutions:

* One would be adding a new field to the pages called "response type" to each page (default to "text/html") and use that when serving a page
* The other option would be making the "format: 'json'" parameter in the consume liquid tag "override" the response type, changing it to "text/javascript" when json was used and "text/xml" for xml.
* The two options above are not mutually exclusive, by the way ;)

Best regards,

Enrique