twigphp / Twig

Twig, the flexible, fast, and secure template language for PHP
https://twig.symfony.com/
BSD 3-Clause "New" or "Revised" License
8.17k stars 1.25k forks source link

Complex Dynamic inheritance #653

Closed lucassouza1 closed 12 years ago

lucassouza1 commented 12 years ago

I include a template that is a proxy to another:

{% extends 'html/field/type/' ~ field.type ~ ".html" %}

but all instances use the same as the first item value. This happens because parent is created just once: ´´´php

public function getParent(array $context)
{
    if (null === $this->parent) {
        $this->parent = (("html/field/type/" . $this->getAttribute((isset($context['field']) ? $context['field'] : null), "type", array(), "any", false, 1)) . ".html");
        if (!$this->parent instanceof Twig_Template) {
            $this->parent = $this->env->loadTemplate($this->parent);
        }
    }

    return $this->parent;
}

´´´

There's a way to use dynamic inheritance?

MichaelJCole commented 12 years ago

https://github.com/fabpot/Twig/issues/17

stof commented 12 years ago

@MichaelColeAmbiguity Please don't use issues closed 2 years ago during the first step of the development. Twig has dynamic inheritance now (but with a bug in it apparently)

MichaelJCole commented 12 years ago

@stof Awesome! Thanks! http://twig.sensiolabs.org/doc/tags/extends.html#dynamic-inheritance

fabpot commented 12 years ago

This has been fixed ages ago in Twig 1.1.2 (see #395).

lucassouza1 commented 12 years ago

Dynamic inheritance works only if you use the template just once. If I use the same template with another value bindings as the template was already created, the parent attribute in template generated class is already set, causing an unexpected behavior, The behavior inside first if statement:

    if (null === $this->parent) {
        $this->parent = (("html/field/type/" . $this->getAttribute((isset($context['field']) ? $context['field'] : null), "type", array(), "any", false, 1)) . ".html");
        if (!$this->parent instanceof Twig_Template) {
            $this->parent = $this->env->loadTemplate($this->parent);
        }
    }

happens just once making inheritance dynamic just for the first time.

So, I believe that it's another functionality, maybe not a simple dynamic inheritance.

fabpot commented 12 years ago

As I said, you are probably using a very old version of Twig. But since then, it has been fixed. So, any version newer than 1.1.1 will work as expected.

lucassouza1 commented 12 years ago

My local version was outdated. Everything is working fine on 1.7.

Sorry for the inconvenience!