pass variable to a page {variable]

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

Post Reply
bradles
Forum Commoner
Posts: 89
Joined: Wed Jun 30, 2004 10:40 pm

pass variable to a page {variable]

Post by bradles »

I have seen a couple of sample scripts for an image gallery that have passed variables to a html template page, something like:
<title>{txt_script_name} {txt_script_version}</title>
I don't know a lot about template engines but this one doesn't appear to have one...i wouldn't know.

My question is: Can I pass variables from a php script to a template (html) file so the page can use them? If so, is there any explanations/tutorials floating around as I cannot seem to find anything in the php manual. I'd like to set up my php so that it passes variables to a template page.

Brad
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


To grossly oversimplify, there are several ways this is done. With a template like you showed:

Code: Select all

$template = '<title>{txt_script_name} {txt_script_version}</title>';
$txt_script_name = 'Name';
$txt_script_version = '1.0';
$output = str_replace('{txt_script_name}', $txt_script_name, $template);
$output .= str_replace('{txt_script_version}', $txt_script_version, $template);
echo $output;
Or:
template.php

Code: Select all

<title><?php echo "$txt_script_name $txt_script_version"; ?></title>

$txt_script_name = 'Name';
$txt_script_version = '1.0';
include 'template.php';
Or:

Code: Select all

$template = '<title>$txt_script_name $txt_script_version</title>';
$txt_script_name = 'Name';
$txt_script_version = '1.0';
echo eval($template);
Plus others. See http://wact.sourceforge.net/index.php/TemplateView for a discussion of the different types.


feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply