Skip to content

Instantly share code, notes, and snippets.

@getify
Created August 12, 2012 14:15
Show Gist options
  • Save getify/3332023 to your computer and use it in GitHub Desktop.
Save getify/3332023 to your computer and use it in GitHub Desktop.
exploring inline decision-logic in templates
<!-- here's the PHP'ish way of making inline decisions while constructing HTML markup -->
<select name="foobar">
<option value="bam" <?=($foobar==="bam"?"selected":"")?>>Bam</option>
<option value="baz" <?=($foobar==="baz"?"selected":"")?>>Baz</option>
</select>
<input type="radio" name="foobar" value="bam" <?=($foobar==="bam"?"checked":"")?>> Bam
<input type="radio" name="foobar" value="baz" <?=($foobar==="baz"?"checked":"")?>> Baz
<!-- here's another PHP'ish approach using parameterized function calling -->
<?php
function selected($val,$test){ return ($val===$test?"selected":""); }
function checked($val,$test){ return ($val===$test?"checked":""); }
?>
<select name="foobar">
<option value="bam" <?=selected($foobar,"bam")?>>Bam</option>
<option value="baz" <?=selected($foobar,"baz")?>>Baz</option>
</select>
<input type="radio" name="foobar" value="bam" <?=checked($foobar,"bam")?>> Bam
<input type="radio" name="foobar" value="baz" <?=checked($foobar,"baz")?>> Baz
<!-- this illustrates the same problem but not in relation to forms. specifically,
the problem revolves around how to cleanly make conditional inclusion of small
snippets of markup inside other bigger markup templates. -->
<a href="<?=$bam?>" <?=isExternalLink($bam)?"target=_blank":""?>>Bam</a>
<a href="<?=$baz?>" <?=isExternalLink($baz)?"target=_blank":""?>>Baz</a>
<!--
one approach I'm considering for handlegrips:
http://github.com/getify/handlegrips
let you pre-compute a comparison hash for a data value (`data.foobar`) against a set
of values (`"bar","baz"`), and only make the assignment for the matching set value in
the comparison hash.
-->
{$: "#foobar" | data.foobar{"bar","baz"} = "checked" }
<input name="foobar" value="bar" {$=data.foobar{"bar"}$}>
<input name="foobar" value="baz" {$=data.foobar{"baz"}$}>
{$}
<!--
an alternative approach I'm considering for handlegrips:
http://github.com/getify/handlegrips
kind of like an array comprehension, where you can specify a set-literal as the
looping context of values, so your computation happens once for each iteration.
-->
{$: "#foobar" }
<!-- {$* is the looping construct. this example allows a set literal for the
looping context. you then evaluate the loop expression for each iteration,
setting a helper local variable `checked` to use in the markup.
-->
{$* ["bar","baz"] | checked = (data.foobar==.) ? "checked" }
<input name="foobar" value="{$=.$}" {$=checked$}>
{$}
{$}
@getify
Copy link
Author

getify commented Aug 18, 2012

@rhysbrettbowen-

I think what you're pointing out is valid but orthagonal to the question at hand. The context of this question is how to handle a specific task in the template-string rendering approach.

Sure, dynamic live updating of simple properties probably should NOT occur with a whole re-rendering of a template partial for the surrounding element... that would be overkill.

But templating stands on its own as a valid approach for initial view rendering (especially on the server). I know some people would prefer that all UI is built via DOM manipulation, but that's also very distasteful to many others for lots of reasons. Look at the most recent updates from Twitter, where they've gone back to a server-rendered view (using template-strings, undoubtedly) approach, because they found that to be more performant for what they care about.

Essentially, there are two different paradigms: string-based template rendering, and DOM-manipulation based rendering. They are both equally valid, and depending on needs you would do either or both in a project.

But I don't think it's valid to blanket claim (without any supporting detail) "It should just be done in a totally different paradigm."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment