XF 2.1 Failure to apply template modifications with custom tags

Arty

Well-known member
I have an issue with add-on setup that I don't know how to solve.

Add-on creates several custom template tags. Those template tags are used in template modifications. Problem is, template modifications fail to apply during add-on installation because event listeners aren't working, so custom tags aren't working, so templates cannot be compiled.

To add custom tags I'm listening to app_setup event:
Code:
<?php

namespace Iconify\Iconify;

use Iconify\Iconify\Template\Compiler\Tag\Icon;
use Iconify\Iconify\Template\Compiler\Tag\IconBoxRow;

class EventListener
{
    public static function appSetup(\XF\App $app)
    {
        $compiler = $app->templateCompiler();
        $templater = $app->templater();

        $compiler->setTag('icon', new Icon('icon'));
        $compiler->setTag('inlineicon', new Icon('inlineicon'));
        $compiler->setTag('iconbox', new IconBoxRow('iconbox'));
        $compiler->setTag('iconboxrow', new IconBoxRow('iconboxrow'));

        $templater->setPageParam('head.js-iconify', $templater->preEscaped('<script src="//code.iconify.design/1/1.0.0-rc5/iconify.min.js"></script>'));
    }
}
How do add same tags to compiler during add-on installation or uninstallation?

Or is it impossible to have add-on that creates custom tags and uses them in templates and template modifications?

Whole code is posted here: https://github.com/cyberalien/iconify_xf2
 
Additional issue: if those modifications are manually applied by simply opening them in admin panel and clicking submit, add-on uninstallation fails with error
Code:
Oops! We ran into some problems.
Line 69: Unknown tag iconboxrow encountered.
 
Solved this by splitting code into 2 add-ons: one that defines all custom tags, one that adds template modifications. When first add-on is installed tags become available, so installation of second add-on doesn't fail. Uninstall also works fine: first uninstalling add-on with template modifications, then uninstalling add-on that creates tags.

But I don't like this solution. Is there a simpler way that would allow to keep it as one add-on? My add-on is already designed for add-on authors, not for forum owners, so with such split forum owners will be expected to install at least 3 add-ons in specific order.
 
Adding custom templater tags is totally not something we directly allow intentionally -- you can see a comment in the app that explains why this isn't doable. Your approach, regardless of number of add-ons, will break upgrades as the upgrade process doesn't run add-ons. (Try a master rebuild and you should see it error.)

The only way we "support" adding custom template tags is by editing config.php so that they are added independent of add-ons. (You can extend elements of the DI container here.)

Saying that, you can add templater functions safely as those are analyzed at run time, so if you want this to be add-on related, you'd need to do something like {{ iconbox(...) }} though that may not be that easy to work with if you're allowing arbitrary attributes, etc.
 
Yep, it does cause errors when rebuilding master data. Why though? Why does Templater have ability to add custom tags when its not used at all and cannot possibly be used?

Oh well. :( I'll try to rewrite it using macros instead.

Btw, when testing 2 add-ons method just few minutes ago I found a bug. Bug report incoming :)
 
The only way we "support" adding custom template tags is by editing config.php so that they are added independent of add-ons. (You can extend elements of the DI container here.)
Actually this is brilliant! Thanks a lot!

I've managed to make it work during setup and rebuilding master data and when event listeners are disabled. And now it all makes sense.
 
Back
Top Bottom