Code Monkey home page Code Monkey logo

Comments (5)

jbboehr avatar jbboehr commented on May 24, 2024

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.

shabbyrobe avatar shabbyrobe commented on May 24, 2024

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.

shabbyrobe avatar shabbyrobe commented on May 24, 2024

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.

jbboehr avatar jbboehr commented on May 24, 2024

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.

shabbyrobe avatar shabbyrobe commented on May 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.