Access dictionary property with dollar sign `$` in it's name using django template syntax in Klaviyo

Viewed 17

I'm trying to access a property with a $ in its name in a django template. Unfortunately I have no control over filters nor over the variable names.

The object is structured as follows:

{
    "title": "Some title",
    "metadata": {
        "$price": 9.99,
        "$inventory_policy": 1
    }
}

I am trying to access {{ item.metatadata.$price }}, but the template builder crashes with an unspecified error.

I already tried the workarounds for python templates, but they crash as well:

{{ item.metatadata.$$price }}
{{ item.metatadata.${price} }}

For future reference, this is in a Klaviyo template.

2 Answers

I think you can use a custom filter for doing that : https://docs.djangoproject.com/fr/4.1/howto/custom-template-tags/

in templatetags.extras.py

from django import template

register = template.Library()

@register.filter
def get_value(dictio, key):
   return dictio.get(key, '')

and for using it:

{% load extras %}
{{ item.metatadata|get_value:"$price" }} 
Related