Hack to Extend the Website content

In this article you’ll find some useful web-mastering tips and tricks. You can use this technique to make own web-site template engine or to extend the website functionality without ruining the old code as it was discussed in the previous article.

Define the new content as a variable. The previous and the next decorators could be dynamic arrays:

$sidebar_before[] = '<h2>Sidebar</h2>';
$sidebar_before[] = 'print_adsense';
$sidebar = <<<END
<p>
Some aside content here...
</p>
END;
$sidebar_after[] = 'block-links.inc';
$sidebar_after[] = '<h3>Advertisement<h3>';
$sidebar_after[] = 'print_adsense';

To call custom functions, include files or just print some html code before and after the main content, the PHP code in your website template should be something like this:

if (isset($sidebar_visible) && $sidebar_visible) {

	echo "\t\t\t<!-- Sidebar -->\n";
	echo "\t\t\t<div id=\"sidebar\" class="$css_class">\n\n";
	
	// Print content before the Sidebar
	if (isset($sidebar_before))
		foreach ($sidebar_before as $s) {
			if (function_exists($s)) call_user_func($s); 
			elseif (is_file($s)) include($s);
			else echo $s;
			echo "\n\n";
		}	

	// Print the Sidebar
	if (isset($sidebar)) echo $sidebar."\n\n";
	
	// Print content after the Sidebar
	if (isset($sidebar_after))
		foreach ($sidebar_after as $s) {
			if (function_exists($s)) call_user_func($s); 
			elseif (is_file($s)) include($s);
			else echo $s;
			echo "\n\n";
		}	
	
	echo "\t\t\t</div>\n";
	echo "\t\t\t<!-- /Sidebar -->\n\n";
} 

If you have any questions relative this article or know some other useful web-master hacks write a comment or contact our team directly.

Power of Backward Loops

Loops aka cycles (while, for, do until, etc) are common in programming. Actually they are a language level implementation of the iterator pattern.

Those who learned programming in the college/university, remember lots of tasks for sorting an array or traverse a double-linked list in both directions. But in practice, very few people apply the backward loops.

There are a number of tasks where the reverse iteration automatically solves a bunch of problems, for example:

If you need to release all elements of array/collection/list, it is much easier to start from the end and delete the last element each time, rather then go from the beginning, save the length of the container, make the reference for every deleting object, and so on.

Compare this:

var len = someCollection.length();
for (i = 0, i < len, i++) {
    var element = someCollection[i];
    someCollection.delete(i);
    delete element;
}

with that:

var i = someCollection.length();
while (i--) {
    delete someCollection[i];
}

So the backward loop is not only for students, the backward traverse rules in many cases 🙂