Within a .tpl file, setting $var equal to {var}

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

richmix
Forum Commoner
Posts: 31
Joined: Fri Dec 22, 2006 5:21 am

Within a .tpl file, setting $var equal to {var}

Post 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. :D ) If anyone knows if this is possible, please, please help!
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Is this is a custom template engine, Smarty or PHP native?
richmix
Forum Commoner
Posts: 31
Joined: Fri Dec 22, 2006 5:21 am

Post by richmix »

It's phpBB's template engine.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Hmmmm sorry dude...not familiar phpBB :)
richmix
Forum Commoner
Posts: 31
Joined: Fri Dec 22, 2006 5:21 am

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
richmix
Forum Commoner
Posts: 31
Joined: Fri Dec 22, 2006 5:21 am

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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).
richmix
Forum Commoner
Posts: 31
Joined: Fri Dec 22, 2006 5:21 am

Post 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. 8O I'm working on it though.

Thanks for the tips! If you guys have any others for me, please toss them my way!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
richmix
Forum Commoner
Posts: 31
Joined: Fri Dec 22, 2006 5:21 am

Post 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.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

Read up on objects, search the forum for some information on how template systems work, and google phpBB intergration.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
richmix
Forum Commoner
Posts: 31
Joined: Fri Dec 22, 2006 5:21 am

Post 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!
Post Reply