Giter VIP home page Giter VIP logo

Comments (1)

jon-batscha avatar jon-batscha commented on August 18, 2024

Hey, great question!

As you alluded to, we have a render_template endpoint, though as you alluded to, the rate limit is sufficiently low such that it’s available for testing as opposed to running production workloads.

As for whether you can overcome this rate limit by rendering the email yourself before sending it on your own: yes, though with some caveats.
Klaviyo uses the Django Template Language, though with some customizations. If your template uses standard Django (e.g: just context variables), you should be able to render it using something like the following:


from django import template
from django.template import Context, Template
from django.conf import settings
import django
import json
import datetime

template_file = 'template.html'
context_file = 'context.json'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
    },
]

settings.configure(TEMPLATES=TEMPLATES)
django.setup()

register = template.Library()

@register.simple_tag
def current_year():
    return datetime.datetime.now().year

with open(template_file, 'r') as f:
    template_text = '{% load current_year %}' + f.read()

with open(context_file, 'r') as f:
    context_payload = json.load(f)
    
template = Template(template_text)
context = Context(context_payload)
rendered = template.render(context)

with open('rendered.html', 'w') as f:
    f.write(rendered)

Caveat: If your template uses non-standard Django features (e.g: Klaviyo-specific tags, such as {% current_year %}), you may need to make some modifications (such as above code for registering current_year as a tag and adding {% load current_year %} to start of template). We might be using a common extension of the Django template language, though I'll need to look into that. I'll also look into whether there is additional tooling/code samples we can share to cover more cases, maybe a django app that defines all tags.

In the meantime, I’ll link you these 2 guides which you may find helpful.


Please reach out if you run into any further issues, we really value this kind of feedback.

Cheers,
Jon

from klaviyo-api-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.