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

calling fetch() within custom function not working #70

Closed
talisto opened this issue Jul 6, 2015 · 2 comments
Closed

calling fetch() within custom function not working #70

talisto opened this issue Jul 6, 2015 · 2 comments

Comments

@talisto
Copy link

talisto commented Jul 6, 2015

Using the following code:

function smarty_function_test($params, $template)
{
    return $template->fetch('string:testing');
}

$smarty = new Smarty();
$smarty->registerPlugin('function', 'test', 'smarty_function_test');
$smarty->display('string:{test}');

with Smarty version 3.1.21, it works as expected, outputting testing. However, with any of the released versions after 3.1.21 including the latest 3.1.27, it generates an error:

PHP Fatal error:  Maximum function nesting level of '100' reached, aborting! in smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php on line 164
PHP Stack trace:
PHP   1. {main}() smarty/index.php:0
PHP   2. Smarty->display() smarty/index.php:12
PHP   3. Smarty->fetch() smarty/vendor/smarty/smarty/libs/Smarty.class.php:834
PHP   4. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/Smarty.class.php:820
PHP   5. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP   6. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP   7. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP   8. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP   9. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  10. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  11. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  12. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  13. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  14. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  15. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  16. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  17. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  18. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  19. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  20. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  21. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  22. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  23. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  24. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  25. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  26. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  27. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  28. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  29. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  30. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  31. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  32. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  33. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  34. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  35. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  36. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  37. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  38. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  39. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  40. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  41. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  42. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  43. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  44. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  45. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  46. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  47. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  48. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  49. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  50. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  51. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  52. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  53. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  54. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  55. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  56. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  57. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  58. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  59. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  60. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  61. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  62. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  63. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  64. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  65. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  66. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  67. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  68. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  69. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  70. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  71. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  72. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  73. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  74. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  75. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  76. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  77. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  78. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  79. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  80. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  81. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  82. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  83. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  84. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  85. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  86. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  87. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  88. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  89. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  90. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  91. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  92. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  93. Smarty_Internal_Template->fetch() smarty/index.php:7
PHP  94. Smarty_Internal_Template->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:142
PHP  95. Smarty_Template_Compiled->render() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:246
PHP  96. Smarty_Internal_Template->getRenderedTemplateCode() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php:202
PHP  97. content_559afb87eab359_26465742() smarty/vendor/smarty/smarty/libs/sysplugins/smarty_internal_template.php:371
PHP  98. smarty_function_test() smarty/templates_c/5647c846453a2f32c92204618a91b18bee9cf698_0.string.php:26
PHP  99. Smarty_Internal_Template->fetch() smarty/index.php:7

Interestingly, I tried it with the latest dev-master, and it doesn't generate an error, but it doesn't generate any output, either. So both the latest release and the dev-master are broken.

Also, it doesn't matter if the function is calling a string template or an actual file template, it generates the same error.

@talisto talisto changed the title calling fetch() within custom function not working after version 3.1.21 calling fetch() within custom function not working Jul 6, 2015
uwetews added a commit that referenced this issue Jul 7, 2015
…to get output from other template

     like $template->fetch('foo.tpl') #70
@uwetews
Copy link
Contributor

uwetews commented Jul 7, 2015

It was originally not intended that you can call fetch() and display() on a template object with parameters to get output from another template. The correct code of your function should be

function smarty_function_test($params, $template)
{
    return $template->smarty->fetch('string:testing');
}

But I have added this now as a feature,
The patch is in dev-master and will be included in the next release.

Another note:
If your function name has the standard plugin format like 'smarty_function_test' you don't need
to register it.

@uwetews uwetews closed this as completed Jul 7, 2015
@talisto
Copy link
Author

talisto commented Jul 7, 2015

Ok, great, thanks for the clarification! I suppose the documentation is a bit vague, which lead me to believe my code should work:

"If the function needs to assign some variables to the template or use some other Smarty-provided functionality, it can use the supplied $template object to do so eg $template->foo()"

(emphasis mine). Perhaps a mention that the Smarty object should be referenced with $template->smarty would be useful on that page?

@uwetews uwetews mentioned this issue Sep 18, 2015
think-mcunanan pushed a commit to think-mcunanan/smarty that referenced this issue Mar 22, 2023
Merge release-2.0.7.2 into master branch
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