Opened 3 years ago

Closed 2 years ago

Last modified 2 years ago

#27730 closed Cleanup/optimization (fixed)

Document that template tags with "as" variable assignment don't propogate variables across blocks

Reported by: Peter Bittner Owned by: kapil garg
Component: Documentation Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Peter Bittner)

The trans ... as tag as documented in topics / i18n / translation only works in an isolated fashion outside blocks or inside of blocks. In other words, it does not work across blocks when defined outside of them, i.e. "globally" in a template.

The following example doesn't work: -- the value of {{ title }} will be empty.

{% extends 'base.html' %}{% load i18n %}
{% trans "My Projects" as title %}

{% block head_title %}{{ title }}{% endblock %}
{% block content %}
    <h2>{{ title }}</h2>
{% endblock %}

This behavior is not documented, and neither is this behavior intuitive.

The same behavior and/or lack of documentation is true for blocktrans asvar ....

Enhancement or Documentation Fix?

This issue should be clarified and either fixed in the documentation (if the observations are confirmed), or alternatively changed in the template tag's behavior (if feasible and sensible).

Which route should be taken? Please clarify.

See Also

Change History (9)

comment:1 Changed 3 years ago by Peter Bittner

Description: modified (diff)

comment:2 Changed 3 years ago by Tim Graham

Component: InternationalizationDocumentation
Summary: i18n: trans tag with "as" does not work across blocksDocument that template tags with "as" variable assignment don't propogate variables across blocks
Triage Stage: UnreviewedAccepted

Do any template tags that use the "as" syntax work that way? I doubt it but I haven't checked. Probably the behavior should be documented in template inheritance -- this likely isn't specific to {% trans %} and {% blocktrans %}.

comment:3 Changed 3 years ago by Peter Bittner

I think it makes sense to document the behavior in the section of the tag itself, because when you do programming you jump in directly to the specific section. You don't read up on concepts first.

If this is some generic behavior it should be documented in a central place (maybe "template inheritance" makes sense?), but links from the specific sections should point there. Especially if the behavior is not obvious---and I have a bit of a feeling it isn't.

comment:4 in reply to:  2 Changed 3 years ago by kapil garg

Replying to Tim Graham:

Do any template tags that use the "as" syntax work that way? I doubt it but I haven't checked. Probably the behavior should be documented in template inheritance -- this likely isn't specific to {% trans %} and {% blocktrans %}.

Basically "block" tags cannot use variables which are generated outside them (which are not passed in initial context) in the child template. Do the changes need to be made on dev documentation only ?

comment:5 Changed 3 years ago by Tim Graham

All pull requests go to the master branch and are backported by the committer, if needed. See the Supported Versions policy.

comment:6 Changed 3 years ago by kapil garg

Owner: changed from nobody to kapil garg
Status: newassigned

comment:7 Changed 3 years ago by kapil garg

Has patch: set

comment:8 Changed 2 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 278a09a:

Fixed #27730 -- Doc'd that template vars created outside a block can't be used in it.

comment:9 Changed 2 years ago by Tim Graham <timograham@…>

In 8a1340ea:

[1.11.x] Fixed #27730 -- Doc'd that template vars created outside a block can't be used in it.

Backport of 278a09ac1f410cf4e3472146324feded2e2c18b3 from master

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