How can I get the $template data?

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
Siyu
Registered User
Posts: 4
Joined: Fri Sep 26, 2014 6:52 am

How can I get the $template data?

Post by Siyu »

:D Hi,men!

It seems that I need to get the $template value in my extension but I can not reach it.
Actually I downloaded the ACME-DEMO viewtopic.php?f=461&t=2211276. In this system, there's a file /tests/mock/template.php, which is used to mock class template. But it doesn't work :cry: .

When proceeding my extension it tells me:

Code: Select all

 Call to protected method phpbb\template\twig\twig::get_template_vars() from context 'acme\demo\controller\main' in D:\xampp\htdocs\phpBB3v3\ext\acme\demo\controller\main.php on line 52
:geek: What should I do ?

THANKS!!!
User avatar
PayBas
Former Team Member
Posts: 930
Joined: Thu May 25, 2006 12:37 am

Re: How can I get the $template data?

Post by PayBas »

What are you trying to do?

Usually, if you want to get/modify template data, you use events to change the output before it is sent to the template engine.
Siyu
Registered User
Posts: 4
Joined: Fri Sep 26, 2014 6:52 am

Re: How can I get the $template data?

Post by Siyu »

PayBas wrote:What are you trying to do?

Usually, if you want to get/modify template data, you use events to change the output before it is sent to the template engine.
Sometimes the data I want do not appear in the events, just like some error messages. For example, when I login with bad username or password, the $err message could be shown as title in the next login box, then I will know that I set the incorrect username/password. It is produced in the function

Code: Select all

function login_box($redirect = '', $l_explain = '', $l_success = '', $admin = false, $s_display = true)
. And I can not handle it with events, so I have to catch it in the template.
User avatar
hexplor
Registered User
Posts: 31
Joined: Sat Feb 16, 2008 3:34 pm
Name: Karol

Re: How can I get the $template data?

Post by hexplor »

Might be useful for others:

Code: Select all

global $phpbb_container;
$context = $phpbb_container->get('template_context');
$rootref = &$context->get_root_ref();
Then to get already defined template variable, you use something like:

Code: Select all

$rootref['YOUR_TEMPLATE_VARIABLE_NAME'];
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: How can I get the $template data?

Post by javiexin »

WOW!!! I had been looking for this! Very handy in extensions when the data you are looking for is not available in the event data!
Post Reply

Return to “Extension Writers Discussion”