Parse/render twig template in php

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
Badhon_raj
Registered User
Posts: 141
Joined: Wed Jan 04, 2012 6:33 pm

Parse/render twig template in php

Post by Badhon_raj » Sun Jan 14, 2018 1:07 pm

Hi,
I was wondering if it is possible to parse/render twig in php.
see the example:

Code: Select all

            $string="{% if true %}Some {{ CONTENT }} in twig template{% endif %}"
            $loader = new Twig_Loader_Array(array(
                'mytemplate' => "$string",
            ));
            $twig = new Twig_Environment($loader);

            $output = $twig->render('mytemplate', $anArray);
I want all the variables available to phpBB, to be available to the render method.
What should I use as the second argument of $twig_>render?

Is it possible to do something like this?
or is there any other way of achieving this?

The purpose is to render some html content for menu for this extension.
viewtopic.php?f=456&t=2438676
Last edited by Badhon_raj on Sun Jan 14, 2018 5:39 pm, edited 1 time in total.

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 2457
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Parse/render twig template in php

Post by kasimi » Sun Jan 14, 2018 1:10 pm

The code you posted should work fine. Are you getting any errors? For another example, see here: viewtopic.php?p=14875171#p14875171

Badhon_raj
Registered User
Posts: 141
Joined: Wed Jan 04, 2012 6:33 pm

Re: Parse/render twig template in php

Post by Badhon_raj » Sun Jan 14, 2018 5:36 pm

kasimi wrote:
Sun Jan 14, 2018 1:10 pm
The code you posted should work fine. Are you getting any errors? For another example, see here: viewtopic.php?p=14875171#p14875171
Sorry I was not clear in the op.
I was wondering what I can use instead of $anArray.
I don't want to specify a set of predefined variable fur the template.
Instead all the variables available to phpBB should be available during that render.
I am still not sure if I described it correctly.
Please let me know.

And. Thanks for your reply.

User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 2457
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Parse/render twig template in php

Post by kasimi » Sun Jan 14, 2018 8:10 pm

Here's a solution that does service replacement. The code is for phpBB 3.2. The template service in phpBB 3.1 has different arguments.

Re-define the template service:

Code: Select all

template:
	class: 'foo\bar\my_template'
	arguments:
		- '@path_helper'
		- '@config'
		- '@template_context'
		- '@template.twig.environment'
		- '%core.template.cache_path%'
		- '@user'
		- '@template.twig.extensions.collection'
		- '@ext.manager'
The new my_template class looks as follows. It needs to extend the core's template service class:

Code: Select all

namespace foo\bar;

class my_template extends \phpbb\template\twig\twig
{
	public function render_string($template)
	{
		$old_loader = $this->twig->getLoader();
		$this->twig->setLoader(new \Twig_Loader_Array(['template' => $template]));
		$content = $this->twig->render('template', $this->get_template_vars());
		$this->twig->setLoader($old_loader);
		return $content;
	}
}
Now you can inject the template service the usual way and call the render_string() method:

Code: Select all

echo $this->template->render_string('{{ S_USERNAME }} is on page {{ PAGE_TITLE }}');
I couldn't get this working with service decoration which is the recommended way in phpBB 3.2. If anyone knows how to do it, please share.

Badhon_raj
Registered User
Posts: 141
Joined: Wed Jan 04, 2012 6:33 pm

Re: Parse/render twig template in php

Post by Badhon_raj » Wed Jan 17, 2018 6:05 pm

bump.

nicofuma
3.2 Release Manager
3.2 Release Manager
Posts: 539
Joined: Sun Apr 13, 2014 1:47 am
Location: Grenoble - France

Re: Parse/render twig template in php

Post by nicofuma » Sun Jan 21, 2018 10:29 am

Actually phpBB template engine IS Twig with an overlay for BC and historic reasons. So you can just use the regular template service and you can assign arrays and objects like with Twig (unlike the 3.0 template engine)
Member of phpBB Development-Team
No Support via PM

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: picaron and 2 guests