Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inheritance / {extend} - Outputs result of previous fetch again #312

Closed
SpazzMarticus opened this issue Nov 8, 2016 · 5 comments
Closed

Comments

@SpazzMarticus
Copy link

SpazzMarticus commented Nov 8, 2016

Took me a while but I found a strange behaviour when using Inheritance and {extend}

Setup: I got a Base class which should use the base.tpl and a Child class (Child extends Base) which should use the child.tpl (The child.tpl uses {extend 'base.tpl'})

And I got a BaseClone which is just class BaseClone extends Base{ /* Nothing here */ } and should use the base.tpl

(Let's assume the output is an ID and the class.)

Templates:
base.tpl

{$e->id} {block name=elementContent}{$e->getHTML()}{/block}

child.tpl

{extends 'base.tpl'}{block name=elementContent}{$e->getHTML()}{/block}

The problem:

  • If I fetch only Bases and BaseClones everything is fine.
  • If I fetch one Child and then BaseClones everything is fine too.
  • But once I fetch two or more Childs and then try to fetch BaseClones I end up getting the result of a previous Child again and again.

Premise:

$smarty->assign('e',new Child( 1 ));
$smarty->fetch('child.tpl');
$smarty->assign('e',new BaseClone( 2 ));
$smarty->fetch('base.tpl');

Output OK:

1 Child
2 BaseClone

Premise:

$smarty->assign('e',new Child( 1 ));
$smarty->fetch('child.tpl');
$smarty->assign('e',new Child( 2 ));
$smarty->fetch('child.tpl');
$smarty->assign('e',new BaseClone( 3 ));
$smarty->fetch('base.tpl');
$smarty->assign('e',new BaseClone( 4 ));
$smarty->fetch('base.tpl');

Output FAILED:

1 Child
2 Child
2 Child //Instead of "3 Base Clone"
2 Child //Instead of "4 Base Clone"

I set up some testing in this repository: https://github.com/SpazzMarticus/smarty-bug

@uwetews
Copy link
Contributor

uwetews commented Nov 8, 2016

Interesting case.
This is caused by template object caching, which should not be turned off for performance reasons.
The workaround is to use on one of the templates a compile id. Like

$smarty->fetch('child.tpl', null, 1);

I will try to find a solution with out the work around.

Perhaps a useful performance by creating template objects.

$child = $smarty->createTemplate('child.tpl',null,1);
$base = $smarty->createTemplate('base.tpl');
$smarty->assign('e',new Child( 1 ));
$child->fetch();
$smarty->assign('e',new Child( 2 ));
$child->fetch();
$smarty->assign('e',new BaseClone( 3 ));
$base->fetch();
$smarty->assign('e',new BaseClone( 4 ));
$base->fetch();

@SpazzMarticus
Copy link
Author

Simplest workaround I found was creating a copy of the base.tpl or using a extension of base.tpl which does nothing extra.

Nevertheless I would appreciate a fix in the source code. Good luck finding a solution! 👍

@uwetews
Copy link
Contributor

uwetews commented Nov 9, 2016

This problem does not occur the dev-master version due to changes in template object caching.

@uwetews uwetews closed this as completed Nov 9, 2016
@SpazzMarticus
Copy link
Author

SpazzMarticus commented Nov 10, 2016

I tested it with your latest commit (e867ae1) and it worked.

I then altered Child and child.tpl and now it fails fatal with

Call to undefined method BaseClone::getText() in <FILE>_0.file.child.tpl.php

Somehow the child.tpl is called again.


Changes:

Child-class now has a custom function

    public function getText()
    {
        return $this->getHTML();
    }

and child.tpl calls it

{extends 'base.tpl'}{block name=elementContent}{$e->getText()}{/block}

I updated my repo so you can test it: https://github.com/SpazzMarticus/smarty-bug

Maybe you could reopen the issue and I hope you can fix it.

@uwetews uwetews reopened this Nov 10, 2016
uwetews added a commit that referenced this issue Nov 11, 2016
…ch() or Smarty::isCached() the inheritance data

    must be removed #312
@uwetews
Copy link
Contributor

uwetews commented Nov 12, 2016

It has bin now finally fixed in the master branch

@uwetews uwetews closed this as completed Nov 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants