Using str_replace() content ends up in improper place

Replacing a string in a template using str_replace() results in improp

Page 1 of 1

8 Replies - 1408 Views - Last Post: 09 September 2008 - 05:39 PM Rate Topic: -----

#1 coffejor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-September 08

Using str_replace() content ends up in improper place

Post icon  Posted 09 September 2008 - 10:39 AM

Hello all,

I'm creating a template engine to replace placeholder strings in my html template with dynamic content served by a function. To do this I have written the following template processor:

<?php
function load_template()
{
	$template_file = "template.html";
	$template = file_get_contents($template_file);
	$template_vars = array('{MAIN}', '{NAV}', '{TITLE}');
	$template_vals = array(bodyContent(), navContent(), $title);

	function template_eval (&$template, &$template_vars, &$template_vals)
	{
		return str_replace($template_vars, $template_vals, $template);
	}

	echo template_eval($template, $template_vars, $template_vals);
}

load_template();
?> 


which is called by a page like so:

<?php
$title = "Example Title";

function navContent()
{
?>
					<h1>Page Links:</h1>
					<ul>
						<li><a href="">Link 1</a></li>
						<li><a href="">Link 2</a></li>
						<li><a href="">Link 3</a></li>
					</ul>
<?php
}

function bodyContent()
{
	?>

					<h1>Example Title 1</h1>
					<p>Example Content 1</p>
					<h1>Example Title 2</h1>
					<p>Example Content 2</p>
					<h1>Example Title 3</h1>
					<p>Example Content 3</p>
<?php
}

	require_once (URL."/test_templateProcessor.php");
?>


Unfortunately, the content fails to be placed into the appropriate place in the template but, rather, ends on top of the template as can be seen here.

I'm not quite sure why it does this, so any insight would be greatly appreciated. Thanks in advance for any and all help!

Jordan

Is This A Good Question/Topic? 0
  • +

Replies To: Using str_replace() content ends up in improper place

#2 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: Using str_replace() content ends up in improper place

Posted 09 September 2008 - 10:44 AM

i think because your closing the function with ?> it stops making the the function a functipon and just makes it html text

tray taking out the ?> and instead doing somethig like

<?php
function navcontent(){
print(" <h1>Page Links:</h1>
                     <ul>
                        <li><a href="">Link 1</a></li>
                        <li><a href="">Link 2</a></li>
                        <li><a href="">Link 3</a></li>
                    </ul>
");
}
?>



if not then im not sure
Was This Post Helpful? 0
  • +
  • -

#3 coffejor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-September 08

Re: Using str_replace() content ends up in improper place

Posted 09 September 2008 - 10:57 AM

Thanks for the suggestion. I implemented the suggested change but the result was the same.

Jordan
Was This Post Helpful? 0
  • +
  • -

#4 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 43
  • View blog
  • Posts: 903
  • Joined: 08-August 08

Re: Using str_replace() content ends up in improper place

Posted 09 September 2008 - 11:02 AM

sorry also forgot you would need to ad \ backslashes infront of the double quotes
Was This Post Helpful? 0
  • +
  • -

#5 coffejor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-September 08

Re: Using str_replace() content ends up in improper place

Posted 09 September 2008 - 11:18 AM

yeah, caught that but wasn't going to correct you on it :)
Was This Post Helpful? 0
  • +
  • -

#6 jausions  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-September 08

Re: Using str_replace() content ends up in improper place

Posted 09 September 2008 - 11:24 AM

Use the heredoc syntax to return the HTML from the functions. Right now, you're outputting the HTML to the web browser, instead of returning its string.

http://www.php.net/m....syntax.heredoc
Was This Post Helpful? 0
  • +
  • -

#7 coffejor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-September 08

Re: Using str_replace() content ends up in improper place

Posted 09 September 2008 - 11:33 AM

Thanks for the response. I was trying to avoid using heredocs because I wanted to be able to inject php into the template. It is my understanding that Heredocs only support strings but nothing like function calls, but I will look into heredocs again.

Thanks again.

Jordan
Was This Post Helpful? 0
  • +
  • -

#8 coffejor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-September 08

Re: Using str_replace() content ends up in improper place

Posted 09 September 2008 - 12:10 PM

Hi All,

I've adapted my code to use Heredocs as so:

<?php
$title = "Title";

$nav = <<<NAV
					<h1>Page Links:</h1>
					<ul>
						<li><a href="">Home</a></li>
						<li><a href="">Becoming a Member</a></li>
						<li><a href="">Downloads</a></li>
						<li><a href="">Club Merchandise</a></li>
						<li><a href="">Highlited Questions</a></li>
						<li><a href="">Type Certificates</a></li>
						<li><a href="">Links</a></li>
						<li class="last"><a href="">Members Only</a></li>
					</ul>
NAV;

$titleone = getTitle (about, one);
$contentone = getContent (about, one);
$titletwo = getTitle (about, two);
$contenttwo = getContent (about, two);

$main = <<<MAIN
					<h1>$titleone</h1>
					<p>$contentone</p>
					<hr />
					<h1>$titletwo</h1>
					<p>$contenttwo</p>
MAIN;

require_once (URL."/templateProcessor.php");
?>


with the templateProcessor like so:

function load_template()
{
	$template_file = "template.html";
	$template = file_get_contents($template_file);
	$template_vars = array('{MAIN}', '{NAV}', '{TITLE}');
	$template_vals = array($main, $nav, $title);

	function template_eval (&$template, &$template_vars, &$template_vals)
	{
		return str_replace($template_vars, $template_vals, $template);
	}

	echo template_eval($template, $template_vars, $template_vals);
}

load_template();


but have the same result with the content being placed outside the template.

Thanks for all of your patience and your help so far.

Jordan
Was This Post Helpful? 0
  • +
  • -

#9 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 463
  • View blog
  • Posts: 820
  • Joined: 17-June 08

Re: Using str_replace() content ends up in improper place

Posted 09 September 2008 - 05:39 PM

View Postcoffejor, on 9 Sep, 2008 - 02:10 PM, said:

but have the same result with the content being placed outside the template.

You've set $main, $nav, and $title in the global scope, but you reference them in load_template() without a global declaration, which means they're out of scope. In other words, load_template() sees them as uninitialized variables. You need to either declare them as global or, better yet, pass them into load_template() as parameters.

As for why your content is still showing up outside the template, that doesn't make sense to me. From what you've posted, your content shouldn't show up at all. Presumably that particular issue is in some of the code you're not showing us.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1