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
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;
   
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.