Rendering templates of other extensions

Discussion forum for Extension Writers regarding Extension Development.
User avatar
kasimi
Former Team Member
Posts: 4900
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany

Rendering templates of other extensions

Post by kasimi »

There have been similar questions ([1] & [2]) but I'm creating a new topic because I'm asking about template files of other extensions specifically.

I have the following scenario: Extension A and extension B have routes /a and /b, respectively. They each display some sweet content using their template files /ext/kasimi/a/styles/prosilver/template/a_content.html and /ext/kasimi/b/styles/prosilver/template/b_content.html.

Now imagine an extension C that wants to display the information from A's page on other pages of the board. This is how I tried to solve it: C hooks into an event that is triggered on every page where A's content should be display.

Code: Select all

// Event handler in extension C
public function event_handler()
{
    // Assign content of A's page to the template
    $this->ext_a_controller->assign_a_content();
}
C then subscribes to a template event to include A's content:

Code: Select all

<!-- INCLUDE a_content.html --> displayed by C
This doesn't work because A's template file a_content.html can't be found. I fixed this by adding the following code to C's event_handler() above:

Code: Select all

$this->template->set_style(array(
    'ext/kasimi/c/styles', // so that the template event in C is found
    'ext/kasimi/a/styles', // so that it can render a_content.html
    'styles', // phpBB's core template files
)); 
Now it works, A's a_content.html is displayed on every page where the event is triggered that C subscribed to.

Here's the problem: it doesn't work on B's page at /b - and on any other page where an extension renders something for that matter. It can't find B's b_content.html because the call to set_style() overwrites the paths where the template engine looks for template files.

So, is there a way to add style paths and not overwrite existing ones?
nicofuma
3.2 Release Manager
3.2 Release Manager
Posts: 546
Joined: Sun Apr 13, 2014 1:47 am
Location: Grenoble - France

Re: Rendering templates of other extensions

Post by nicofuma »

You don't have to mess up with set_style. You can simply use <!-- INCLUDE @vendor_extname/a_content.html -->
Member of phpBB Development-Team
No Support via PM
User avatar
kasimi
Former Team Member
Posts: 4900
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany

Re: Rendering templates of other extensions

Post by kasimi »

I didn't expect the solution to be as simple as that. :)

When including a template file in the same extension what's the difference between these two includes? For example, in extension A's a_content.html, is there a difference between using <!-- INCLUDE @kasimi_a/a_another.html --> and <!-- INCLUDE a_another.html -->?

As a general rule, is there any reason to use file.html as opposed to @vendor_extname/file.html when using INCLUDE, INCLUDECSS and INCLUDEJS? What's the recommended approach?
nicofuma
3.2 Release Manager
3.2 Release Manager
Posts: 546
Joined: Sun Apr 13, 2014 1:47 am
Location: Grenoble - France

Re: Rendering templates of other extensions

Post by nicofuma »

For now, known. But after some thoughts, I think it is better to use @vendor_extname/file.html because it may prevent a lot of issues.

So to sum up:
  • When including the template of an extension, always use @vendor_extname/file.html
  • When including the template from the core of phpBB, use file.html
Member of phpBB Development-Team
No Support via PM
User avatar
kasimi
Former Team Member
Posts: 4900
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany

Re: Rendering templates of other extensions

Post by kasimi »

That helped a lot, thanks.

Return to “Extension Writers Discussion”