Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow __call handle requests for view methods. #76

Closed
wants to merge 2 commits into from

3 participants

@conormcd

In the case where method_exists($view, $tag_name) fails and when isset($view->$tag_name) fails, this patch alters Mustache to attempt to call the method anyway. This allows the view to use __call to handle the method invocation.

Example:

class MyModel {
    public function baz() {
        return "baz";
    }
}
class MyView {
    public $foo = "foo";

    public function bar() {
        return "bar";
    }
}
class View {
    protected $_model;

    public function __construct($model) {
        $this->_model = $model;
    }

    public function __call($name, $args) {
        return call_user_func_array(array($this->_model, $name), $args);
    }
}

In this scenario, if we pass an instance of MyView to Mustache, then foo, bar and baz are all available.

@scribu

If the class doesn't have __call() defined and the method doesn't exist, it will result in a fatal error.

@conormcd

Good catch @scribu ! I've updated the patch accordingly.

@bobthecow
Owner

Mustache.php cannot support __call magic methods. Please see #16, #19 and #55, and this gist.

@bobthecow bobthecow closed this
@conormcd

Ah, I didn't think of that.

Thanks, and sorry for wasting your time.

@bobthecow
Owner

No worries. It's obviously a common need. Unfortunately it just doesn't work right in PHP.

@conormcd conormcd referenced this pull request from a commit
@conormcd conormcd Add a test to discourage people from implementing support for __call.
Pull in the test from https://gist.github.com/655784

This should discourage reports like #16, #19 and #55, and #76.
1ab066c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 0 deletions.
  1. +5 −0 Mustache.php
View
5 Mustache.php
@@ -795,6 +795,11 @@ protected function _findVariableInContext($tag_name, $context) {
return $view->$tag_name();
} else if (isset($view->$tag_name)) {
return $view->$tag_name;
+ } else if (method_exists($view, '__call')) {
+ $var = @$view->$tag_name();
+ if ($var) {
+ return $var;
+ }
}
} else if (is_array($view) && array_key_exists($tag_name, $view)) {
return $view[$tag_name];
Something went wrong with that request. Please try again.