Skip to content

Instantly share code, notes, and snippets.

@peteheaney
peteheaney / twig-variable-array-key.twig
Created April 30, 2020 15:35
Twig - use the value of a variable as a hash key
{% set foo = "myKey" %}
{% set bar = "my value" %}
{# To name a hash key with the value of the foo variable, wrap the variable in parentheses: #}
{% set newThing = {(foo): bar} %}
{# Which is the same as #}
{% set newThing = {myKey: "my value"} %}
@peteheaney
peteheaney / craft-guzzle.twig
Last active March 24, 2023 18:17
Use Guzzle in a Craft CMS twig template. No need for a plugin!
{# Create Guzzle instance #}
{% set client = create({
'class': 'GuzzleHttp\\Client'
}) %}
{# Send API request #}
{% set response = client.request('GET', 'https://jsonplaceholder.typicode.com/posts') %}
{# Check the response status #}
{% set status = response.getStatusCode() %}