diff --git a/docs/content/templates.md b/docs/content/templates.md index 9f105c0..1ae956c 100644 --- a/docs/content/templates.md +++ b/docs/content/templates.md @@ -37,6 +37,8 @@ these are explained on this page. See the example in [/example/templates/tag.html.twig](https://github.com/samwilson/basildon/blob/main/example/templates/tag.html.twig) 6. `wikipedia(lang, title)` — Returns an HTML extract of the given article. For example: `{{wikipedia('en', 'Tag (metadata)')|raw}}` +7. `get_json(url)` — Fetch JSON data from any URL. + For example: `{{get_json('https://api.wikitree.com/api.php?action=getProfile&key=Hall-22337').0.profile.LongName}}` ## Filters and escapers diff --git a/example/templates/recent.html.twig b/example/templates/recent.html.twig index c60fdb2..6e21dfb 100644 --- a/example/templates/recent.html.twig +++ b/example/templates/recent.html.twig @@ -6,6 +6,12 @@ {{ page.body|md2html|raw }} + {% set example_data = get_json('https://microsoftedge.github.io/Demos/json-dummy-data/64KB.json') %} + +

Example description: {{example_data.3.bio}}

+ +

Another JSON example: {{get_json('https://api.wikitree.com/api.php?action=getProfile&key=Hall-22337').0.profile.LongName}}

+
    {% for p in database.query('SELECT * FROM pages WHERE date IS NOT NULL ORDER BY date DESC LIMIT 10') %}
  1. diff --git a/src/Twig.php b/src/Twig.php index 0c43e75..c09daa5 100644 --- a/src/Twig.php +++ b/src/Twig.php @@ -50,6 +50,7 @@ final class Twig extends AbstractExtension 'commons' => [], 'wikidata' => [], 'flickr' => [], + 'json' => [], ]; public function __construct(Database $db, Site $site, Page $page) @@ -83,6 +84,7 @@ public function getFunctions(): array new TwigFunction('date_create', [$this, 'functionDateCreate']), new TwigFunction('strtotime', 'strtotime'), new TwigFunction('json_decode', 'json_decode'), + new TwigFunction('get_json', [$this, 'functionGetJson']), new TwigFunction('tex_url', [$this, 'functionTexUrl']), new TwigFunction('wikidata', [$this, 'functionWikidata']), new TwigFunction('commons', [$this, 'functionCommons']), @@ -305,25 +307,35 @@ public function functionCommons(string $filename): array public function functionWikipedia(string $lang, string $articleTitle): string { - if (isset(self::$data['wikipedia'][$articleTitle])) { - return self::$data['wikipedia'][$articleTitle]; + $url = "https://$lang.wikipedia.org/api/rest_v1/page/summary/" . str_replace(' ', '_', $articleTitle); + $response = $this->functionGetJson($url); + if (!isset($response['extract_html'])) { + throw new Exception("Unable to get extract of Wikipedia article: $articleTitle"); + } + return $response['extract_html']; + } + + public function functionGetJson(string $url): mixed + { + $cacheKey = md5($url); + if (isset(self::$data['json'][$cacheKey])) { + return self::$data['json'][$cacheKey]; } - $cache = $this->getCachePool('wikipedia'); - $cacheItem = $cache->getItem('wikipedia' . $articleTitle); + $cache = $this->getCachePool('json_' . parse_url($url, PHP_URL_HOST)); + $cacheItem = $cache->getItem($cacheKey); if ($cacheItem->isHit()) { return $cacheItem->get(); } - CommandBase::writeln("Wikipedia fetch extract: $articleTitle"); - $url = "https://$lang.wikipedia.org/api/rest_v1/page/summary/" . str_replace(' ', '_', $articleTitle); + CommandBase::writeln("Get JSON: $url"); $json = (new Client())->get($url)->getBody()->getContents(); $response = json_decode($json, true); if (!$response) { - throw new Exception("Unable to get extract of Wikipedia article: $articleTitle"); + throw new Exception("Unable to get JSON from URL: $url"); } - self::$data['commons'][$articleTitle] = $response['extract_html']; - $cacheItem->set(self::$data['commons'][$articleTitle]); + self::$data['json'][$cacheKey] = $response; + $cacheItem->set(self::$data['json'][$cacheKey]); $cache->save($cacheItem); - return self::$data['commons'][$articleTitle]; + return self::$data['json'][$cacheKey]; } /**