Python expressions allow you to evaluate Python code directly in template tag attributes by wrapping the expression in parentheses. This provides a Vue/React-like experience for writing component templates.
{% component "button"
text="Submit"
disabled=(not editable)
/ %}True when editable is False{% component "button"
text="Delete"
variant=(my_user.is_admin and 'danger' or 'primary')
/ %}'danger' when my_user.is_admin is True, otherwise 'primary'{% component "button"
text="Click Me"
size=(name.upper() if name else 'medium')
/ %}.upper()) and conditionals to transform the name value{% component "user_card"
username=my_user.username
is_active=(my_user.status == 'active')
is_admin=(my_user.role == 'admin')
score=(my_user.points + bonus_points)
/ %}Status: active
Score: 175
Admin{% component "button"
text=(items[0].title if items else 'No Items')
disabled=(items_len == 0)
variant=(config.get('button_style', 'primary'))
/ %}You would need to compute values in get_template_data():
def get_template_data(self, args, kwargs, slots, context):
return {
"disabled": not kwargs["editable"],
"variant": "danger" if kwargs["my_user"].is_admin else "primary",
}Evaluate directly in the template:
{% component "button"
disabled=(not editable)
variant=(my_user.is_admin and 'danger' or 'primary')
/ %}get_template_data() or views