- Issue created by @msn5158
Although I don't know how to use source Twig function to load JSON files, because I tried, No effect seen.
I still hope that this module can add this feature, such as throughdrupal_loads_json()
or other names.Thanks
- 🇷🇺Russia Chi
drupal_loads_json()
That would just duplicate
source
function from Twig core. /sites/default/files/json/test.json
Json Data:
{
"nid": 1,
"title": "Sample One",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing."
}Use by Twig:
{% set data = source('/sites/default/files/json/test.json', ignore_missing = true) %}{% for key, value in data %}
{{ key }}: {{ value }}
{% endfor %}No output, Is there anything wrong with it?
- Status changed to Closed: duplicate
11 months ago 5:18pm 24 February 2024 - 🇷🇺Russia Chi
source()
just loads file content. It does not decode it. You need something likejson_decode
.See related issue for explanations.
For such case I would recommend loading data in preprocess hook. That should be trivial.
function example_preprocess(array $variables): void { $json = \file_get_contents('public://test.json'); $variables['test'] = \json_decode($json); }