XF 2.2 Template in widget

DreamNetworks

Active member
Hi All,

Just starting to develop.

Now I'm creating a widget, but the content in the widget will be HTML.

Do I really need to create a widget definition and at the html seperatly in a .php file?

Or is it possible to link a certain template (From the dev ACP) directly to the widget?

Kind Regards,
Alex
 
If it doesn't need any PHP at all then you can just create a HTML widget:

View attachment 259504

You can then write your HTML directly in the template field:

View attachment 259505
Hi Chris,

Thanks for your reply.

I guess I forgot to mention that I'm creating an addon, that should install automatically a widget, that directly shows upon install but contains html content, but don't know how to add the HTML content directly..?

Greetz,
Alex
 
It's a little more complicated but it can be done.

You can create any template, associate it with your add-on, and populate it with the content you want in your widget. When you're inserting that widget via your add-on Setup class, the insertion code just needs to pass in the template name you created along with any other options and the positions you want to show the widget in:

PHP:
$this->createWidget('widget_key', 'html', [
   'positions' => [
      'forum_list_sidebar' => 1
   ],
   'options' => [
      'template_title' => 'your_template_name'
   ]
]);

But note that by doing this, the admins can modify the content of the template if they wish directly via the widget edit page in the admin control panel. This may or may not be desirable.

If it isn't desirable then you'll probably want to create your own widget definition class, which will also give you more flexibility in terms of config options for the widget if that is desirable.
 
It's a little more complicated but it can be done.

You can create any template, associate it with your add-on, and populate it with the content you want in your widget. When you're inserting that widget via your add-on Setup class, the insertion code just needs to pass in the template name you created along with any other options and the positions you want to show the widget in:

PHP:
$this->createWidget('widget_key', 'html', [
   'positions' => [
      'forum_list_sidebar' => 1
   ],
   'options' => [
      'template_title' => 'your_template_name'
   ]
]);

But note that by doing this, the admins can modify the content of the template if they wish directly via the widget edit page in the admin control panel. This may or may not be desirable.

If it isn't desirable then you'll probably want to create your own widget definition class, which will also give you more flexibility in terms of config options for the widget if that is desirable.
I have implemented the above code, everything looks like it's working fine except for this error:

Template errors​

  • Template public:: [E_WARNING] include(/internal_data/code_cache/templates/l1/s1/public/.php): Failed to open stream: No such file or directory (src/XF/Template/Templater.php:313)
  • Template public:: [E_WARNING] include(): Failed opening 'internal_data/code_cache/templates/l1/s1/public/.php' for inclusion (include_path='.:/opt/plesk/php/8.0/share/pear') (src/XF/Template/Templater.php:313)
  • Template public:: [E_USER_WARNING] Template public: is unknown (src/XF/Template/Templater.php:689)
It creates the widget, it creates the template and also with the content within it. But it searching for a .php file...
 
Top Bottom