XF 2.3 How to update template through PHP Callback?

bzcomputers

Well-known member
Building an add-on in which I'm trying to figure out the process of getting an existing template (or creating it if it doesn't exist), update its' contents, resave it, and update the cache. I think that covers the whole process.

Seems my many tries at it have failed. I have searched everywhere for any documentation on this and have found nothing. If anyone could provide a link to some info or a generic sample code it would be appreciated. Thanks.
 
What is your use case? This is almost always something you should be doing with the template modification system.
 
Trying to update a template based off an add-ons admin options. These options include settings for associating FA icon styles and names to existing navigation items (nav-ids). I've used PHP callback to process the input and create valid LESS content now I just need to update the add-ons LESS template with this updated content.
 
You can just use the option directly:

Less:
<xf:if is="$xf.options.some_option === 'some_value'">
    .some-class
    {
        // some code
    }
<xf:else />
    .some-class
    {
        // some other code
    }
</xf:if>

The issue with that is that updating the option won't cause the template to be recompiled. You can fix that by setting a special option validation callback that we bundle, XF\Option\Style::triggerStyleUpdate.
 
One option is a text box option for custom input of navid:iconname pairs. Which would not work this way.
 
Last edited:
I see. I would consider if there's a better way to do it, but given the constraints it might be ok to do dynamic updates of the template contents then. They're just normal entities at the end of the day so you should be able to do something like:

PHP:
$template = \XF::em()->findOne(\XF\Entity\Template::class, [
    'style_id' => 0,
    'type' => 'public',
    'title' => 'your_template.less',
]);
$template->template = 'new contents';
$template->save();
 
Back
Top Bottom