include() and overheads

Sic

Sic

Soldato
Joined
9 Nov 2004
Posts
15,365
Location
SO16
i have a function that passes arrays of objects to a template file, and that template file has details from php arrays/objects to output to the screen (to keep html separate from php) but I'm concerned about the overheads in a real situation. is there a better way of doing this?

Code:
public static function render($arrb,$template) {
		if (is_array($arrb)) {
			foreach ($arrb as $b) {
				include($template);
			}
		}
	}

example of a template file:
Code:
<h2><a href='/blog.php?p=<?php echo $b->blog_id ?>'><?php echo $b->title ?></a></h2>
<h3><?php echo $b->posted ?></h3>
<div class='entry_body'>
	<?php echo $b->entry ?>
</div>
<p><a href='/blog.php?p=<?php echo $b->blog_id ?>#comments'><?php echo $b->nophrase ?></a></p>
<p><?php echo $b->tag ?></p>

just wondering if there's a better way of doing this, and how you'd go about doing it - I thought about fopen and fread but that won't allow me to parse the variables in the template file.

if it's ok using includes to do it, i'll carry on that way, but I just thought I'd ask.
 
It's kinda a similar thing to smarty, and you'll probably think it's fairly pointless, but for this website only, I'd like to have all my own code in there - it's like a 'look at the sort of things I can do' site. any normal circumstances I'd use smarty - was just wondering if using includes for this was the best way to go about it

actually, forget that - smarty looks incredibly easy to use compared to the inconvenience this is going to cause! thanks for that link
 
Last edited:
Personally I would avoid Smarty in most situations tbh.

i have a function that passes arrays of objects to a template file, and that template file has details from php arrays/objects to output to the screen (to keep html separate from php) but I'm concerned about the overheads in a real situation. is there a better way of doing this?
The method doesn't seem bad, it depends on how much you're using it. If you're using it a lot, do some benchmarking and see if it seriously hinders performance.

just wondering if there's a better way of doing this, and how you'd go about doing it - I thought about fopen and fread but that won't allow me to parse the variables in the template file.
To keep my HTML and PHP separate I usually load HTML files with the DOM and set template variables etc. using it and XPath. It's really nice and simple for equally simple setups, but may cause problems if you are loading large files and want to put data in very specific areas (without mass id attributes), so it's not the most flexible situation.
 
out of curiosity, why wouldn't you use smarty? just seems that if there's a decent library out for this sort of thing already, I'd rather concentrate on writing the app than a template system

Dj_Jestar said:
Templates are sooooo 1990's..
they're also pretty necessary - that comment could've been 100% more productive by offering alternatives to using a template system, as they seem to be the best way to generate/manage code that someone with no knowledge of php could understand. It also provides the V in my MVC in a convenient way
 
Last edited:
Reason's for not using Smarty:

1) Bloated.
2) Is an extra syntax to (unecessarily) learn.
3) PHP was born as a templating system.
4) Smarty syntax is convoluted compared to PHP's own.
5) Templates are non-regenerative. You need a new template for every page, even if you use "included" templates.
6) DOM generators such as PHP's own are much easier to work with.
7) Designers do not touch markup - they only, and only use CSS (+ the odd image.) They design, not program. Do your designers create templates for XML feeds? ;)

oh, and P.S. : Templates do nothing to separate the V from the M and C; especially when Smarty allows you to use direct PHP syntax which will circumvent any separation you have. You application design is what creates the separation, not the technologies used.
 
Last edited:
it's quite funny how, if you talk to different people they say such varying things on this subject. A guy I've been working with recently tore me a new one last week because my html files should ONLY contain html and php variable names, and they should be generated as many times as needed to keep the view totally physically separate from the controller.

I quite like the idea of having the controller as the page that's actually visited, and it generates and includes different view parts depending on the logic specified in the controller. I guess the bottom line is that there's no set way to do this, and I should just work out what works best for me but I've gotten myself a bit confused by it all.

I think you're right though, Mr Jestar, I can do everything I need to do in php alone, just means there'll be a little foreaching in my 'view' files (if that is what you were implying)
 
Dj_Jestar said:
Actually, I'm implying you don't write a snitch of markup at all, and you use a markup generator.

can you recommend a markup generator? as much as I'd like to write my own, the prospect of traversing the DOM all evening leaves me somewhat un-moist.

Shoseki, Joomla isn't what I'm after at all - I'm writing my own CMS
 
Dj_Jestar said:

how bad is it that that's actually fun? :(

had a quick play with it - seems I could save myself a lot of superfluous files and include confusion by generating the html inline and outputting it in the __destruct(). i.e.

Code:
<?php
class testpage {
	private $divName; // the phpdom div to put name data in
	private $divJob; // the phpdom div to put job data in
	
	private $dom; // the phpdom object
	
	private $arrPeople; // array of people data
	
	function __construct() {
		$this->dom = new DOMDocument;
		//define the name div
		$this->divName = $this->dom->createElement('div');
		$divNameID = $this->dom->createAttribute('id');
		$divNameIDText = $this->dom->createTextNode('Name');
		//define the job div
		$this->divJob = $this->dom->createElement('div');
		$divJobID = $this->dom->createAttribute('id');
		$divJobIDText = $this->dom->createTextNode('Job');
		//give name a idname
		$this->dom->appendChild($this->divName);
		$this->divName->appendChild($divNameID);
		$divNameID->appendChild($divNameIDText);
		//give job a idname
		$this->dom->appendChild($this->divJob);
		$this->divJob->appendChild($divJobID);
		$divJobID->appendChild($divJobIDText);
		
		$this->arrPeople = array(
				0 => array(
						'name' => 'Jake',
						'job' => 'Senior Developer'
					),
				1 => array(
						'name' => 'Jasper',
						'job' => 'Developer'
					)
			);
		$this->name();
		$this->job();
	}
	//insert data into the name div
	function name() {
		foreach ($this->arrPeople as $ap) {
			$name = $this->dom->createTextNode($ap['name']);
			$p = $this->dom->createElement('p');
			$this->divName->appendChild($p);
			$p->appendChild($name);
		}
	}
	//insert data into the job div
	function job() {
		foreach ($this->arrPeople as $ap) {
			$job = $this->dom->createTextNode($ap['job']);
			$p = $this->dom->createElement('p');
			$this->divJob->appendChild($p);
			$p->appendChild($job);
		}
	}
	//output html
	function __destruct() {
		echo $this->dom->saveHTML();
	}
}

$pg = new testpage
?>

that the sort of thing you were talking about? That'd be my controller and view combined, I guess. meh, silly class structures confuse me when I look at them for too long
 
hehe, I really like it, which is odd because I find the javascript DOM infuriating.

I'll try writing an entire set of classes outputting data this way, and see if I'm still as enthusiastic about it!

the way I've done things in that class there, does that make sense or have I grabbed the wrong end of the stick and started to swing wildly?
 
I'd have the constructor setting up members etc. then have reusable methods for creating elements, assigning attributes etc. - make it a bit more flexible. At the moment it's pretty specific to what it's handling; have generic methods which take arguments, e.g.
Code:
	function job() {
		foreach ($this->arrPeople as $ap) {
			$job = $this->dom->createTextNode($ap['job']);
			$p = $this->dom->createElement('p');
			$this->divJob->appendChild($p);
			$p->appendChild($job);
		}
	}


becomes
Code:
	function createEl($element, $textValue) {
			$text = $this->dom->createTextNode($textValue);
			$el = $this->dom->createElement($element);
			$this->wrap->appendChild($el); // Some sort of wrapper
			$el->appendChild($text);
		}
	}
It's just an example: but this is more flexible as not only can you loop $obj->arrPeople in a foreach and call the method, but you can use it in a variety of manners. The way you're currently doing is kind of an anti-OOP ethos as it's case-specific. Make things more modular. It may mean more code outside of the class, but it means you can reuse it in other scenarios.
 
Last edited:
It seems to me that's an awful lot of (overcomplicated, relatively speaking) code there Sic, for very little output?

I assume I'm very wrong, but I'm a fan of K.I.S.S...
 
KingAdora, that's because at this stage it's on a small scale for the sake of examples. I'm also a fan of KISS, but i'm also a fan of MIEFMITLR (making it easier for myself in the long run)

for example, the dom class following contains a function that can generate any html tag from the php, negating the need for any html (hopefully!). only a stylesheet will be needed, and the link to that can also be created using this method. I've put a condition in the constructor which will allow me to use this class for pseudo-html includes (in theory, I've not tested this yet)

Code:
<?php
class testpage {
	private $divName; // the phpdom div to put name data in
	private $divJob; // the phpdom div to put job data in
	private $ulJob;
	
	private $dom; // the phpdom object
	
	private $arrPeople; // array of people data
	
	function __construct() {
		$this->dom = new dom(true);
		
		$this->divJob = $this->dom->newElement('div',null,array('id' => 'myJob'));
		$this->ulJob = $this->dom->newElement('ul',null,array('class' => 'jobs'));
		
		for ($i = 0; $i < 10; $i++) {
			$this->ulJob->appendChild($this->dom->newElement('li','this time: '.$i));
		}
		$this->dom->body->appendChild($this->divJob);
		$this->divJob->appendChild($this->ulJob);
		
		$this->dom->body->appendChild($this->dom->newElement('a','google',array('href' => 'http://www.google.co.uk','target' => '_blank')));
		
		$this->dom->head->appendChild($this->dom->newElement('title','page title'));	
		$this->dom->body->appendChild($this->dom->formElement('text','name','hellooooo!!!'));
		$this->dom->body->appendChild($this->dom->formElement('submit','login','login, yo!'));
	}

}

class dom {
	public $dom;
	private $html;
	public $head;
	public $body;
	
	function __construct($fullPage = false) {
		$this->dom = new DOMDocument;
		if (true === $fullPage) {
			$this->html = $this->dom->appendChild($this->newElement('html'));
			$this->head = $this->html->appendChild($this->newElement('head'));
			$this->body = $this->html->appendChild($this->newElement('body'));
		}
	}
	
	function newElement($element,$textValue = null,$optional = null) {
		$el = $this->dom->createElement($element);
		if (!is_null($textValue)) {
			$text = $this->dom->createTextNode($textValue);
			$el->appendChild($text);
		}
				
		if (!is_null($optional) && is_array($optional)) { //accepting array in format attribute => value
			foreach ($optional as $key => $op) {
				$o = $this->dom->createAttribute($key);
				$ov = $this->dom->createTextNode($op);
				$o->appendChild($ov);
				$el->appendChild($o);
			}
		}
		
		return $el;
	}
	function formElement($inputType, $name, $value) {
		return $this->newElement('input',null,array('type' => $inputType, 'name' => $name, 'value' => $value));
	}
	
	function textArea() {
	
	}
	 
	function __destruct() {
		echo $this->dom->saveHTML();
	}
}

$pg = new testpage
?>

thanks to Dj_Jestar and psyr33n for the help
 
Last edited:
Back
Top Bottom