I'm having a template, where a link shall be inside a translation-
marked text. I see two ways how to accomplish this, but both don't
work:
1. {% trans "You need to <a href="{% url login %}">login</a> before
you can do anything" %}
I get an template syntax error: "Searching for value. Unexpected
end of string" (because of the quotes). Escaping the quotes or taking
single quotes also didn't work
2. {% blocktrans %}You need to <a href="{% url login %}">login</a>
before you can do anything {% endblocktrans %}
Again template syntax error: "'blocktrans' doesn't allow other
block tags (seen u'url login') inside it"
Is there a way with either of these tags to accomplish what I want? Of
course there would also be the possibility to mark each single part of
the text, like so:
3. {% trans "You need to " %}<a href="{% url login %}">{% trans "login
</a> before you can do anything" %}
But I don't want to split the string in the middle of a sentence.
I've recently run across a similar problem and solved it using some
nasty with-tag trickery.
In your case the following code should do the trick:
{% url login as login_url %}
{% with "<a href=\""|add:login_url|add:"\">" as link_begin %}
{% with "</a>" as link_end %}
{% blocktrans %}Please {{ link_begin }}login{{ link_end }} to proceed.
{% endblocktrans %}
{% endwith %}
{% endwith %}
{% blocktrans %} also has a builtin "with" support, but for some reason
it does not support escaping special characters.
So the following doesn't work:
{% blocktrans with "<a href=\""|add:login_url|add:"\">" as link_begin
and "</a>" as link_end %}Please {{ link_begin }}login{{ link_end }} to
proceeed.{% endblocktrans %}
--mp
On 10 Sty, 12:59, Andreas Pfrengle <a.pfren...@gmail.com> wrote:
> I'm having a template, where a link shall be inside a translation-
> marked text. I see two ways how to accomplish this, but both don't
> work:
> 1. {% trans "You need to <a href="{% url login %}">login</a> before
> you can do anything" %}
(...)
> 2. {% blocktrans %}You need to <a href="{% url login %}">login</a>
> before you can do anything {% endblocktrans %}
(...)
>
> Is there a way with either of these tags to accomplish what I want?
>
I haven't tried this but maybe it will work:
{% url login as login_url %}
{% blocktrans %}You need to <a href="{{login_url}}">login</a> before
you can do anything {% endblocktrans %}
I'm curious if this is good guess, so please let me know.
--
Tomasz Zielinski
http://pyconsultant.eu
It works! Didn't know that the url tag can name its result with "as".
Thanks again :-)
{% blocktrans trimmed with '<a href="'|add:url_to_sth|add:'">'|safe as link_begin and '</a>' as link_end %}
Lorem lipsum sit dolor amet enim - {{ link_begin }}Sth{{ link_end }}
{% endblocktrans %}