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

Strange behavior in smarty 3.1.30 #297

Closed
HolyGhost07 opened this issue Sep 26, 2016 · 3 comments
Closed

Strange behavior in smarty 3.1.30 #297

HolyGhost07 opened this issue Sep 26, 2016 · 3 comments

Comments

@HolyGhost07
Copy link

HolyGhost07 commented Sep 26, 2016

Hello! I have a problem with smarty 3.1.30. I wrote this code

<?php

require_once('vendor/autoload.php');

class Widget
{
    private $_smarty = null;

    public function __construct($smarty)
    {
        $this->_smarty = $smarty;
    }

    public function render($data)
    {
        $this->_smarty->assign('name', $data['name']);
        return "assing in smarty {$data['name']} - " . $this->_smarty->fetch($data['tpl']);
    }
}


$smarty = new Smarty();
$smarty->setTemplateDir('templates/');
$smarty->setCompileDir('templates_c');
$smarty->compile_check=true;
$smarty->force_compile=true;
$smarty->caching=false;

$widget = new Widget($smarty);
$smarty->registerPlugin('function', 'widget', [$widget, 'render']);

$smarty->display('templates/index.tpl');

?>

index.tpl

<html>

<body>
    <p>Hello world!!!</p>
    {include file='templates/widget.tpl' name='smarty' nocache}
    {include file='templates/widget.tpl' name='smarty' nocache}
    {widget name='smarty_1' tpl='templates/widget.tpl'}
    {widget name='smarty_2' tpl='templates/widget.tpl'} 
</body>
</html>

or index.tpl

<html>

<body>
    {$tpl='templates/widget.tpl'}
    <p>Hello world!!!</p>
    {include file=$tpl name='smarty'}
    {widget name='smarty_1' tpl=$tpl}
    {widget name='smarty_2' tpl=$tpl}  
</body>
</html>

widget.tpl

<p>Hello {$name}!!!</p>

In the issue

<html>

<body>
    <p>Hello world!!!</p>
    <p>Hello smarty!!!</p>

    <p>Hello smarty!!!</p>

    assing in smarty smarty_1 - <p>Hello smarty!!!</p>

    assing in smarty smarty_2 - <p>Hello smarty!!!</p>

</body>
</html>

When template has {include} tag where the file attribute is the same path in the users plugin, then smarty assign in the users plugin doesn't override current template variables. In smarty 3.1.27 it works.

uwetews added a commit that referenced this issue Sep 27, 2016
… Smarty::fetch() calls

           the template and config variables must be cleared #297
@uwetews
Copy link
Contributor

uwetews commented Sep 27, 2016

This bug is fixed now in the master branch.

Just some notes:
It's not necessary to store the Smarty object in your Widget object, It can be obtained from the passed template object.

class Widget
{
    public function render($data,  $template)
    {
       $template->smarty->assign('name', $data['name']);
        return "assing in smarty {$data['name']} - " . $template->smarty->fetch($data['tpl']);
    }
}

The your Widget class does update the template variable 'name' only in the Smarty object. This change
will not be seen in any called/included templates except new Smarty::fetch() and Smarty::display() calls.

<html>

<body>
    <p>Hello world!!!</p>
    {include file='templates/widget.tpl' name='smarty'}
    {widget name='smarty_1' tpl='templates/widget.tpl'}
    {widget name='smarty_2' tpl='templates/widget.tpl'} 
    {include file='templates/widget.tpl' name='smarty'}
</body>
</html>

Will output

<html>

<body>
<p>Hello world!!!</p>
<p>Hello smarty!!!</p>
assing in smarty smarty_1 - <p>Hello smarty_1!!!</p>
assing in smarty smarty_2 - <p>Hello smarty_2!!!</p>
<p>Hello smarty!!!</p>
</body>
</html>

@uwetews uwetews closed this as completed Sep 27, 2016
@HolyGhost07
Copy link
Author

Thank you for your help.

@edgarsi
Copy link

edgarsi commented Oct 21, 2016

Every other user might be using the same template from another template and then from PHP. I am, for sure, and tracking this down was not fun.
Shouldn't you revert the release version because of this or document the bug in http://www.smarty.net/docs/en/language.function.include.tpl ?

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

3 participants