Jump to content

[SOLVED] Template Variables Not Working


ShoeLace1291

Recommended Posts

I've been having this problem with my PHP template variables script, not only with this project, but for other projects as well.  I have HTML in TPL files with variables from an array such as SITE_NAME.  The problem is that only the last item in the array works.  For example, say my array is the following:  array('SITE_NAME' => "MySite.com",SITE_MOTTO => "We are freakin awesome!");  Only the site motto will work.  Here is my current script:

 

<?php

if($_POST['submit']){

$book = array(
				'FIRST_NAME' => $_POST['authfirst'],
				'LAST_NAME' => $_POST['authlast'],
				'MI' => $_POST['authmi'],
			  	'TITLE' => $_POST['booktitle'],
				'PUBLISHER' => $_POST['publisher'],
			  	'PULICATION_LOCATION' => $_POST['publocation'],
			  	'VOLUME' => $_POST['volume'],
			  	'DATE' => $_POST['date']
			  	);

$file = file_get_contents("templates/displays/MLA_book.tpl");
foreach($book as $key => $value){
	$display = str_replace($key, $value, $file);
}

echo $display;

} else {

$vars = array(
			  'ACTION' => $_SERVER['PHP_SELF']
			  );
$file = file_get_contents("templates/forms/MLA_book.tpl");
foreach($vars as $key => $value){
	$display = str_replace($key, $value, $file);
}

echo $display;

}

?>

 

This is "templates/display/MLA_book.tpl":

<div style='width: 300px;'>
LAST_NAME. <u>TITLE</u>. PUBLICATION_LOCATION: PUBLISHER, DATE
</div>

 

The only thing that works is DATE, and everything else displays as is.

 

Thanks for any help.

Link to comment
https://forums.phpfreaks.com/topic/155407-solved-template-variables-not-working/
Share on other sites

The problem is in your str_replace function.  You are always replacing the contents of "file" and storing it in "display."  Each time you make a replacement, you store the result in display, and move on to the next one.  Sounds good, right?  Except, on the next replacement, you open the original file contents again and store the result in display again.  Consider this code:

 

<?php

if($_POST['submit']){

   $book = array(
               'FIRST_NAME' => $_POST['authfirst'],
               'LAST_NAME' => $_POST['authlast'],
               'MI' => $_POST['authmi'],
                 'TITLE' => $_POST['booktitle'],
               'PUBLISHER' => $_POST['publisher'],
                 'PULICATION_LOCATION' => $_POST['publocation'],
                 'VOLUME' => $_POST['volume'],
                 'DATE' => $_POST['date']
                 );
               
   $file = file_get_contents("templates/displays/MLA_book.tpl");
   foreach($book as $key => $value){
      $display = str_replace($key, $value, $file);
   }
   
   echo $display;
   
} else {

   $vars = array(
              'ACTION' => $_SERVER['PHP_SELF']
              );
   $file = file_get_contents("templates/forms/MLA_book.tpl");
   foreach($vars as $key => $value){
      $file = str_replace($key, $value, $file);
   }
   
   echo $file;
   
}

?>

Ah.  I didn't notice you had to references to the same code, and I only changed one.  Basically, you should change:

 

$display = str_replace($key, $value, $file);

 

to:

 

$file = str_replace($key, $value, $file);

 

The reason is because you want to process a template variable, save the replaced result in the variable "file", then use that replaced result while you search for the next template key, and so on.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.