Skip to content

Commit

Permalink
allow dynamically binding data to views.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Sep 30, 2013
1 parent 328e5cf commit e7fd520
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Illuminate/Foundation/changes.json
Expand Up @@ -36,6 +36,7 @@
{"message": "Added Queue::bulk method for pushing several jobs out at once.", "backport": null}, {"message": "Added Queue::bulk method for pushing several jobs out at once.", "backport": null},
{"message": "Added 'dates' property to Eloquent model for convenient setting of date columns.", "backport": null}, {"message": "Added 'dates' property to Eloquent model for convenient setting of date columns.", "backport": null},
{"message": "Added 'chunk' method to query builder and Eloquent for doing work on large result sets.", "backport": null}, {"message": "Added 'chunk' method to query builder and Eloquent for doing work on large result sets.", "backport": null},
{"message": "Facades are now mockable without an application root.", "backport": null} {"message": "Facades are now mockable without an application root.", "backport": null},
{"message": "Data may now be dynamically bound to views via magic methods.", "backport": null}
] ]
} }
17 changes: 17 additions & 0 deletions src/Illuminate/View/View.php
Expand Up @@ -304,6 +304,23 @@ public function __unset($key)
unset($this->data[$key]); unset($this->data[$key]);
} }


/**
* Dynamically bind parameters to the view.
*
* @param string $method
* @param array $parameters
* @return \Illuminate\View\View
*/
public function __call($method, $parameters)
{
if (starts_with($method, 'with'))
{
return $this->with(snake_case(substr($method, 4)), $parameters[0]);
}

throw new \BadMethodCallException("Method [$method] does not exist on view.");
}

/** /**
* Get the string contents of the view. * Get the string contents of the view.
* *
Expand Down
5 changes: 5 additions & 0 deletions tests/View/ViewTest.php
Expand Up @@ -18,6 +18,11 @@ public function testDataCanBeSetOnView()
$view->with('foo', 'bar'); $view->with('foo', 'bar');
$view->with(array('baz' => 'boom')); $view->with(array('baz' => 'boom'));
$this->assertEquals(array('foo' => 'bar', 'baz' => 'boom'), $view->getData()); $this->assertEquals(array('foo' => 'bar', 'baz' => 'boom'), $view->getData());


$view = new View(m::mock('Illuminate\View\Environment'), m::mock('Illuminate\View\Engines\EngineInterface'), 'view', 'path', array());
$view->withFoo('bar')->withBaz('boom');
$this->assertEquals(array('foo' => 'bar', 'baz' => 'boom'), $view->getData());
} }




Expand Down

0 comments on commit e7fd520

Please sign in to comment.