Jump to content

Template system with layout (Views)


Stevenn

Recommended Posts

I've made a template system, but the way I've chosed to do it doesn't give me the ability to choose a design. I've posted the code below with a bad solution for my design problem. Does anyone have any suggestions how to make a very simple and smart templatesystem? I don't have any problems coding. I just need ideas :-)

 

<?php
final class View implements IView {
protected $filename, $variables = array();

public function __construct($filename = null, $variables = array()) {
	$this->filename = $filename;
	$this->variables = $variables;
}

public function __toString() {
	if(!file_exists($this->filename)) {
		throw new Exception("View does not exists.");
	}

	$content = file_get_contents($this->filename);

	foreach($this->variables as $variable => $value) {
		$content = str_replace("#{{$variable}}", $value, $content);
	}

	return str_replace("#{content}", $content, file_get_contents("design.html"));
}
}
?>

Link to comment
Share on other sites

Your code makes little sense. $this->filename should be set to your template.

 

Anyway, why re-invent the wheel? If you must use a template engine of some sort (which I would recommend against) there are plenty of solutions already out there.

Link to comment
Share on other sites

Your code makes little sense. $this->filename should be set to your template.

 

Anyway, why re-invent the wheel? If you must use a template engine of some sort (which I would recommend against) there are plenty of solutions already out there.

 

Hi thorpe,

 

Is a template system a bad idea? I've looked at Smarty, but I don't like to use code I haven't coded myself. Smarty is too big.

Link to comment
Share on other sites

A template parser (like Smarty, Twig) is generally frowned upon. They are usually very large libraries (Smarty), and cost a lot of overhead to use.

 

Besides that, they are completely unnecessary. Probably one of the biggest arguments I see in favor of template engines is something along the lines of: "well I want my designers who don't know PHP to be able to make templates". This is a load of crap. Smarty's syntax is nearly identical to PHP, if they can do that they can definitely use PHP. You shouldn't have logic in templates anyway, just conditionals, loops, and variables.

 

Another big reason I see for using stuff like Smarty is the separation of the presentation. This is a good argument, but has nothing to do with template parsers. The same thing can be achieved using pure PHP in the templates.

 

but I don't like to use code I haven't coded myself.

 

This is going to cripple you as a developer. Don't be so stubborn, libraries exist for a good reason.

Link to comment
Share on other sites

I personally have developed my own contemplating system that slots in perfectly for my page execution concept. I don't personally use procedural style programming and as such find it annoying writing code into the pages hence the want for a template system.

 

If you do want to make a template system, keep it simple. There is no point creating a massive, elaborate, do it all template system as it'll consume unnecessary resources. Whilst the above posters wouldn't recommend a template system, I would argue it is required in certain programming concepts.

 

And take scootstah's advice, don't be so stubborn. I was like that two years ago and learn the hard way.

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.