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

createTemplate() and fetch() loose assigned variables #242

Closed
ghost opened this issue Jun 9, 2016 · 2 comments
Closed

createTemplate() and fetch() loose assigned variables #242

ghost opened this issue Jun 9, 2016 · 2 comments

Comments

@ghost
Copy link

ghost commented Jun 9, 2016

I upgraded my Smarty from v3.1.27 to v3.1.29 and found the following problem:

$tpl = $smarty->createTemplate('subtemplate.html', null, null, $smarty); $myvar = array("something"); $tpl->assignByRef('MYVAR', $myvar); echo $tpl->fetch('subtemplate.html');

The above code fails in 3.1.29 with: Notice: Undefined index: MYVAR. My application uses $smarty quite a lot and uses createTemplate() several times to create custom results that are stored in php variables, all of them broke with "Undefined index". I tried various alternatives, like using assign instead of assignByRef, removing the $smarty as a parent from the createTemplate() parameters and changing variable names, but none of that had any effect. Going back to 3.1.27 makes everything work again.

@uwetews
Copy link
Contributor

uwetews commented Jul 11, 2016

If you call fetch() or display() on a template object you should call it without parameter like

echo $tpl->fetch();

See http://www.smarty.net/docs/en/api.create.template.tpl example 14.18

For other requirements since 3.1.29 it is possible to call fetch() on a template object but switch to another template.

echo $tpl->fetch('other.tpl');

In your case a new template object was create with same name 'subtemplate.html' but loosing the assigned variable.

#152 does fix this issue, but still calling fetch() on a template object with same template name as in createTemplate() does not make sense and has bad performance.

@uwetews uwetews closed this as completed Jul 11, 2016
@ghost
Copy link
Author

ghost commented Jul 19, 2016

You are absolutely right, I'm calling the same template twice and I didn't even know it! Thank you for your help and for fixing this small bug.

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

1 participant