Permalink
Browse files

allow dynamically binding data to views.

  • Loading branch information...
taylorotwell committed Sep 30, 2013
1 parent 328e5cf commit e7fd520a8b7d38b9aa68aee8fea880b725da46e9
Showing with 24 additions and 1 deletion.
  1. +2 −1 src/Illuminate/Foundation/changes.json
  2. +17 −0 src/Illuminate/View/View.php
  3. +5 −0 tests/View/ViewTest.php
@@ -36,6 +36,7 @@
{"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 '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}
]
}
@@ -304,6 +304,23 @@ public function __unset($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.
*
@@ -18,6 +18,11 @@ public function testDataCanBeSetOnView()
$view->with('foo', 'bar');
$view->with(array('baz' => 'boom'));
$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());
}

0 comments on commit e7fd520

Please sign in to comment.