Skip to content

Commit

Permalink
Fixed programatically loading a pattern with custom data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Hewitt committed Jun 20, 2016
1 parent e534552 commit 98209c3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Primer/Renderable/Pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public function __construct($id, $customData = array())
$this->loadCopy();

// Load the data
$this->loadData($customData);
$this->loadData();
$this->setData($customData);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Primer/Templating/ViewData.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public function merge($data)
{
$arr = [];

if (get_class($data) == ViewData::class) {
$arr = $data->toArray();
} elseif (is_array($data)) {
if (is_array($data)) {
$arr = $data;
} elseif (get_class($data) == ViewData::class) {
$arr = $data->toArray();
} else {
throw new Exception('Unexpected data type passed to ViewData merge function');
}

$this->setDataFromArray($data->toArray());
$this->setDataFromArray($arr);
}

protected function setDataFromArray(array $data)
Expand Down
27 changes: 27 additions & 0 deletions tests/PatternTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rareloop\Primer\Tests;

use Rareloop\Primer\Primer;
use Rareloop\Primer\Renderable\Pattern;
use Rareloop\Primer\TemplateEngine\Handlebars\Template as HandlebarsTemplateEngine;

class PatternTest extends \PHPUnit_Framework_TestCase
{
protected $primer;

public function testCustomPatternData()
{
$primer = Primer::start(array(
'basePath' => __DIR__.'/primer-test',
'templateClass' => HandlebarsTemplateEngine::class,
));

$pattern = new Pattern('components/patterns/custom-data', [
'name' => 'Test name',
]);

$output = $pattern->render(false);
$this->assertEquals("Test name", $output);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ name }}

0 comments on commit 98209c3

Please sign in to comment.