Reputation: 307
I have several template that use limited number of string many times, so I'm thinking of reusing strings in some way instead of hard coding in templates.
I found two way, but both of them has limitation, so I'm asking of better way to do this or probably standard way to managing String in Django templates.
First and simply: Storing them in Database, it works well if I create a simple model below and send all of them in views (about 500 objects), for all rendered template. Further more I can categorize them, and send just related category in each template.
class Subject(models.Model):
key = models.CharField()
trans = models.TextField()
type = models.SmallIntegerField(choices=string.types)
Second: using {% with x="done" %}
tag I can use "with" tag in base template, and extend it in other templates, so i have access to all of tag with standard django.
Thanks to @Danial Roseman's comment I can use i18n.
Upvotes: 1
Views: 299
Reputation: 77912
You could also just store your strings in a dict (let's call it 'strings') that you pass into the context using a context processor and then use the standard {{ strings.key }}
notation. No security issue, no database required, no special management needed and you can mark your strings as translatable as well if you need it.
Upvotes: 1