Page 1 of 1

pass variable to a page {variable]

Posted: Fri Sep 03, 2004 1:39 am
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

Posted: Fri Sep 03, 2004 2:08 am
by feyd

Posted: Fri Sep 03, 2004 3:25 am
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]