Pragmas execute a function #110

Closed
jcubic opened this Issue Sep 13, 2012 · 3 comments

Comments

Projects
None yet
3 participants

jcubic commented Sep 13, 2012

Pragmas execute a function, and they can be used as tags that return empty string (maybe newline after pragma should be remove too).

class Mustache {
...
protected function _renderTag($modifier, $tag_name, $leading, $trailing) {
      switch ($modifier) {
          case '=':
              return $this->_changeDelimiter($tag_name, $leading, $trailing);
              break;
          ...
          case '&':
                       if ($this->$unescape) {
                           return $this->_renderEscaped($tag_name, $leading, $trailing);
                       } else {
                           return $this->_renderUnescaped($tag_name, $leading, $trailing);
                       }
                       break;
          case '%':
                       $method = '_' . strtolower($tag_name);
                       if (in_array($method, get_class_methods(get_class($this)))) {
                             call_user_func_array(array($this, $method));
                       }
                       return '';
          ...
       }
}
function _unescape() {
    $this->unescape = true;
}

I'm not sure but maybe this will allow to have something like

<html>
   <body>
      <h1>Title</h1>
      {{%unescape}}
      {{raw}}
      {{%escape}}
   </body>
</html>

or if pragmas are swiches like

function _unescape() {
    $this->unescape = !$this->unescape;
}
<html>
   <body>
      <h1>{{title}}</h1>
      {{%unescape}}
      {{raw_data}}
      {{%unescape}}
   </body>
</html>

and adding new pragma will be very easy

PS: no code because it's an idea, and this change probably will allow to delete some code, since pragmas are not handle as tags, but I can provide code for this.

@jcubic Is this a feature of mustache in general that you want to be present in mustache.php or is it not (yet) present in mustache itself? If it's not present in mustache itself, I'm not sure you'd want to introduce it here, considering that it's probably better that mustache libraries should follow the mustache spec.

Comments?

jcubic commented Nov 18, 2012

This is the feature that is already in Mustache.php. There is one pragma, it's undocumented I think, but it's there. The code indicate that there will be more pragmas in the future, so this implementation will be much better since it will be very easy to add new pragmas and maybe users will be able to create their own.

Owner

bobthecow commented Nov 18, 2012

The develop branch currently supports a single pragma (filters). This has not made it into a stable release yet, so it hasn't been added to the documentation either.

@kohenkatz There's prior art for pragmas in Mustache. For example, "dot notation" started as a pragma — and was implemented in a couple of languages — before it was added to the spec.

That said, pragmas are by their very nature non-standard extensions to the Mustache spec. There is almost always a better way than resorting to a pragma. In the case of dot notation (and the new filters pragma) they are added as an unstable implementation of what will most likely become part of the Mustache spec at some point. The analog to this would be the -webkit-* and -moz-* css properties, as precursors to things like border-radius and text-shadow.

In the case of user-defined pragmas, I'm reluctant to add them, as any use case I can come up with them is essentially a workaround for not using a proper ViewModel :)

bobthecow closed this Dec 5, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment