Jump to content

Recommended Posts

Ciao a tutti,

I'm new and I've got a question:

I made ​​small template engine in PHP and works very well with a HTML file.

But when I wanna include a PHP file, in the part I wanted, it shows "1" and the file is included at the top of the page.

 

Here's a part of the code (maybe the affected):

 

foreach($this->values as $key => $value) {
	$Tags_To_Replace = "[$key]";
	$this->output = str_replace($Tags_To_Replace, $value, $this->output);
}
return $this->output;

 

"values" is an array

$this->output = file_get_contents($this->file);

 

the code for include the file

set("Left", require_once('pages/left.php'));

 

Can someone help me? :D

If you need more information, ask

 

Thanks Thanks  so much!!

P.S = Sorry for my bad english, i'm italian :D

post-135680-13482403679498_thumb.png

Firstly, you have't shown us enough code.

 

Secondly, there are a few easily noticeable errors. require_once does not return a string, so unless you are using return to return a value within your left.php file, that is why you are receiving 1.

 

The other part that is confusing. You say "values is an array", you then show us some seemingly unrelated code.

First of all thank you :D

 

Firstly, you have't shown us enough code.

 

function set:

 

function set($Key, $Value){
		$this->Values[$Key] = $Value;
	}

 

function: __construct:

 

 

function __construct($file) {
		$this->File = $File;
		$this->output = file_get_contents($this->File);
	}

 

function output:

 

function output() {
		foreach($this->Values as $Key => $Value) {
			$Tags_To_Replace = "[$Key]";
			$this->output = str_replace($Tags_To_Replace, $Value, $this->output);
		}
		return $this->output;
	}

 

declaration of the variables:

 

public $output;
	public $File;
	public $Values = array();

 

The other part that is confusing. You say "values is an array", you then show us some seemingly unrelated code.

 

Sorry I was confused :)

 

Thanks again for the help.

Why don't you post the entire class? Doing so will ensure that the people trying to help you can be certain that they know the entire scope, and as such not only be more inclined towards helping you but also find it easier to give you accurate and speedy assistance.

Providing some example data would also be extremely appreciated, as it allows us to observe ourselves what the problem is firsthand. The more we know about a problem, the easier it is to spot the solution, after all.

I posted the whole class, I've broken it to better understand.

 

	class Template{
	public $output;
	public $file;
	public $values = array();

	function __construct($file) {
		$this->file = $file;
		$this->output = file_get_contents($this->file);
	}

	function set($key, $value){
		$this->values[$key] = $value;
	}

	function output() {
		foreach($this->values as $key => $value) {
			$Tags_To_Replace = "[$key]";
			$this->output = str_replace($Tags_To_Replace, $value, $this->output);
		}
		return $this->output;
	}
}

Here's the index.php, which uses the template engine

 

error_reporting(E_ALL);
ini_set("display_errors", 1); 

//Includo il template engine
require_once("template.php");

//Includo i files di configurazione
       //Configurations file
require_once("include/config.php");
require_once("include/connect.php");
require_once("include/funzioni.php");

$Template =  new Template('default.tpl');
$Template -> set("Titolo_Pagina", "Benvenuti in Ginho! - Il sito che parla di tutto ci? che ruota attorno all'informatica");
$Template -> set("Descrizione", "Il sito che parla di tutto ci? che ruota attorno all'informatica");
$Template -> set("Left", require_once('pages/left.php'));

echo $Template->output();

 

it isn't show any error, and it isn't include correctly.(look the attachement in the first post).

 

config.php    contains the costant for the DB connection,

connect.php connect to the DB,

funzioni.php contains general function (in this case doesn't matter with the current problem)

 

I haven't got any idea to solve it. i've tried the impossible, but.. nothing.

It's a vital issue xD

 

Thanks again :)

 

Secondly, there are a few easily noticeable errors. require_once does not return a string, so unless you are using return to return a value within your left.php file, that is why you are receiving 1.

 

I use return only to output the render template with this:

 

return $this->output;

 

PS: Breaking up a class like that doesn't make it easier to understand, but harder as it breaks the logical flow of the code.

 

Sorry, I will adjust it for next time :D

The problem is on this line:

$Template -> set("Left", require_once('pages/left.php'));

 

The cause of the problem is quite well explained in the PHP manual for include (). In fact, it's stated in the very first sentence.

What you need to do is to rethink your logic, because what you're trying to do here won't work.

The second parameter of the ->set() method expects a string, i.e. like the first two uses set("Titolo_Pagina", ...

 

If your 'pages/left.php' file contains the string your want to supply as the second parameter, use file_get_contents instead of require_once.

If your 'pages/left.php' file contains the string your want to supply as the second parameter, use file_get_contents instead of require_once.

 

Yeah, it works. I've tried this but the php code it isn't been elaborate.

 

Why use a foreach instead of using preg_replace?

 

? foreach is a cycle, preg_replace is something completely different...  explain better :D

 

Thanks guys

With preg_replace () you can use a Regular Expression to capture and replace all of the template variables in one call, skipping the loop entirely. Something which you can do with "str_replace ()" as well, coincidentally, seeing as it supports arrays as the first two arguments. Latter one just requires a bit more work, to get the square brackets added to the keys.

 

In any case, example of a preg_replace () to insert the template data:

return preg_replace ('/\[([\w\d_-]+)\]/e', '$this->values[$1]', $this->output);

 

Whether this or the existing method is better, is something you'll have to decide.

With preg_replace () you can use a Regular Expression to capture and replace all of the template variables in one call, skipping the loop entirely. Something which you can do with "str_replace ()" as well, coincidentally, seeing as it supports arrays as the first two arguments. Latter one just requires a bit more work, to get the square brackets added to the keys.

 

In any case, example of a preg_replace () to insert the template data:

return preg_replace ('/\[([\w\d_-]+)\]/e', '$this->values[$1]', $this->output);

 

Whether this or the existing method is better, is something you'll have to decide.

 

IIRC, PCRE regex allows you to replace values of any variable in your template. Let's say you use {{var_name}} to signify variables, you can be almost certain that it won't come up randomly in a sentence. You can replace anything that has not been defined with [var_name not defined], or whatnot.

You're welcome, but as for optimized... Well, that you won't know until you've benchmarked the two methods against each other. :P

 

When it comes to PHP files you have to keep in mind that when you include them, you execute the code inside of them at the same time. What you need to do, is to separate the PHP and HTML code in your mind (and code). All PHP is parsed on the server, before any output is sent to the browser. Prior to that time the output is simply PHP strings, the fact that the browser interprets it as HTML (JS, CSS, or whatever) is purely coincidental and not important at this time.

That means you have to reorganize your code, so that you can either store the output in a variable until it's needed, or use the included code to set the name of the HTML template file to be included.

You're welcome, but as for optimized... Well, that you won't know until you've benchmarked the two methods against each other. :P

 

xD c'mon rough guess it's better xD

Thanks again...

 

i've got an idea!

is it right create a tag

, where the php code will process?

can i do this with a simple if?

 

Cheers

 

P.S = are correct the periods i wrote? xD

Your templates should be logic-less. There's no reason to have to include PHP within a template. Take a look {{mustache}} as an example; simple tags allow for echoing variables, including partials and looping through data sets. You shouldn't need to do anything more in them.

OK. perfect.

 

one question: how do you manage the pages? Currently I have two files, called header.php and footer.php, which I include on the interested page.

 

And you?

 

What's the better method to manage pages in a CMS?

 

Thanks

Well blocks of content are traditionally entered using a WYSIWYG editor. The header and footer though would need to be more controlled, given that anyway who doesn't know what they're doing could completely destroy the website. You don't really need to worry about proving a way to edit the templates, no one that isn't a dev should be able to go in and change the HTML, and the dev can do that in the file as usual.

You don't really need to worry about proving a way to edit the templates, no one that isn't a dev should be able to go in and change the HTML

 

I would use my cms to another site and create a new template with ease, thanks to the separation of design and code. that's all...

 

 

for example, i tried to see the joomla code but i'm getting crazy, and i don't know how can do this.

 

Thanks.

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.