Tech News
← Back to articles

Sharing a mutable reference between Rust and Python

read original related products more articles

As part of my ongoing project to reimplement Django’s templating language in Rust, I have been adding support for custom template tags.

The simplest custom tag will look something like:

# time_tags.py from datetime import datetime from django import template register = template . Library() @register.simple_tag def time (format_string): now = datetime . now() return now . strftime(format_string)

# time_tags.py from datetime import datetime from django import template register = template . Library() @register.simple_tag def current_time (format_string): return datetime . now() . strftime(format_string)

This can then be used in a Django template like:

{% load time from time_tags %} < p >Time: {% time '%H:%M:%S' %}

{% load current_time from time_tags %} < p >The time is {% current_time '%H:%M:%S' %}

The context#

Django’s templating language uses an object called a context to provide dynamic data to the template renderer. This mostly behaves like a Python dictionary.

details Technically, Django’s context contains a list of dictionaries. This allows for temporarily changing the value of a variable, for example within a {% for %} loop, while keeping the old value for later use.

... continue reading