Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

JSON Question

Started by Muut Archive 11 years ago · 12 replies · 481 views
11 years ago

hi all,

for automatically loading items via the page.children i created an page.json.twig template. if i call the file directly via "http://server.com/page.json" the *.json file is parsed like it should.

if an element inside the code calls for the json file via javascript some numbers and so are added to the file uri (http://server.com/page.json?_22344234) and an error appears that this file can not be found.

what could be a solution for this? thanks very much.

11 years ago

how are you calling the page via javascript? Can you paste a sample of that code?

Also can you please include your code snippets in markdown backticks? or code blocks? (see the question mark in top right of the text-entry box on this forum for help).

11 years ago

i call it via the data-json="portfolio.json" attribute in the html code. i dont se markdown at all.

in the js i use
---js
if( $('.page').length ) {
var pageJSON = $('.page[data-json]');
var pageData = '';
var $page = $('.page');

JS
    if( pageJSON.length ) {
        $.ajax({
            url: pageJSON.attr('data-json'),
            cache: false,
            success: function(data) {
                pageData = data;

...

11 years ago

what happens if you set cache: true or remove that bit? I don't think Grav has anything to do with this checksum data appended after the .json part.

11 years ago

ok i will try. i wondered that calling directly in browser works but in attribute not. so i have to check the javascript. thanks

11 years ago

ok this was/is the reason. the parsed json is not valid and i have no clue how to get {{ page.title }} and so on in an array to let jsonencode via twig work...

11 years ago

Here's what we have in the simplesearch plugin's simplesearch_resultsjson.twig file:

TWIG
{"results":[
{%- for search_result in search_results -%}
{{- search_result.route|json_encode -}},{{ not loop.last ? ',' }}
{%- endfor -%}
]}

Basically we are constructing an results array and using the |json_encode twig filter to ensure it's in the correct json format.

11 years ago

i have something like this
---twig
[
{% for page in page.children %}
{
"image": "{{ page.media.images|first.url }}",
"name": "{{ page.title }}",
"categories": ["Logo"],
"url": "{{ page.url }}"
{% if not loop.last %}
},
{% else%}
}
{% endif %}
{% endfor %}
]

TXT


but the json format is not valid becaus of curly braces
11 years ago

ok right now i change the indentation and it works. so the first image is pulled out of the folder, the title, the categories out of the taxonomy tags and the url of the page
---twig
[
{% for page in page.children %}
{
"image": "{{ page.media.images|first.url }}",
"name": "{{ page.title }}",
"categories": [{% for tag in page.taxonomy.tag %}"{{ tag }}"{% if not loop.last %}, {% endif %},{% endfor %}],
"url": "{{ page.url }}"
{% if not loop.last %}
},
{% else%}
}
{% endif %}
{% endfor %}
]

TXT


maybe this post will help other users to deal with it. thanks for your help :)
11 years ago

ok nice to know. twig has a lot of options which i have to dig in deeper :)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1326 9 years ago
Archive · by Muut Archive, 9 years ago
2 921 9 years ago
Archive · by Muut Archive, 9 years ago
2 4050 9 years ago
Archive · by Muut Archive, 9 years ago
1 2928 9 years ago
Archive · by Muut Archive, 9 years ago
3 1107 9 years ago