Archive for May, 2011

Three Line Ternaries

These little buggers are so much more readable than their single-line brethren:

$yourmom['fat_joke'] = $yourmom['dinner'] instanceof Elephant
	? "so fat she ate Dumbo" 
	: "so skinny Africa sends her food";

Anonymous Functions in Mustache

When using Anonymous Functions in Mustache, it’s important to note this line from the spec:
https://github.com/mustache/spec/blob/master/specs/~lambdas.yml#L82

Lambdas used for sections should receive the raw section string.

This means that the inputs to your lambda will not be pre-rendered. The output of the lambda will be rendered.

So I had a lambda that looked like this…

function($timestamp) {
	return str_replace("T", " ", $timestamp);
}

But It was giving jibberish.
bobthecow (author of Mustache.php) helped me realize that I needed to render the inputs myself…

function($timestamp) use($view) {
	return str_replace("T", " ", $view->render($timestamp));
}

The lesson is this:
The next time I run into a problem, I’ll be sure to read the tests.

Thanks, Justin!