As designed Rendering a template in a widget in position 'Public' produces '[ErrorException] No template type was provided'

refael

Well-known member
Affected version
2.0.0 beta 1
In a custom widget, I want to render a template and pass it as a parameter to the widget template.
My widget render function looks something like this
PHP:
...
class Widget extends AbstractWidget
{
    ...
    public function render()
    {
        ...
        $viewParams = [
            ...
            'latest_posts' => $this->app->templater()->renderTemplate('example_template', [...]),
        ];

        return $this->renderer('widget_example_name', $viewParams);
    }
}

It works fine except in position 'Public: sidebar top' and 'Public: sidebar bottom', it throws an error
PHP:
An exception occurred: [ErrorException] No template type was provided. Provide template name in type:name format. in C:\www\xf2\src\XF\Template\Templater.php on line 1104
It expects me to type 'public:example_template'.. which is unexpected as this isn't an issue in other sidebar positions.
 
I think this is working as designed, all standard XF calls to renderTemplate() do pass the template type.

That it did work in other locations without the type seems coincidence, eg. the Templater had currentTemplateType set and thus guessed the type.

But besides that:
Why are you directly rendering a template?
IMHO it would be better to just pass the data into viewParams and include the sub template in your main template.
 
Why are you directly rendering a template?
IMHO it would be better to just pass the data into viewParams and include the sub template in your main template.
I simplified it for a shorter test case. But I do a lot of logic on the template before rendering it which is harder/less clean to do when including. It's also not always the same template, the template name is dynamic based on the widget options.
 
I created a HTML widget, added some Adsnese code and put it in the Public: Sidebar top position. It gave this error:

Code:
An exception occurred: [ErrorException] No template type was provided. Provide template name in type:name format. in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Template/Templater.php on line 1104

XF::handlePhpError()
trigger_error() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Template/Templater.php at line 1104
XF\Template\Templater->renderTemplate() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Widget/Html.php at line 17
XF\Widget\Html->render() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Template/Templater.php at line 1397
XF\Template\Templater->widgetPosition() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Pub/App.php at line 411
XF\Pub\App->renderPageHtml() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/App.php at line 1606
XF\App->renderPage() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Mvc/Dispatcher.php at line 293
XF\Mvc\Dispatcher->render() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Mvc/Dispatcher.php at line 44
XF\Mvc\Dispatcher->run() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/App.php at line 1771
XF\App->run() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF.php at line 319
XF::runApp() in /home/sites/xf.spurscommunity.co.uk/public_html/index.php at line 13

If I put it in a different position then it's fine.
 
I created a HTML widget, added some Adsnese code and put it in the Public: Sidebar top position. It gave this error:

Code:
An exception occurred: [ErrorException] No template type was provided. Provide template name in type:name format. in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Template/Templater.php on line 1104

XF::handlePhpError()
trigger_error() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Template/Templater.php at line 1104
XF\Template\Templater->renderTemplate() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Widget/Html.php at line 17
XF\Widget\Html->render() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Template/Templater.php at line 1397
XF\Template\Templater->widgetPosition() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Pub/App.php at line 411
XF\Pub\App->renderPageHtml() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/App.php at line 1606
XF\App->renderPage() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Mvc/Dispatcher.php at line 293
XF\Mvc\Dispatcher->render() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Mvc/Dispatcher.php at line 44
XF\Mvc\Dispatcher->run() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/App.php at line 1771
XF\App->run() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF.php at line 319
XF::runApp() in /home/sites/xf.spurscommunity.co.uk/public_html/index.php at line 13

If I put it in a different position then it's fine.
This is certainly a bug in that it was generated by the default Html widget, I think. I've merged your post with a similar report.
 
In a custom widget, I want to render a template and pass it as a parameter to the widget template.
My widget render function looks something like this
PHP:
...
class Widget extends AbstractWidget
{
    ...
    public function render()
    {
        ...
        $viewParams = [
            ...
            'latest_posts' => $this->app->templater()->renderTemplate('example_template', [...]),
        ];

        return $this->renderer('widget_example_name', $viewParams);
    }
}

It works fine except in position 'Public: sidebar top' and 'Public: sidebar bottom', it throws an error
PHP:
An exception occurred: [ErrorException] No template type was provided. Provide template name in type:name format. in C:\www\xf2\src\XF\Template\Templater.php on line 1104
It expects me to type 'public:example_template'.. which is unexpected as this isn't an issue in other sidebar positions.
It was rather unclear under which circumstances you could omit the type prefix and which you couldn't so we've attempted to make it clearer. From Beta 6 calls to XF\Template\Templater::renderTemplate() will always require a type prefix or the call will fail in the same way. So, technically, this error is As Designed and the solution is to specify the type prefix.

I created a HTML widget, added some Adsnese code and put it in the Public: Sidebar top position. It gave this error:

Code:
An exception occurred: [ErrorException] No template type was provided. Provide template name in type:name format. in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Template/Templater.php on line 1104

XF::handlePhpError()
trigger_error() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Template/Templater.php at line 1104
XF\Template\Templater->renderTemplate() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Widget/Html.php at line 17
XF\Widget\Html->render() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Template/Templater.php at line 1397
XF\Template\Templater->widgetPosition() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Pub/App.php at line 411
XF\Pub\App->renderPageHtml() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/App.php at line 1606
XF\App->renderPage() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Mvc/Dispatcher.php at line 293
XF\Mvc\Dispatcher->render() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/Mvc/Dispatcher.php at line 44
XF\Mvc\Dispatcher->run() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF/App.php at line 1771
XF\App->run() in /home/sites/xf.spurscommunity.co.uk/public_html/src/XF.php at line 319
XF::runApp() in /home/sites/xf.spurscommunity.co.uk/public_html/index.php at line 13

If I put it in a different position then it's fine.
Keeping in mind what I said above this was a genuine bug and we should have provided the template type in our calls to the HTML widget template. This has been rectified for Beta 6.
 
Top Bottom