It’s not all politics…sometimes I hack around
Over the last few months off and on I have been messing with an idea I had in PHP. The idea came to me when I was working on a rails project. In rails they have the idea of partials. Now this is nothing new (but bear with me). In rails, you can make sections of the view into a partial template and then include it in other templates. This is pretty easily done using Smarty (or regular php for that matter). The one thing I did find interesting was that by using the JS httprequest object you can update the partial and consequently the portions of the view without a page reload.
All that got me thinking. Since I use php from time to time for web work, and I also usually try to add a dash of ajax where it makes sense, having some sort of partials that could be updated in that way would be useful. So I created the smallest framework known to man :) It’s essentially two classes and a JS file. And really only one classes is filled in for you. I packaged it all up and put it in subversion. Now if I have a project I check it out and gets to hacking.
Anyway…the main class that does the magic in php is as follows:
< ?
include('Smarty.class.php');
include('../includes/Assign.class.php');
$p = new Partial();
$div_id = $_POST['div_id'];
$string = $p->updatePartial();
print "Partial.RenderPartial('$div_id', '$string');";
class Partial {
function updatePartial() {
$smarty = new Smarty();
$assign = new Assign();
$smarty->compile_dir = '../templates_c';
$template_dir = dirname(__FILE__) . '/../templates/';
$template = $template_dir . $_POST['partial'];
$assign_method = preg_replace('/.tmpl/', '', $_POST['partial']);
if (method_exists($assign, $assign_method)) {
$assign->$assign_method($smarty, $_POST);
}
$string = $smarty->fetch($template);
$string = preg_replace('/\n/', '', $string);
$string = addslashes($string);
return $string;
}
It’s pretty simple, and really doesn’t do a whole lot, but it does provide me with a quick and dirty solution. It also keeps the html from mixing with the JS. Speaking of Javascript…here is a snip of the JS I use with this:
this.RenderPartial = function(div_id, string) {
div = document.getElementById(div_id);
div.innerHTML = string;
}
Looking at all that, you take a smarty template, you turn it into a string, you pass it back to the client side and update whatever div you supply in the intial POST. The assignment stuff in the PHP is just a way to name a method similar to the template file in Assign class and assign any template variables in the partial template.
< ?
require_once('Db.class.php');
class Assign {
function page_results($smarty, $post) {
$db = new DataBase();
$count = $db->pageCount($post['from'], $post['to']);
$smarty->assign('page_count', $count);
$smarty->assign('from', $post['from']);
$smarty->assign('to', $post['to']);
}
} // End Class
?>
I have run into a few problems here and there and was able to solve them so far. I haven’t used it real heavly yet to uncover all the places where the model breaks down, but I expect to as time goes on (use it I mean :). Oh and as always, check your input if it’s coming from the client side ;)