开发者

How do I DRY up common text in a Django template?

I have some static text that needs to show up at 2 locations within a template.

For example:

<div>
{% if something %}
    This is a static text
{% else %}
    Something else happened
{% endif %}
</div>
... more html
<span>
{% if something %}
    This is a static text
{% else %}
    Something else happend
{% endif %}
</span>
  1. I can do the above by duplicating the above text at 2 different locations in my template file(as shown abov开发者_运维技巧e).
  2. I could also create a model which will store the text(This is DRY but cost a call to the DB for a simple task)
  3. I'm thinking of using include template but that's probably not the best way to achieve my goal.

What's the best way to do it?


Definitely use Inclusion Tags:

http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#inclusion-tags

The tag file would either be something super simple like just the text "This is a static text" or the entire block:

{% if something %}
This is a static text
{% else %}
Something else happened
{% endif %}

"something" can be passed as a variable to the template tag so you can use that entire block in a variable way.


I use the django internationalization to do that. So in my apps/template I just write the key, and in the .po files is the value of the keys.

{% load i18n %}

<div>
{% if something %}
    {% trans "static" %}
{% else %}
    {% trans "something else" %}
{% endif %}
</div>

And in my .po file:

msgid "static"
msgstr "This is a static text"

msgid "something else"
msgstr "Something else happened

Besides useful for multi-language, it's much easier for copy writing just in case you want to change it in the future because you can just look unto one file instead of browsing several templates.


There are several ways, but it probably depends on what the text is and how often it will be used. It's hard to recommend a specific choice without full details

  1. Create a custom template tag (this one makes the most sense based on how you've described your problem above).
  2. Create a base template which has the text in it at the correct location and then inherit off of it for your "2 locations"
  3. Put the static piece of text in a settings file and pass it to the template renderer via Context (probably not the best idea, but depending on what you're doing it could be a possibility)


You could use flatblocks : http://github.com/zerok/django-flatblocks

or chunks : http://code.google.com/p/django-chunks/

Those may be overkill for your problem, since they store your snippets in the database, but they add the benefit of making it possible to edit them via the admin.

{% load chunks %}
<div>
{% if something %}
    {% chunk "something" %} 
{% else %}
    {% chunk "something_else" %}
{% endif %}
</div>

There are lots of forks or similar projects, for example:

  • http://bitbucket.org/hakanw/django-better-chunks/
  • http://github.com/bartTC/django-generic-flatblocks


I have a file like Java properties that I use for all of my resource strings. I just serve up the one that I want. Keeping these in one place also makes translating easy.

Ex.:

welcome_msg="hello user!" 
thank_you="thank you" 
goodbye_msg="goodbye, " + thank_you


If the included text gets bigger, use an 'include' tag.

{% include "myapp/helptext.html" %}

GrtzG

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜