Extensions (PHP) · Changing templates
Changing templates
Every forum template is a hook point, more than a thousand of them. Your extension can add HTML before or after a template, find and replace inside it, or change the data it gets, without copying the template or editing ForumSpark's files. The admin panel's Developer, Templates page lists every name.
Before and after a template
$this->afterTemplate('topics.partials.reply-content', function (array $data) {
return view('harbour-member-map::pin-badge', ['member' => $data['reply']->author]);
});
$this->beforeTemplate('forum.topiclist', fn (array $data) => '<p class="hm-note">Pins are open this week.</p>');
You get the data the template was given and return HTML, a view or nothing. Text from members must be escaped with e().
Find and replace
$this->modifyTemplate('users.show', '<h1 class="ui-pagehead__title">', '<h1 class="ui-pagehead__title hm-title">');
$this->modifyTemplate('board.partials.index-head', '/(<h2[^>]*>)/', '$1<i class="fa-solid fa-map"></i> ', regex: true);
The replacement can also be a function that gets the match and the template data. A pattern that no longer matches after a ForumSpark update simply changes nothing, so the page keeps working; check your changes on each new version.
Changing the data
$this->templateData('topics.show', function (array $data) {
$data['pinCount'] = Pin::query()->where('topic_id', $data['topic']->id)->count();
return $data;
});
Return the changed array. Your extra keys are available in the template and in your other hooks for it.
If something goes wrong
Each hook runs on its own. If one fails, ForumSpark logs it, shows the template without that change and counts the failure, so a broken extension never breaks a page. Hooks only run on forums where your extension is on.
Or use a page slot
For the common places, like under each post or above the topic list, a page slot is simpler and keeps working whatever the theme does with the template.