Skip to content

Instantly share code, notes, and snippets.

@mminguezz
Created March 22, 2016 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mminguezz/942d4b5e3d0e35749aab to your computer and use it in GitHub Desktop.
Save mminguezz/942d4b5e3d0e35749aab to your computer and use it in GitHub Desktop.
Pre parse underscore JS templates with PHP
<?php
$string = '
<div class="page-nav-header">
<h1>Compose</h1>
<ul class="nav nav-tabs">
<li id="type-email"<%= compose_type == \'email\' ? \' class="active"\' : \'\' %>><a href="#" class="email change-type-email">E-mail</a></li>
<li id="type-twitter"<%= compose_type == \'twitter\' ? \' class="active"\' : \'\' %>><a href="#" class="twitter change-type-twitter">Twitter</a></li>
</ul>
<br clear="all" />
</div>
<form class="form-horizontal">
<fieldset>
<legend id="form-title"></legend>
<div class="control-group fields-email">
<label class="control-label" for="field-to">To:</label>
<div class="controls">
<input type="text" name="to" id="field-to" class="input-xxlarge" />
</div>
</div>
<div class="control-group fields-email">
<label class="control-label" for="field-subject">Subject:</label>
<div class="controls">
<input type="text" name="subject" id="field-subject" class="input-xxlarge" />
</div>
</div>
</fieldset>
<fieldset>
<legend class="spacer fields-email">&nbsp;</legend>
<div class="control-group">
<div class="avatar"><label for="field-body"><img src="http://www.gravatar.com/" alt="" /></label></div>
<div class="controls">
<textarea name="body" class="input-xxlarge" id="field-body"></textarea>
</div>
</div>
<div class="form-actions">
<input type="submit" class="btn btn-success btn-large" value="Post" id="field-submit" /> <a href="/inbox" class="btn btn-large open_inbox">Cancel</a>
</div>
</fieldset>
</form>
';
$string = str_replace(array("\t", "\n"), array("\\t", "\\n"), $string);
$templateSettings = array(
'evaluate' => "/<%([\s\S]+?)%>/",
'interpolate' => "/<%=([\s\S]+?)%>/",
'escape' => "/<%-([\s\S]+?)%>/"
);
$unescape = function($code) {
return str_replace("\\'", "'", str_replace("\\\\", "\\", $code));
};
$string = preg_replace("/\\\/", "\\\\", $string);
$string = preg_replace("/'/", "\\'", $string);
$string = preg_replace_callback($templateSettings['escape'], function($match) use ($unescape) {
return "',_.escape(" . $unescape($match[1]) . "),'";
}, $string);
$string = preg_replace_callback($templateSettings['interpolate'], function($match) use ($unescape) {
return "'," . $unescape($match[1]) . ",'";
}, $string);
$string = preg_replace_callback($templateSettings['evaluate'], function($match) use ($unescape) {
return "');" . preg_replace("/[\r\n\t]/", " ", $unescape($match[1])) . ";__p.push('";
}, $string);
$string = preg_replace("/\r/", "\\r", $string);
$string = preg_replace("/\n/", "\\n", $string);
$string = preg_replace("/\t/", "\\t", $string);
$tmpl = "var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push('" . $string . "');}return __p.join('');";
echo $tmpl . PHP_EOL;
$expected = 'var __p=[],print=function(){__p.push.apply(__p,arguments);};with(obj||{}){__p.push(\'\n\t<div class="page-nav-header">\n\t\t<h1>Compose</h1>\n\t\t<ul class="nav nav-tabs">\n\t\t\t<li id="type-email"\', compose_type == \'email\' ? \' class="active"\' : \'\' ,\'><a href="#" class="email change-type-email">E-mail</a></li>\n\t\t\t<li id="type-twitter"\', compose_type == \'twitter\' ? \' class="active"\' : \'\' ,\'><a href="#" class="twitter change-type-twitter">Twitter</a></li>\n\t\t</ul>\n\t\t<br clear="all" />\n\t</div>\n\n\t<form class="form-horizontal">\n\t\t<fieldset>\n\t\t\t\n\t\t\t<legend id="form-title"></legend>\n\t\t\t\n\t\t\t<div class="control-group fields-email">\n\t\t\t\t<label class="control-label" for="field-to">To:</label>\n\t\t\t\t<div class="controls">\n\t\t\t\t\t<input type="text" name="to" id="field-to" class="input-xxlarge" />\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t\t<div class="control-group fields-email">\n\t\t\t\t<label class="control-label" for="field-subject">Subject:</label>\n\t\t\t\t<div class="controls">\n\t\t\t\t\t<input type="text" name="subject" id="field-subject" class="input-xxlarge" />\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t\n\t\t</fieldset>\n\t\t<fieldset>\t\n\t\t\t\n\t\t\t<legend class="spacer fields-email">&nbsp;</legend>\n\t\t\t\n\t\t\t<div class="control-group">\n\t\t\t\t<div class="avatar"><label for="field-body"><img src="http://www.gravatar.com/" alt="" /></label></div>\n\t\t\t\t<div class="controls">\n\t\t\t\t\t<textarea name="body" class="input-xxlarge" id="field-body"></textarea>\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t\t\n\t\t\t<div class="form-actions">\n\t\t\t\t<input type="submit" class="btn btn-success btn-large" value="Post" id="field-submit" /> <a href="/inbox" class="btn btn-large open_inbox">Cancel</a>\n\t\t\t</div>\n\t\t</fieldset>\n\t</form>\n\n\');}return __p.join(\'\');';
echo $expected . PHP_EOL;
var_dump($expected === $tmpl);
for($i = 0; $i < strlen($expected); $i++) {
if($expected{$i} != $tmpl{$i}) {
echo substr($expected, $i - 30, 60) . "<--" . PHP_EOL;
echo substr($tmpl, $i - 30, 60) . "<--" . PHP_EOL;
die("Error " . $i);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment