Example #1
0
 public function testToArrayRecursiveObject()
 {
     $data = new ViewData([]);
     $child = new \stdClass();
     $child->key = 'value';
     $data->child = $child;
     $array = $data->toArray();
     $this->assertArrayHasKey('child', $array);
     $this->assertArrayHasKey('key', $array['child']);
 }
 /**
  * Render this template with the provided data
  *
  * @param  ViewData $data [description]
  * @return [type]       [description]
  */
 public function render($data = null)
 {
     // Ensure that $data is not null
     if (!isset($data)) {
         $data = new ViewData(array());
     }
     // Access the singleton Twig engine
     $engine = Twig::instance();
     // Render the template
     return $engine->render($this->templatePath(), $data->toArray());
 }
Example #3
0
 /**
  * Retrieve data for a patter
  *
  * @param  String $id The id of the pattern
  * @param  Boolean $resolveAlias Whether or not to resolve data from aliased patterns (e.g. button~outline -> button)
  * @return ViewData     The decoded JSON data
  */
 public static function getDataForPattern($id, $resolveAlias = false)
 {
     $data = array();
     $id = Primer::cleanId($id);
     // Load the Patterns default data
     $defaultData = @file_get_contents(Primer::$PATTERN_PATH . '/' . $id . '/data.json');
     if ($defaultData) {
         $json = json_decode($defaultData);
         if ($json) {
             // Merge in the data
             $data += (array) $json;
         }
     }
     if ($resolveAlias) {
         // Parent data - e.g. elements/button is the parent of elements/button~primary
         $parentData = array();
         // Load parent data if this is inherit
         if (preg_match('/(.*?)~.*?/', $id, $matches)) {
             $parentData = FileSystem::getDataForPattern($matches[1]);
         }
         // Merge the parent and pattern data together, giving preference to the pattern data
         $data = array_replace_recursive((array) $parentData, (array) $data);
     }
     // Create the data structure
     $viewData = new ViewData($data);
     // Give the system a chance to mutate the data
     ViewData::fire($id, $viewData);
     // Return the data structure
     return $viewData;
 }
Example #4
0
 */
Event::listen('render', function ($data) {
    $data->primer->environment = 'development';
});
/**
 * Listen for when new Handlebars objects are created so that we can register any required helpers
 */
Event::listen('handlebars.init', function ($handlebars) {
});
/**
 * Listen for when a View (not pattern template) is about to be rendered
 * view.[viewName] - below example would call when views/pattern.handlebars is loaded
 */
View::composer('pattern', function ($data, $eventId) {
    // $data->id = 'testing';
});
/**
 * A function that calls anytime a data for a pattern is loaded
 * Useful for dynamically generating pattern data, e.g. sprites
 * $data is the raw output of the data.json
 */
ViewData::composer('elements/forms/input', function ($data) {
    // $data->label = 'boo yah!';
});
/**
 * Create an instance of Primer
 *
 * @var Primer
 */
$primer = Primer::start(['basePath' => __DIR__ . '/..', 'templateClass' => HandlebarsTemplateEngine::class]);
return $primer;
Example #5
0
 */
Event::listen('render', function ($data) {
    $data->primer->environment = 'development';
});
/**
 * Listen for when new Handlebars objects are created so that we can register any required helpers
 */
Event::listen('handlebars.init', function ($handlebars) {
});
/**
 * Listen for when a View (not pattern template) is about to be rendered
 * view.[viewName] - below example would call when views/pattern.handlebars is loaded
 */
View::composer('pattern', function ($data, $eventId) {
    // $data->id = 'testing';
});
/**
 * A function that calls anytime a data for a pattern is loaded
 * Useful for dynamically generating pattern data, e.g. sprites
 * $data is the raw output of the data.json
 */
ViewData::composer('elements/forms/input', function ($data, $id, $originalId) {
    // $data->label = 'boo yah!';
});
/**
 * Create an instance of Primer
 *
 * @var Primer
 */
$primer = Primer::start(['basePath' => __DIR__ . '/..', 'templateClass' => HandlebarsTemplateEngine::class]);
return $primer;
Example #6
0
 public function testLoadingDataShouldTriggerEventsForParentPaths()
 {
     $testData = new \stdClass();
     $testData->count = 0;
     $event1 = ViewData::composer('components/events/*', function ($data) use($testData) {
         // Is the data the right type?
         $this->assertEquals(ViewData::class, get_class($data));
         // Increment the count
         $testData->count++;
     });
     $event2 = ViewData::composer('components/*', function ($data) use($testData) {
         // Is the data the right type?
         $this->assertEquals(ViewData::class, get_class($data));
         // Increment the count
         $testData->count++;
     });
     $viewData = FileSystem::getDataForPattern('components/events/loading-data');
     $this->assertEquals(2, $testData->count);
     // Unbind event so it can't mess with other tests
     $event1->stop();
     $event2->stop();
 }