Skip to content

Instantly share code, notes, and snippets.

@mattwellss
Last active May 21, 2016 01:01
Show Gist options
  • Save mattwellss/8b8fecf01096eef68ca5624315d94a3c to your computer and use it in GitHub Desktop.
Save mattwellss/8b8fecf01096eef68ca5624315d94a3c to your computer and use it in GitHub Desktop.
<?php
// For use with really dumb simple php -S
if ($_SERVER['REQUEST_URI'] !== '/') {
return false;
}
$tmpl = <<<MUSTACHE
{{#enabled}}
<div class="add-to-cart">
{{^isGrouped}}
<label for="qty">{{#__}}Qty:{{/__}}</label>
<input type="text" name="qty" id="qty" maxlength="12" value="{{qty}}" title="{{#quote}}Qty{{/quote}}" class="input-text qty" />
{{/isGrouped}}
<button type="button" title="{{buttonTitle}}" id="product-addtocart-button" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span>{{buttonTitle}}</span></span></button>
</div>
{{/enabled}}
MUSTACHE;
require __DIR__ . '/vendor/autoload.php';
$m = new \Phly\Mustache\Mustache();
class AddToCart
{
private $data = [];
public function __get($name)
{
if ($this->__isset($name)) {
return $this->data[$name];
}
return '';
}
public function getData()
{
return $this->data;
}
public function __isset($name)
{
return isset($this->data[$name]);
}
public function __construct($data)
{
// The data hash is merged with Higher-Order versions of class methods
$this->data = array_merge([
'__' => function () {
return function ($text) {
return $this->__($text);
};
},
'quote' => function () {
return function ($text) {
return $this->quote($text);
};
}
], $data);
}
public function __($text)
{
return strtoupper($text);
}
public function quote($text)
{
return htmlspecialchars($this->__($text), ENT_QUOTES, null, false);
}
}
// Initialize a new View
$view = new AddToCart([
'enabled' => true,
'isGrouped' => false,
'qty' => 1,
'buttonTitle' => 'Add to cart!',
'children' => ''
]);
$viewData = $view->getData();
?>
<!DOCTYPE html>
<html>
<head>
<title>universal rendering</title>
</head>
<body>
<main><?= $m->render($tmpl, $viewData); ?></main>
<script src="/node_modules/hogan.js/dist/hogan-3.0.2.js"></script>
<script src="/node_modules/jquery/dist/jquery.js"></script>
<script src="/node_modules/underscore/underscore-min.js"></script>
<script src="/node_modules/backbone/backbone-min.js"></script>
<script type="text/template" id="add-to-cart-template">
<?= $tmpl ?>
</script>
<script>
var AddToCart = Backbone.View.extend({
el: $('main'),
data: {
qty: 1,
enabled: true,
isGrouped: false,
buttonTitle: 'Add To Cart',
children: '',
quote: function (text) { return text; },
__: function (text) { return text; }
},
render: function () {
this.$el.html(
this.template.render(this.data));
},
initialize: function (data) {
this.data = _.extend(this.data, data);
this.template = Hogan.compile($('#add-to-cart-template').html());
}
});
var atcInst = new AddToCart(<?= json_encode($viewData) ?>);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment