Question about template.php

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Post Reply
Without_Boundaries
Registered User
Posts: 75
Joined: Sat Jul 20, 2002 6:35 pm
Location: England
Contact:

Question about template.php

Post by Without_Boundaries »

Well I've been looking through the templating system and such, and I was wondering if anyone could tell me how to use template.php to compile a template with one of my own pages outside of phpBB.
Basically, I'll have my header.inc or something, with the assign_vars in it, and then I'd like to compile it using template.php and a file with all the assign_vars functions in it.

Could anyone give me some advice on how to go about doing this? :)
(I looked through knowledge base and didn't see anything, so I decided to ask here)
Without_Boundaries
Registered User
Posts: 75
Joined: Sat Jul 20, 2002 6:35 pm
Location: England
Contact:

Post by Without_Boundaries »

Does no-one know how to do this?
I thought at least one person might :(
User avatar
Arty
Former Team Member
Posts: 16654
Joined: Wed Mar 06, 2002 2:36 pm
Name: Vjacheslav Trushkin
Contact:

Post by Arty »

First initialize template with something like this:

Code: Select all

$template = new Template($phpbb_root_path . 'templates/subSilver');
then you can use it. The only parameter to that function is path where tpl files are stored.

To show some tpl file you should assign handle to it, and then parse it. Like this:

Code: Select all

$template->set_filenames(array('body' => 'my_template.tpl'));
$template->pparse('body');
'body' is unique handle for tpl file. you can use anything you want as its name.

To assign some variables to template use this:

Code: Select all

$template->assign_vars(array(
 'VAR1' => 'first variable',
 'VAR2' => $second_variable
 ));
and in tpl use {VAR1} and {VAR2} instead. Variables should be assigned before pparse(). Once you assigned variable it can be used in all tpl files that will be shown by script after that.

If you want some code to be shown on certain condition in php script write something like this:

Code: Select all

if(...some condition...)
{
 $template->assign_block_vars('switch_my_condition', array());
}
and in tpl file use this:

Code: Select all

<!-- BEGIN switch_my_condition -->
This code will be shown only if switch_my_condition is assigned with assign_block_vars
<!-- END switch_my_condition -->
If you want to use loops for items that repeat then use assign_block_vars (same as in previous example. actually its exactly the same - in previous example i assigned one loop so code inside switch is shown once). Use it like this:

Code: Select all

for($i=0; $i<count($some_array); $i++)
{
 $template->assign_block_vars('my_loop', array(
  'VAR1' => $some_array['some_variable'],
  'VAR2' => 'This is interation ' . $i
 ));
}
and in tpl file use code like this:

Code: Select all

<!-- BEGIN my_loop -->
var1 = {my_loop.VAR1}
{my_loop.VAR2}
<!-- END my_loop -->
For more examples look in subSilver tpl files and phpBB php files. Simpliest example is faq.php and faq_body.tpl


...i hope my explanation is clear enough.
Vjacheslav Trushkin / Arty.
Free phpBB 3.1 styles | New project: Iconify - modern SVG framework
Without_Boundaries
Registered User
Posts: 75
Joined: Sat Jul 20, 2002 6:35 pm
Location: England
Contact:

Post by Without_Boundaries »

Thank you!
That was perfectly understandable. Thanks a bunch buddy :)

Now to try using this - should be fun 8)
Post Reply

Return to “[2.0.x] MOD Writers Discussion”