question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Overriding of parent's variables in global scope

See original GitHub issue

Hi

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:closed
  • Created 8 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
slavafomincommented, Aug 13, 2015

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:

{# main.nj #}
{% set title = title | default('Hello World') %}

<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
</head>
</html>
{# index.nj #}
{% extends 'main.nj' %}
{% set title = 'Index!' %}

It works fine with strings, however, when I try to use booleans it’s not working for some reason.

{# main.nj #}
{% set show_footer = show_footer | default(true) %}
...
{# index.nj #}
{% extends 'main.nj' %}
{% set show_footer = false %}

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:

{% set default show_footer = true %}

So, variables marked as default could be overridden in the child templates.

In Twig we were using this type of statement:

{% if show_footer is not defined %}
    {% set show_footer = true %}
{% endif %}

It’s also too bulky, but it was working perfectly.

0reactions
slavafomincommented, Aug 25, 2016

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found