Jump to content

Recommended Posts

im trying to make a simple template engine but am having a problem replacing the string in the content....if anyone could give me any help or pointers please

 

<?php
class Template
{	
public function display($filename)
{
	echo "hello";
}

public function assign($variable, $value)
{
	if (!is_array($value))
	{
		ob_start();
		$contents = ob_get_contents();
		$contents = str_replace($variable, $value, $contents);
		ob_end_clean();

		return $contents;
	}
}
}

$template = new Template();
$template->display();
$template->assign("hello", "test");
?>

 

code above will only display hello and not test as it should

Link to comment
https://forums.phpfreaks.com/topic/218199-oop-simple-template-class/
Share on other sites

You're just echoing out a static value:

 

public function display($filename)
{
    echo "hello";
}

 

Plus it doesn't make logical sense to try and display() the template, before you've assign()ed your vars..?

 

$template->display();
$template->assign("hello", "test");

There's no code in your code to do anything other than echo 'hello'.

 

In order for you to write a class to implement even a simple template, you must first define what you want each of your class methods to do and what class variables you will need. You have just thrown some random code into a class structure.

It doesn't make sense to output code before you make all your changes to it, because now you're wasting cycles and time doing string manipulations. A much easier method would be to have an associative array of things that you'd like to change, and their values. assign() will simply set the value of the array key and display() will simply use each value from the array.

As someone already suggested, you need to define everything before you can write any code to do it.

 

For example, where is your template stored? You class constructor should accept, as a parameter, the name of the template to use and then read it from wherever it is stored and place it into a class variable. Once you have the raw template available in an instance of your class, you can then think about replace values in it. Once you have replaced all the values in it, you can think about displaying it.

you set strings in the template that would serve as markers to be replaced by strings that are dynamic..

 

Static Template:

$template = 'My name is {username}, i like to whatever...';

 

Replace items in Static Template..

str_replace('{username}', $filename, $template);

 

revision.. something along the lines of..

	

public function display($filename)
{
       $template = 'My name is {username}, i like to whatever...';
        echo str_replace('{username}', $filename, $template);
}

 

super basic concept of how the idea is to work, but hopefully it gives you the idea.. now knowing my luck as I didnt test that im sure theres a bug in it, but the idea is the same regardless.

If you're going to go down the road of parsing templates like that, then you're also going to need to think about how you'll interpret control structures; like if conditions, loops, includes, etc. If you plan on using normal PHP for those, then what's the point in the overhead of parsing templates just to replace variables? You'd may as well just use PHP-based templates in that case.

I commented out your first display call, that is what was showing the first echo, then, since I didn't know where you were getting for ob_get_content(), just threw in a string to be returned -

 

try this:

 

<?php

class Template

{

/*************************************************

*  echos out the passed in data

* @param string $valueToDisplay what should be the screen output

* @return void

*************************************************/

public function display($valueToDisplay)

{

echo  $valueToDisplay;

}

/*************************************************

*  replaces the variable with the value

*

* @param string $variable - what should be replaced

* @param string $value - value to insert

* @return string containing value rather than variable

*************************************************/

public function assign($variable, $value)

{

if (!is_array($value))

{

ob_start();

$contents = $this->get_page();

$contents = str_replace($variable, $value, $contents);

ob_end_clean();

return $contents;

}

}

/*************************************************

*  gets web page, with 'placeHolder' set

*

* @return string of html page

**************************************************/

protected function get_page()

{

return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><title> placeHolder </title></head><body>hi, this is your web page, <br /><h2>placeHolder</h2></body></html>';

}

}

 

$placeHolder = "placeHolder";

$replacePlaceHolderWithThis = "replaced place holder with this string ";

$template = new Template();

//$template->display($placeHolder);

$page = $template->assign($placeHolder, $replacePlaceHolderWithThis);

$template->display($page);

?>

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.