Skip to content

Commit

Permalink
Added View::assign()
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Jan 14, 2016
1 parent b0354a8 commit 5f9f60e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ class ArticlesController extends Controller
> **Note:** The `model` getter is provided by the [icanboogie/module][] package, and is only
available if the route has a `module` property, which is automatic for routes defined by modules.

The `assign()` method may be used to assign multiple values to the view with a single call:

```php
<?php

$content = new SignupForm;
$title = "Sign up";
$params = $_POST;

$this->view->assign(compact('content', 'title', 'params'));
```




Expand Down
14 changes: 14 additions & 0 deletions lib/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,20 @@ public function offsetUnset($offset)
unset($this->variables[$offset]);
}

/**
* Assign multiple variables.
*
* @param array $variables
*
* @return $this
*/
public function assign(array $variables)
{
$this->variables = array_merge($this->variables, $variables);

return $this;
}

/**
* Resolve a template pathname from its name and type.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ public function test_get_variables()
$this->assertEquals([ 'content' => $content, 'v1' => $v1, 'v2' => $v2, 'view' => $view ], $view->variables);
}

public function test_assign()
{
$controller = $this->controller;
$content = self::generate_bytes();
$v1 = self::generate_bytes();
$v2 = self::generate_bytes();

$view = new View($controller);
$view->assign(compact('content', 'v1', 'v2'));

$this->assertSame($content, $view->content);
$this->assertSame($content, $view['content']);
$this->assertEquals([ 'content' => $content, 'v1' => $v1, 'v2' => $v2, 'view' => $view ], $view->variables);
}

/**
* @dataProvider provide_test_get_template
*
Expand Down

0 comments on commit 5f9f60e

Please sign in to comment.