Overriding of parent's variables in global scope
See original GitHub issueHi
First of all, I may be very wrong on my further assumptions. So, please, take my apologies in case I understand inheritance in wrong way and just wasting you time. Thanks in advance!
Just let’s take for example:
We have following structure:
Layouts
|_ _layout.nj
|_ index.nj // extends `_layout.nj`
|_ extendsIndex.nj // extends `index.nj`
and following content:
// _layout.nj
<!DOCTYPE html>
<html>
<head></head>
<body>
{% set layoutVar = 'Test var from <b>layout</b>' %}
<p>TestVar: {{ testVar }}</p>
<p>layoutVar: {{ layoutVar }}</p>
<p>MainBlock: {% block main %}{% endblock %}</p>
</body>
</html>
// index.nj
{% extends "_layout.nj" %}
{# --------- #}
{% set testVar = "Test var from <b>index</b>" %}
{% set layoutVar = "Test var from <b>index</b>" %}
{# --------- #}
{% block main %}Test block from <b>index</b>{% endblock %}
// extendsIndex.nj
{% extends "_layout.nj" %}
{# --------- #}
{% extends "index.nj" %}
{# --------- #}
{% set testVar = "Test var from <b>extendsIndex</b>" %}
{% set layoutVar = "Test var from <b>extendsIndex</b>" %}
{# --------- #}
{% block main %}Test block from <b>extendsIndex</b>{% endblock %}
What I’m expecting to see in output:
// index.html
TestVar: Test var from index
layoutVar: Test var from index
MainBlock: Test block from index
// extendsIndex.html
TestVar: Test var from extendsIndex
layoutVar: Test var from extendsIndex
MainBlock: Test block from extendsIndex
What we will see:
// index.html
TestVar: Test var from index
layoutVar: Test var from layout // I assume it had to be overridden by `index.nj` variables, isn't it?
MainBlock: Test block from index
// extendsIndex.html
TestVar: Test var from index // I assume it had to be overridden by `extendsIndex.nj` variables
layoutVar: Test var from layout // I assume it had to be overridden by `extendsIndex.nj` variables
MainBlock: Test block from extendsIndex
Once again, maybe I’m simply getting it wrong and child template shouldn’t override parent variables. But it seems to me like it should, since it’s defined as last, thus must be applied on top of all.
Issue Analytics
- State:
- Created 8 years ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
Variables in Java Do Not Follow Polymorphism and Overriding
Overriding is only applicable to methods but not to variables. In Java, if the child and parent class both have a variable with...
Read more >Overriding variables in child pipeline jobs is inconsistent with ...
When declaring a global variable, overriding it in a job template and running the same job in a parent pipeline and child pipeline, ......
Read more >Overriding member variables in Java ( Variable Hiding)
When you make a variable of the same name in a subclass, that's called hiding. The resulting subclass will now have both properties....
Read more >What is the scope of variables and variable override order?
Variables set at an inner subflow scope override those set at an outer subflow scope. This variable override order also applies to default...
Read more >Why the Instance Variable of the Super Class Is Not ... - DZone
Well generally, we say that the Child class will override the variable declared in the Parent class, and parent.x will give us whatever...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hello!
I’m also looking for an easy way to override variables in parent template.
Of course I can use an “initialization” block as @jlongster suggested, but it’s too cumbersome in my opinion.
I’ve managed to achieve the desired result the following way:
It works fine with strings, however, when I try to use booleans it’s not working for some reason.
What could be the problem?
Also, I think the idea of overriding parent variables is so useful and required in almost any project, so we should provide a more convenient and short way to do it. For example we could define variables that way:
So, variables marked as
default
could be overridden in the child templates.In Twig we were using this type of statement:
It’s also too bulky, but it was working perfectly.
I’m just asking for a way to replace bulky constructs like this:
{% set show_footer = show_footer | default(true) %}
with something more sane like this{% set default show_footer = true %}
. Probably, it’s just a syntactic sugar, I don’t see why it should break the BC with Jinja.And in general, I’m not quite sure that Jinja should prevent us from innovating and implementing new features.