bobthecow / mustache.php

A Mustache implementation in PHP.
http://mustache.github.io/
MIT License
3.23k stars 418 forks source link

Allow to extend mustache #109

Open jcubic opened 11 years ago

jcubic commented 11 years ago

Maybe it's good idea to allow extension of mustache wihout modifing library or Mustache class so user can put {{@foo}} or {{%foo}} and those will execute user defined function

$mustache = Mustache_Engine(array(
    'modifiers' => array(
       '%' => function($tag) {
            return file_get_contents($tag);
       }
    )
));

or

$mustache = Mustache_Engine(array(
    'tags_extension' => function($modifier, $tag_name, $leading, $trailing) {
        switch($modifier) {
            case '%':  return file_get_contents($tag_name);
        }         
    }
));

and user will be able to put

<html>
   <body>
      <h1>My robots.txt</h1>
      <pre>{{%robots.txt}}</pre>
   </body>
</html>

and those modifiers will be check in _renderTag method after all of tags or before so they can be overwriten.

mxhCodes commented 2 years ago

What's wrong / missing with higher-order sections? https://github.com/bobthecow/mustache.php/wiki/Magic-Methods#__invoke You could pass such callbacks to the Mustache renderer as data array. Another option is defining reusable components as partials, and could then be included like {{>robots.txt}}

jcubic commented 2 years ago

@mxhCodes sorry it was 9 years ago when the issue was created. I don't have the use case for the requirement. No one replied for 9 years, so I think that the project is dead.

I think it was for this project Aiki Framework, where I needed my own markup DSL.

bobthecow commented 2 years ago

Ha! Sorry. It's not dead, I just missed this issue somehow when it was opened 😞

In general the mustache-y answer for this sort of thing would be higher order sections, filters, or a smarter view model.