Fill template in code

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

Fill template in code

Post by cangor » Thu Nov 23, 2017 3:55 pm

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
Jr. Extension Validator
Posts: 13155
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Fill template in code

Post by david63 » Thu Nov 23, 2017 4:01 pm

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!
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 as they will be ignored

cangor
Registered User
Posts: 39
Joined: Thu Oct 28, 2010 1:56 pm

Re: Fill template in code

Post by cangor » Thu Nov 23, 2017 4:15 pm

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
posey
Registered User
Posts: 546
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: Fill template in code

Post by posey » Thu Nov 23, 2017 4:36 pm

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!
Ultimate Extensions:
Image Ultimate Blog (coming soon)
Image Ultimate Teams

''I'm pretty sure there's a lot more to life than being really, really, ridiculously good looking. And I plan on finding out what that is.''

cangor
Registered User
Posts: 39
Joined: Thu Oct 28, 2010 1:56 pm

Re: Fill template in code

Post by cangor » Thu Nov 23, 2017 4:46 pm

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
Extension Customisations
Extension Customisations
Posts: 2209
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: Fill template in code

Post by kasimi » Wed Nov 29, 2017 12:33 pm

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: 39
Joined: Thu Oct 28, 2010 1:56 pm

Re: Fill template in code

Post by cangor » Thu Nov 30, 2017 7:40 am

Exactly what I needed - thanks a lot!

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests

cron