Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#19735 closed New feature (wontfix)

Sometimes string interpolation is needed in templates

Reported by: aalexgabi Owned by: nobody
Component: Template system Version:
Severity: Normal Keywords: filter, interpolation
Cc: Marc Tamlyn Triage Stage: Design decision needed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: yes UI/UX: no

Description

Change History (5)

comment:1 by Marc Tamlyn, 12 years ago

Needs documentation: set
Needs tests: set
Patch needs improvement: set
Triage Stage: UnreviewedDesign decision needed
Version: 1.4

comment:2 by anonymous, 12 years ago

(Reading the Stack Overflow question) I don't understand what's the relationship with translation.

If you need to interpolate and translate you can simply use the blocktrans tag in the i18n template library.

comment:3 by Carl Meyer, 12 years ago

Resolution: wontfix
Status: newclosed

I don't think this is nearly a common enough need to warrant adding a new built-in filter to Django. There's a reason Django lets you write your own filters and tags when you need them, and this one is easy enough to write for those who want it.

comment:4 by aalexgabi, 12 years ago

UPDATE: @carljm I saw your message after posting this one

If the view and the template are generic it is not possible to use blocktrans. Specific parameters for generic views are passed in urls.py. Here is an example:

urls.py

    (r'^media/delete/(?P<object_id>[0-9]+)/$', views.generic_delete, {
        'model': models.Media,
        'template_name': 'openservices/partials/generic_delete.html',
        'extra_context': {
            'page_title': _('Delete media'),
            'message': _('Do you want to remove media %s? Media will be lost!'),
        },

generic_delete.html

{% block content %}
    <div class="modal in" tabindex="-1" role="dialog"
         aria-labelledby="formHeader" aria-hidden="true" data-keyboard="false" data-backdrop="static"
         style="display: block">
        <form class="form-horizontal" method="post">
            <div class="modal-header">
                <a class="close" href="{{ next }}">×</a>
                <h3 id="formHeader">
                    {{ page_title }}
                </h3>
            </div>
            <div class="modal-body">
                <p class="text-error">
                    {{ message|interpolate:object }}
                </p>
                {% csrf_token %}
            </div>
            <div class="modal-footer">
                <a class="btn" href="{{ next }}">{% trans 'Close' %}</a>
                <input type="submit" class="btn btn-danger" value="{% trans 'Delete' %}">
            </div>
        </form>
    </div>
    <a class="modal-backdrop in" href="{{ next }}"></a>
{% endblock %}

The same template and view is used for multiple models. For each model there is a different confirmation message. Blocktrans cannot be used because it requires hard coding the string that needs to be translated in the template.

Last edited 12 years ago by aalexgabi (previous) (diff)

comment:5 by Aymeric Augustin, 12 years ago

URLconfs aren't supposed to contain your entire application.

In this case it's much more reasonable to subclass the generic view and override get_context_data.

Note: See TracTickets for help on using tickets.
Back to Top