Page 1 of 2
Within a .tpl file, setting $var equal to {var}
Posted: Fri Dec 29, 2006 1:02 pm
by richmix
Ok. Here's the deal. I've got one php file that parses a template file. Within the template file, I need to include a nav bar that contains php. The code in the navbar needs to check if {var} is defined (which will only happen when the php file parsing the .tpl is run), and if so, set $varid = {var}. Naturally, though, PHP doesn't play nice with the curly braces.
Now, the original PHP parsing the .tpl file is part of a forum I didn't write, but I did define the variable in the correct php file. I am using a mod to allow PHP script in the .tpl files (and parse it correctly). Am I pretty much screwed as far as passing variables to the .tpl file goes, or is it possible to somehow do it?
The following do not work:
$var = {var};
$var = {'var'};
$var = "{var}";
$var = '{var}';
(I know, I know, I just started drawing straws after awhile.

) If anyone knows if this is possible, please, please help!
Posted: Fri Dec 29, 2006 2:13 pm
by alex.barylski
Is this is a custom template engine, Smarty or PHP native?
Posted: Fri Dec 29, 2006 2:19 pm
by richmix
It's phpBB's template engine.
Posted: Fri Dec 29, 2006 2:22 pm
by alex.barylski
Hmmmm sorry dude...not familiar phpBB

Posted: Fri Dec 29, 2006 2:23 pm
by richmix
Hockey wrote:Hmmmm sorry dude...not familiar phpBB

This
shouldn't be hard. Why is it?
/sigh why do people have to make integrating two elements of the same system so difficult?
Posted: Fri Dec 29, 2006 2:43 pm
by John Cartwright
richmix wrote:Hockey wrote:Hmmmm sorry dude...not familiar phpBB

This
shouldn't be hard. Why is it?
/sigh why do people have to make integrating two elements of the same system so difficult?
This seems difficult because templates are typically the end result of processing. Little to no processing should be done in template files, atleast in a properly designed application.
Posted: Fri Dec 29, 2006 2:47 pm
by richmix
Jcart wrote:richmix wrote:Hockey wrote:Hmmmm sorry dude...not familiar phpBB

This
shouldn't be hard. Why is it?
/sigh why do people have to make integrating two elements of the same system so difficult?
This seems difficult because templates are typically the end result of processing. Little to no processing should be done in template files, atleast in a properly designed application.
Yes, I understand that. But I can't figure out a better way to integrate my dynamic header with this particular style. Instead of learning what makes phpBB tick from the ground up, I'm really hoping it's possible to do this with the templates. I'm not really interested in tearing apart the entire code structure. That could turn out even worse than what I'm trying to do now.
Posted: Fri Dec 29, 2006 2:49 pm
by John Cartwright
I never implied that, I was simply replying to "This shouldn't be hard. Why is it?". You could always have .tpl files processed by php, and simply include() your header.
Posted: Fri Dec 29, 2006 3:10 pm
by RobertGonzalez
You don't assign vars like that in the phpBB template code. you use the method assign_var_from_handle() (in the template class).
Posted: Fri Dec 29, 2006 4:43 pm
by richmix
Everah wrote:You don't assign vars like that in the phpBB template code. you use the method assign_var_from_handle() (in the template class).
I know, but I need to use one of those variables in the included header. I need some way to reference it in the template file, but not just normally; I need to reference it in PHP code. The normal {var} reference doesn't work.
Jcart, if you have any idea how to accomplish that in phpBB, I'd much appreciate it if you could let me know. I'm fairly new to PHP, and while I'm able to manage on my own, I'm not up to the level of being able to sort through the page_header.php file by myself. Too much stuff I've never seen before.

I'm working on it though.
Thanks for the tips! If you guys have any others for me, please toss them my way!
Posted: Fri Dec 29, 2006 5:08 pm
by RobertGonzalez
Do you have any code already done? I am having a whale of a time trying to understand why this is that difficult. Are you trying to use the template to actually parse PHP code? Or is teh PHP doing things that can actually generate a part of the template that you can use and then include into your other template by assign_var_from_handle?
Posted: Fri Dec 29, 2006 5:10 pm
by richmix
Well, ok. Here. I'll just ask you this. The line that parses the .tpl file in the original php that calls it is:
$template->pparse('overall_header');
How can I append included PHP onto this?
I still don't fully understand objects and the whole -> deal.
Posted: Fri Dec 29, 2006 7:33 pm
by Z3RO21
Read up on objects, search the forum for some information on how template systems work, and google phpBB intergration.
Posted: Sat Dec 30, 2006 8:54 am
by RobertGonzalez
richmix wrote:Well, ok. Here. I'll just ask you this. The line that parses the .tpl file in the original php that calls it is:
$template->pparse('overall_header');
How can I append included PHP onto this?
I still don't fully understand objects and the whole -> deal.
You don't. What you do is create the code you want and assign that code to a 'handle', then assign that handle that a var that is parsed inside the template. Before the call to ppares, you should things like this...
Code: Select all
<?php
// THESE ARE PURELY FOR ILLUSTRATION!
$myvar = 'var_value';
$function_return = get_function_return_value();
$template->assign_vars(array(
'MYVAR' => $myvar,
'FUNC_RETURN' => $function_return)
);
// Then parse the page
$template->pparse('template_name.tpl');
?>
To essentially include a template into another template, I have done this...
Code: Select all
<?php
// Make a function to handle the parsing of the include
function include_other_template()
{
// This tells the app which template to parse,
// The file name becomes the HANDLE
$tpl->set_filenames(array(
'left_column' => ( $expanded ) ? 'left_col_expanded.tpl' : 'left_col_compacted.tpl')
);
// This assigns some vars
// As you can see, I have used some of their stuff in this app
// ALSO NOTE: this was a long time ago
$tpl->assign_vars(array(
'COLUMN_WIDTH' => $right_column_width,
'NO_ANNOUNCEMENTS' => sprintf($lang['no_announcements'], append_sid('announcements.php')))
);
$tpl->assign_var_from_handle('LEFT_COLUMN', 'left_column');
return;
}
// Call the included file
include_other_template();
// Parse the main template
$tpl->pparse('body.tpl');
?>
Then, inside of 'body.tpl' (or whatever template you are parsing) add the var you assigned by the handle...
Code: Select all
<table>
<tr>
<td>{LEFT_COLUMN}</td>
<td><!-- OTHER STUFF YOU HAVE --></td>
</tr>
</table>
This is a stripped down version of what you want to do, but it very simple to implement.
Posted: Sat Dec 30, 2006 12:48 pm
by richmix
Hmm, ok. I think I understand a little better now. I'll have to find some basic tutorials on working with objects. Thanks!