Comments (5)
If the goal is simply to autoload partials, you could iterate over the array version of the parsed template:
$m = new Mustache();
$ast = $m->parse('{{> myPartial}}');
var_export($ast->toArray());
produces
array (
'type' => 1,
'flags' => 0,
'children' =>
array (
0 =>
array (
'type' => 512,
'flags' => 0,
'data' => 'myPartial',
),
),
);
and render:
var_export($m->render($ast, array(), array('myPartial' => 'blah'));
produces
'blah'
from php-mustache.
Autoloading partials is one potential use case, sure. It's just an extension point, really. If you can pass in an ArrayAccess for partials (and data for that matter), you can pretty much do as you wish.
I'm experimenting with Mustache to replace an existing template syntax which is rather similar, but needs a bit of massaging. It could be a drop-in replacement if I had an extension point that let me handle the array access that the renderer does with my own function.
But I think it's ultimately an idiomatic issue: PHP provides the ArrayAccess interface so you can do exactly this - substitute your own behaviour in when something expects an associative array. It would be great if this extension could support that idiom too.
from php-mustache.
Actually, if it was a case of one or the other, having data support ArrayAccess rather than partials would be far more flexible.
from php-mustache.
This project is PHP bindings for a C++ implementation of mustache. The goal was to provide an extremely fast version of mustache for PHP. At the time I wrote it, mustache.php was orders of magnitude slower than any other mustache implementation. I believe it has gotten better since then, so if you want flexibility, I would recommend that project.
I haven't re-run the benchmarks for a while, but this is what I'm referring to for mustache.php's performance: https://github.com/jbboehr/mustache-benches/wiki/2012.03.25.20.05
Like I said before, you can get pretty far by reducing the input data before it's sent to the C++ VM:
<?php
class MyArrayClass implements ArrayAccess {
public $array = array();
public function offsetExists($offset) {
return isset($this->array[$offset]);
}
public function offsetGet($offset) {
return $this->array[$offset];
}
public function offsetSet($offset, $value) {
$this->array[$offset] = $value;
}
public function offsetUnset($offset) {
unset($this->array[$offset]);
}
public function toArray() {
return $this->array;
}
}
class MyMustache extends Mustache {
public function render($tmpl, $data, array $partials = array()) {
return parent::render($tmpl, $data->toArray(), $partials);
}
}
$arr = new MyArrayClass();
$arr['foo'] = 'bar';
$tmpl = '{{foo}}';
$m = new MyMustache();
$out = $m->render($tmpl, $arr);
var_export($out);
'bar'
If you pass it in directly, the VM just sees the internal data of the object (just like it's actually stored):
$data = new MustacheData($arr);
var_export($data->toValue());
array (
'array' =>
array (
'foo' => 'bar',
),
)
from php-mustache.
This project is PHP bindings for a C++ implementation of mustache.
Ahh, I misunderstood - I didn't realise the C++ part was built to stand alone, I thought it was all in support of the PHP extension. This is not really an appropriate request in that case.
from php-mustache.
Related Issues (18)
- What's licence for code
- Code coverage HOT 1
- Can't store MustacheTemplate in APC HOT 22
- PHP7: php-mustache do not pass "make test" (segfaults) HOT 12
- Segmentation fault when $data contains objects HOT 7
- Error when building HOT 1
- Strange error "free(): invalid pointer" HOT 6
- Improve install instructions HOT 4
- Question: Is it possible to extract or access comment data? HOT 7
- crash when HTML is inside tags HOT 3
- Segfault on Fedora 28 HOT 3
- Error compiling on OS X HOT 2
- Deprecate and/or remove VM HOT 2
- pecl install error HOT 3
- PHP 7.4 support
- Memory Leak with PHP-CLI HOT 4
- Test against PHP7 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-mustache.