Override template viewtopic_body.html

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
soviet9k
Registered User
Posts: 11
Joined: Sat Oct 14, 2017 12:48 pm

Override template viewtopic_body.html

Post by soviet9k » Sat Oct 14, 2017 1:12 pm

Hi everyone,

I realize this is an extension 101 level question, but through searching and looking at some existing extensions, I can't find the best approach. I'm coming from years on a phpBB 3.0 install for a very large community (>40M posts). We have a ton of core file modifications that I'm now migrating to 3.2 extensions.

One very simple thing I'd like to do is override the main viewtopic template based on a URL parameter. E.g.:

Code: Select all

/viewtopic.php?apiview=true&f=3&t=100&start=80
When "apiview=true" is appended to the URL, I'd like to override viewtopic_body.html with a different layout (no header/footer and some different features). Without that parameter I want my extension to just leave the styles alone and use the Prosilver child we've got in place.

Hope this makes sense so far.

One approach I've considered is to just look for this apiview URL parameter in my extension's main class constructor and use the $template->set_custom_style() function, because this same approach may be used to override other templates.

Is there a better approach here? Should I be using an event listener and modifying the template files based on that? The template events seem ideal for injecting new template code, but in my case, I need to remove a good deal of includes. I didn't see a php event specifically for template application.

Cheers and thanks for any help :)

EDIT: I suppose this could also be done without an extension, by using Twig functions in my viewtopic_body.html template to check for the URL parameter, and adjust things accordingly. But I'd love to keep my modifications isolated in extensions that can be easily turned on/off and versioned separately from the rest of the styles.

User avatar
canonknipser
Registered User
Posts: 1131
Joined: Thu Sep 08, 2011 4:16 am
Location: Germany
Name: Frank Jakobs
Contact:

Re: Override template viewtopic_body.html

Post by canonknipser » Sat Oct 14, 2017 1:38 pm

It can be done without any modfication: just append the parameter style=nnn (where nnn is the running number of your style which can be found in acp - styles section by checking the id-value on editing a style setting) to your url
Greetings
Frank
phpbb.de support team member
English is not my native language
New arrival - Extensions and scripts for phpBB
no support via PM or mail

soviet9k
Registered User
Posts: 11
Joined: Sat Oct 14, 2017 12:48 pm

Re: Override template viewtopic_body.html

Post by soviet9k » Sat Oct 14, 2017 1:51 pm

Ahah! Excellent @canonknipser! I didn't realize that was possible. Definitely the easiest approach here. Still curious if it's possible to do the same thing via an extension, but I already have a test using your method that works great.

Many thanks!

User avatar
david63
Jr. Extension Validator
Posts: 12939
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Override template viewtopic_body.html

Post by david63 » Sat Oct 14, 2017 1:55 pm

If you are going down the extension path then you could probably do it by using routing
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email or as they will be ignored

rxu
Extensions Development Team
Posts: 2767
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: Override template viewtopic_body.html

Post by rxu » Sat Oct 14, 2017 2:22 pm

I never tested that, but I'd try checking the URL parameter and then reassigning the template body via $template->set_filenames call within the (for example) core.page_footer event listener.

soviet9k
Registered User
Posts: 11
Joined: Sat Oct 14, 2017 12:48 pm

Re: Override template viewtopic_body.html

Post by soviet9k » Sat Oct 14, 2017 10:18 pm

For some context, we have a main Wordpress site with comments that are powered by phpBB, and a forums area that is more or less pure phpBB. For the Wordpress site, we use curl to pull in a stripped down version of viewtopic.php (among others) that is just the isolation topic html with no headers or footers. Using the method that canonknipser mentioned of appending style=n to the URL and defining a new style is certainly the easiest approach from what I can see.

However, this requires us to open up user styles for everyone when we really want to enforce a main forum style and only use this substyle when we're pulling it into the external Wordpress site. I hope that makes sense. I've also messed around with rxu's suggestion of hooking into the core.page_footer event, but that ends up becoming more complex than I'd like.

Does anyone know if we can use the style=x URL parameter without opening up user styles for everyone, or is there someway to override that via extension?

Sorry for all the questions - thanks!
Soviet

rxu
Extensions Development Team
Posts: 2767
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: Override template viewtopic_body.html

Post by rxu » Sun Oct 15, 2017 4:29 am

Well, one of the options is overriding the style var value using $request->overwrite() method in core.user_setup event listener for example.

User avatar
canonknipser
Registered User
Posts: 1131
Joined: Thu Sep 08, 2011 4:16 am
Location: Germany
Name: Frank Jakobs
Contact:

Re: Override template viewtopic_body.html

Post by canonknipser » Sun Oct 15, 2017 6:53 am

For using style=n, there is not need to have the style activated in acp, it just needs to be installed. A not activated style can't be selected by users in their ucp
Greetings
Frank
phpbb.de support team member
English is not my native language
New arrival - Extensions and scripts for phpBB
no support via PM or mail

soviet9k
Registered User
Posts: 11
Joined: Sat Oct 14, 2017 12:48 pm

Re: Override template viewtopic_body.html

Post by soviet9k » Sun Oct 15, 2017 1:47 pm

You guys are great - I would never have thought of that canonknipser. Works perfectly.

And rxu - great suggestion too. Luckily, trying to find a way to do this has given me a huge leg up on converting some of our other core modifications as I now have a much better handle (no pun intended) on the extension system and PHP/template hooks.

Thanks everyone - I'm sure this isn't the last plea for help :)

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: Lovemaster, MohamedAG and 11 guests

cron