Fill template in code

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
cangor
Registered User
Posts: 86
Joined: Thu Oct 28, 2010 1:56 pm

Fill template in code

Post by cangor »

Hello!

Imagine you have a template like (just an example) in a file:

Code: Select all

This is my {TEMPLATE}. I want to fill in all {DATA}.
Is there a way to give that template and the variables (TEMPLATE, DATA) to phpBB and get the evaluated text back in a php-variable?
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: Fill template in code

Post by david63 »

You are going to have to explain a bit clearer as to what it is that you are trying to do because the way I am reading your post that is, in principle, exactly how templating works ib phpBB - unless I am totally misunderstanding what you are doing.
David
Remember: You only know what you know and - you don't know what you don't know!

I now no longer support any of my extensions but they will start to become available here
cangor
Registered User
Posts: 86
Joined: Thu Oct 28, 2010 1:56 pm

Re: Fill template in code

Post by cangor »

Thanks for your reply. I want to add some code (styles and javascript) to
{$STYLESHEETS} in overall_header.html

So what I'm doing now in the event page_footer_after is:

Code: Select all

$txt = $template->retrieve_var('STYLESHEETS');
$txt .= $mycode;
$template->assign_var('STYLESHEETS', $txt);
So $mycode contains my code ;) So far I'm building $mycode like

Code: Select all

$mycode = '<script type="text/javascript">
function myFunc()
{	...
}
setInterval(function(){ myFunc('.$myParam1.', '.$myParam2.'), '.$myInterval.');
</script>';
But I would prefer creating a file MyTemplate.html like

Code: Select all

<script type="text/javascript">'
function myFunc()
{	...
}
setInterval(function(){ myFunc({MYPARAM1}, {MYPARAM2}) }, {MYINTERVAL});
</script>
and using a code like:

Code: Select all

$vars = array('MYPARAM1', 'MYPARAM2', 'MYINTERVAL');
$txt = $template->retrieve_var('STYLESHEETS');
$txt .= evaluate_template('MyTemplate.html', $vars);
$template->assign_var('STYLESHEETS', $txt);
Is there any easy way to do so?
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Fill template in code

Post by mrgoldy »

Just create your own template file

Code: Select all

<!-- INCLUDE overall_header.html -->

<!-- INCLUDECSS @cangor_yourextensionname/cssfilename.css -->

Your body here ..

<!-- INCLUDEJS @cangor_yourextensionname/jsfilename.js -->

<!-- INCLUDE overall_footer.html -->
I suggest you have a thorough look through the docs!
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
cangor
Registered User
Posts: 86
Joined: Thu Oct 28, 2010 1:56 pm

Re: Fill template in code

Post by cangor »

The thing is I don't want that the users of my extension have to fiddle with the templates. But I want to offer my extention as flexible as possible so the users of my extension might change MyTemplate.html for their own needs and purposes. Thats the reason for coming up with my own template being "injected" into the normal prosilver-template.
User avatar
kasimi
Former Team Member
Posts: 4900
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Fill template in code

Post by kasimi »

Twig can do this easily for you:

Code: Select all

$template = 'This is my {{ TEMPLATE }}. I want to fill in all {{ DATA }}.';
$twig = new \Twig_Environment(new \Twig_Loader_Array(['template' => $template]));
echo $twig->render('template', [
    'TEMPLATE' => 'awesome template',
    'DATA'     => 'my data',
]);

// This is my awesome template. I want to fill in all my data.
cangor
Registered User
Posts: 86
Joined: Thu Oct 28, 2010 1:56 pm

Re: Fill template in code

Post by cangor »

Exactly what I needed - thanks a lot!
Post Reply

Return to “Extension Writers Discussion”