how to extract variables out of $template?

General discussion of development ideas and the approaches taken in the 3.x branch of phpBB. The current feature release of phpBB 3 is 3.3/Proteus.
Forum rules
Please do not post support questions regarding installing, updating, or upgrading phpBB 3.3.x. If you need support for phpBB 3.3.x please visit the 3.3.x Support Forum on phpbb.com.

If you have questions regarding writing extensions please post in Extension Writers Discussion to receive proper guidance from our staff and community.
Post Reply
User avatar
rfdy
Registered User
Posts: 45
Joined: Wed Apr 16, 2014 2:28 pm

how to extract variables out of $template?

Post by rfdy »

Is it at all possible to extract blocks out of $template, so they can be manipulated by the extension before being reassigned?

Trying to override the breadcrumbs functionality, but that function sets template variables directly.

This kind of stuff happens in a few places and is very difficult to code around, where arrays are assigned to $template directly, rather than calling an event before they are assigned. Similar to the core.viewtopic_get_post_data event, which doesn't give access to the template variables before they are assigned. (very frustrating!).

Thanks!
User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 1867
Joined: Thu Mar 02, 2006 4:29 pm
Location: Earth
Contact:

Re: how to extract variables out of $template?

Post by DavidIQ »

Yes. You can iterate through $template->_tpldata to get what you need. Maybe this might help:
https://github.com/DavidIQ/phpBB-ReIMG- ... _reimg.php
Image
User avatar
rfdy
Registered User
Posts: 45
Joined: Wed Apr 16, 2014 2:28 pm

Re: how to extract variables out of $template?

Post by rfdy »

Thanks! I'll try it soon.
User avatar
rfdy
Registered User
Posts: 45
Joined: Wed Apr 16, 2014 2:28 pm

Re: how to extract variables out of $template?

Post by rfdy »

Just tried this out and it didn't work.

print_r($template->_tpldata); results in: Undefined property: phpbb/template/twig/twig::$_tpldata

Any other suggestions?
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 371
Joined: Thu Sep 16, 2004 9:02 am
Contact:

Re: how to extract variables out of $template?

Post by Paul »

DavidIQ wrote:Yes. You can iterate through $template->_tpldata to get what you need. Maybe this might help:
https://github.com/DavidIQ/phpBB-ReIMG- ... _reimg.php
I don't think this still works now 3.1 uses twig.
Nicofuma
3.2 Release Manager
3.2 Release Manager
Posts: 299
Joined: Sun Apr 13, 2014 1:40 am
Location: Paris

Re: how to extract variables out of $template?

Post by Nicofuma »

it is still possible, you just need to retrieve the template_context service:
https://github.com/phpbb/area51-phpbb3/ ... hp#L66-L68
Member of the phpBB Development-Team
No Support via PM
User avatar
EXreaction
Registered User
Posts: 1555
Joined: Sat Sep 10, 2005 2:15 am

Re: how to extract variables out of $template?

Post by EXreaction »

alter_block_array is probably what you're looking for.
User avatar
rfdy
Registered User
Posts: 45
Joined: Wed Apr 16, 2014 2:28 pm

Re: how to extract variables out of $template?

Post by rfdy »

Using alter block with the breadcrumbs for example is cumbersome. It was much easier to just destroy the entire "navlinks" block and recreate it.

Mentioned this before, but: events should really be called with template array data before it is assigned to the template.
User avatar
VSE
Extension Customisations
Extension Customisations
Posts: 674
Joined: Mon Mar 08, 2010 9:18 am

Re: how to extract variables out of $template?

Post by VSE »

rfdy wrote:Is it at all possible to extract blocks out of $template, so they can be manipulated by the extension before being reassigned?

Trying to override the breadcrumbs functionality, but that function sets template variables directly.

This kind of stuff happens in a few places and is very difficult to code around, where arrays are assigned to $template directly, rather than calling an event before they are assigned. Similar to the core.viewtopic_get_post_data event, which doesn't give access to the template variables before they are assigned. (very frustrating!).

Thanks!
Propose changes and events in those situations in a PR or RFC here. For example: https://github.com/phpbb/phpbb/pull/1915/files#diff-3
Has an irascible disposition.
User avatar
rfdy
Registered User
Posts: 45
Joined: Wed Apr 16, 2014 2:28 pm

Re: how to extract variables out of $template?

Post by rfdy »

We did exactly that. And was strongly rejected. The event included "modify template" in its name, yet the array was assigned to the template before the event was called. Our suggestion to fix that was denied claiming we should use "global $template". We found a way around our initial request, but other events still contain the same basic issue.

We have some general concerns over the events system as a whole. Our feeling is that they are not being carefully considered before they are added, which will create major issues going forward as any event once in 3.1-stable will probably not change until 3.2-stable as it'll break backwards compatibility. That is, once the events are added, refactoring becomes very difficult if not impossible without changing the way events behave.
Post Reply