Archive for August, 2009

Last words of an avatar

_why’s last tweets

T-3: “programming is rather thankless. you see your works become replaced by superior works in a year. unable to run at all in a few more.”

T-2: “if you program and want any longevity to your work, make a game. all else recycles, but people rewrite architectures to keep games alive.”

T-1: “an ascending homage to fish bones. culminating in a delicate canopy of mouse furs.”

And then he’s gone.

I can’t tell if T-1 is a subtle reference, a mad lib, or just an obfuscated metaphor typical of the _why persona.

That guy was a riddle.

Lolthorization

Found this under “Security Patterns” on Technoweenie’s restful-authentication wiki

Snazzy Diagram

… but really, there’s some good commentary here.
http://wiki.github.com/technoweenie/restful-authentication/security-patterns

Using Technoweenie’s restful-authentication as an impetus for a do-over of the recess simpleauth plugin.

LoL, acts_as_asshat …

Template troubles resolved.

So I found exactly what I was looking for. It’s called json-template, and it is soooooooooo perfect.

You create a template …

<li id="edge_{id}">
  <a href="/{source}" class="source">{source}</a> is a 
  <a href="/{target}" class="target">{target}</a> 
  <a href="#edge_delete-{id}" class="edge_delete">&nbsp;</a>
</li>

… You slap it together with a PHP (or Java or Python) object on the server …

<?php
$t_file = file_get_contents('node_li.jsont.php');
$t = new JsonTemplate($t_file);
$d = json_encode($model);
 
echo $t->expand($d);
?>

… You slap it together with a JSON object in the browser …

$.post('/edges.json', form.serialize(), function(data, textstatus){
  if(textstatus=="success"){
    $.jsont('node_li', data.edge).insertAfter("ul.edges li:last");
  }
}, "json");

… and you get the same HTML.

<li id="edge_80">
  <a href="/test" class="source">test</a> is a 
  <a href="/asdf31113" class="target">asdf31113</a> 
  <a href="#edge_delete-80" class="edge_delete">&nbsp;</a>
</li>

I just finished wrapping up the Javascript class as a jQuery plugin ($.jsont), and next I’m going to wrap the PHP class in a view helper and she’ll be set to jet.

Simple.

Unimpressed with CodeIgniter

Maybe I’m looking at the wrong side of the coin here, but I’m having difficulty finding good resources for learning CodeIgniter. I’ve been searching for several hours and I’ve yet to find a decent article or tutorial for CodeIgniter more recent than early 2007. Most of the authors don’t know what they’re talking about. And so far, scaffolding appears to be non-existent. In fact, I’m almost certain that the framework comes with no code generators at all.

Coming from using Propel, I’ve become accustomed to expressing my DB Schema in an XML/YAML file and having automatically generated abstract models. But it does not appear that CodeIgniter offers anything close to that. I’m thinking I might wind up installing Doctrine underneath CodeIgniter and hoping they don’t collide.

CI uses some weird conventions like default routing …

/$controller_classname/$action/$arg

… without any sort of inflection which reinforces poor Controller class naming conventions …

class User extends Controller {

… and unRESTful url structures …

<form action="/user/update/23">

This might all be acceptable if there were some upside to the framework, but I’m not seeing it. Supposedly there’s some good validation libraries inside, but I’m not sure I even want to start getting involved with that sort of LOC intensive stuff if the foundation is set in sand.

I suppose the best thing I’ve seen about CodeIgniter so far is that it seems configurable enough that I can tell it to GTFO of my way as I proceed to manhandle it into submission.

If I do start working professionally with CodeIgniter, I can say that within a week I’ll have it behaving like a very different beast.

Template troubles

So recently I’ve come across a lingering dilemma in my web development practice. I want to be able to render JSON objects fetched from my server as HTML for dynamic insertion on the client side, but I don’t want to have to totally rewrite my native PHP templates in JS. So I wind up writing shit like this…

$(this).find('input.submit').click(function(){
  $.post('/edges.json', form.serialize(), function(data, textstatus){
    if(textstatus=="success"){
      // <lotsa-crap>
      var edge = data.edge;
      var source_link = $("<a/>").attr('href', "/"+edge.source).html(edge.source);
      var target_link = $("<a/>").attr('href', "/"+edge.target).html(edge.target);
      var delete_link = $("<a/>").addClass('edge_delete')
        .attr('href', "#edge_delete-"+edge.id).html('&nbsp;');
      $("<li/>").attr('id', "edge_"+edge.id)
        .append(source_link).append(target_link)
        .append(' ').append(delete_link)
        .insertAfter("ul.edges li:last");
      // </lotsa-crap>
      div.find('input[name=edge[target]]').val('').focus();
    }
  }, "json");
  return false;
});

What a pain in the ass. I don’t want to have to rebuild every template in the DOM. I’d rather just …

$(this).find('input.submit').click(function(){
  $.post('/edges.json', form.serialize(), function(data, textstatus){
    if(textstatus=="success"){
      // <less-crap>
      $.htmlFromTemplate('edge_li', data.edge).insertAfter("ul.edges li:last");
      // </less-crap>
      div.find('input[name=edge[target]]').val('').focus();
    }
  }, "json");
  return false;
});

But my templates are written in PHP and fractured into partials …

<ul class="edges">
<?php foreach($edgeSet as $edge) { ?>
	<li id="edge_<?=$edge->id?>">
    <?php Partial::draw('details', $edge) ?>
    <a href="#edge_delete-<?=$edge->id?>" class="edge_delete">&nbsp;</a>
  </li>
<?php } ?>
</ul>

So now I’m getting fed up and considering ditching PHP template rendering altogether. Just serving HTML templates and letting Javascript hydrate them with JSON from my application’s web service in the browser. Any request to the site with header Accept: text/html will be responded to with a static bootstrap HTML file. From that point on, the application flow is in the hands of Javascript. Feels a lot like a Facebook app. In fact, it will likely make it much easier to port features to a Facebook app when the time comes.

I still want Google to index my data, so I’ll probably serve HTTP_USER_AGENT .*(yahoo|googlebot).* some bare scaffolding representation of the site’s resources for indexing.

But you know, I just can’t help feel that this could all be avoided if I had some intermediary template language that could be compiled into both PHP and JS.

Or some (fast) PHP library similar to hpricot / hquery.

Or a (fast) way to run server-side javascript.

In the end, I get the feeling that offloading the site’s template rendering to the client is going to save my server a hell of a lot in view-rendering CPU cycles and excessive markup trafficking.

Have I overlooked some sort of Silver Bullet that I should know about?

Code Enforcement

“Excuse me sir, how long’s it been since you updated your unit tests?”