/** * Tests that exception is thrown if wrong number of arguments is used. * * @expectedException InvalidArgumentException * @dataProvider wrongArgumentsProvider */ public function testArgumentsCount($template) { $storage = new BlockStorage(); $helpers = new \Handlebars\Helpers(array('override' => new OverrideHelper($storage))); $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); $engine->render($template, array()); }
/** * Render given template using supplied data * * @param $path string Path to template file (absolute) * @param $data array Data to be rendered * @return string Template output */ public function render($path, $data) { wfProfileIn(__METHOD__); $templateName = $this->extractTemplateNameFromPath($path); $templateDir = $this->extractTemplateDirFromPath($path); $partialsDir = $templateDir . DIRECTORY_SEPARATOR . self::PARTIALS_DIRECTORY; $partials = is_dir($partialsDir) ? $partialsDir : $templateDir; wfProfileIn(__METHOD__ . " - template: {$path}"); $handlebars = new \Handlebars\Handlebars(); $handlebars->setLoader(new \Handlebars\Loader\FilesystemLoader($templateDir)); $handlebars->setPartialsLoader(new \Handlebars\Loader\FilesystemLoader($partials, ['prefix' => self::PARTIALS_PREFIX])); $contents = $handlebars->render($templateName, $data); wfProfileOut(__METHOD__ . " - template: {$path}"); wfProfileOut(__METHOD__); return $contents; }
/** * Tests that conditions related with inheritance works as expected. */ public function testConditions() { $storage = new BlockStorage(); $helpers = new \Handlebars\Helpers(array('block' => new BlockHelper($storage), 'extends' => new ExtendsHelper($storage), 'override' => new OverrideHelper($storage), 'ifOverridden' => new IfOverriddenHelper($storage), 'unlessOverridden' => new UnlessOverriddenHelper($storage))); $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); // Test "ifOverridden" helper $engine->setLoader(new \Handlebars\Loader\ArrayLoader(array('parent' => '{{#block "name"}}{{/block}}{{#ifOverridden "name"}}true{{else}}false{{/ifOverridden}}', 'child' => '{{#extends "parent"}}{{#override "name"}}{{/override}}{{/extends}}', 'another_child' => '{{#extends "parent"}}{{/extends}}'))); $this->assertEquals($engine->render('parent', array()), 'false'); $this->assertEquals($engine->render('child', array()), 'true'); $this->assertEquals($engine->render('another_child', array()), 'false'); // Test "unlessOverridden" helper $engine->setLoader(new \Handlebars\Loader\ArrayLoader(array('parent' => '{{#block "name"}}{{/block}}{{#unlessOverridden "name"}}false{{else}}true{{/unlessOverridden}}', 'child' => '{{#extends "parent"}}{{#override "name"}}{{/override}}{{/extends}}', 'another_child' => '{{#extends "parent"}}{{/extends}}'))); $this->assertEquals($engine->render('parent', array()), 'false'); $this->assertEquals($engine->render('child', array()), 'true'); $this->assertEquals($engine->render('another_child', array()), 'false'); }
/** * Tests that exception is thrown if wrong number of arguments is used. * * @expectedException InvalidArgumentException * @dataProvider wrongArgumentsSetProvider */ public function testArgumentsCount($template) { $helpers = new \Handlebars\Helpers(array('replace' => new ReplaceHelper())); $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); $engine->render($template, array()); }
/** * Tests that exception is thrown if arguments are invalid. * * @expectedException InvalidArgumentException * @dataProvider invalidArgumentsProvider */ public function testInvalidArguments($template) { $helpers = new \Handlebars\Helpers(array('truncate' => new TruncateHelper())); $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); $engine->render($template, array()); }
/** * Test if and unless adding an extra layer when accessing parent */ public function testIfUnlessExtraLayer() { $loader = new \Handlebars\Loader\StringLoader(); $engine = new \Handlebars\Handlebars(array('loader' => $loader)); $this->assertEquals('good', $engine->render('{{#with b}}{{#if this}}{{../../a}}{{/if}}{{/with}}', array('a' => 'good', 'b' => 'stump'))); $this->assertEquals('good', $engine->render('{{#with b}}{{#unless false}}{{../../a}}{{/unless}}{{/with}}', array('a' => 'good', 'b' => 'stump'))); }
/** * Tests invalid arguments type. * * @expectedException InvalidArgumentException * @dataProvider invalidArgumentsProvider */ public function testInvalidArguments($collection) { $helpers = new \Handlebars\Helpers(array('first' => new FirstHelper())); $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); $engine->render('{{first collection}}', array('collection' => $collection)); }
/** * Tests that exception is thrown if wrong number of arguments is used. * * @expectedException InvalidArgumentException * @dataProvider wrongArgumentsProvider */ public function testArgumentsCount($template) { $helpers = new \Handlebars\Helpers(array('ifBetweenRightClosed' => new IfBetweenRightClosedHelper())); $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); $engine->render($template, array()); }
/** * Tests that exception is thrown if wrong number of arguments is used. * * @expectedException InvalidArgumentException * @dataProvider wrongArgumentsProvider */ public function testArgumentsCount($template) { $helpers = new \Handlebars\Helpers(array('unlessEqual' => new UnlessEqualHelper())); $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); $engine->render($template, array()); }
// Start of the flow: if (!$_POST) { // Fetch appointment types: $appointmentTypes = $acuity->request('/appointment-types'); $_SESSION['appointmentTypes'] = $appointmentTypes; } elseif ($_POST['appointmentTypeID']) { // Appointment type selected: foreach ($_SESSION['appointmentTypes'] as $appointmentType) { if ($_POST['appointmentTypeID'] == $appointmentType['id']) { $_SESSION['appointmentType'] = $appointmentType; break; } } // Time to select a date: $month = strftime('%Y-%m'); $dates = $acuity->request("/availability/dates?month={$month}&appointmentTypeID={$_SESSION['appointmentType']['id']}"); } elseif ($_POST['date']) { // Date selected. Now select a time: $date = $_SESSION['date'] = $_POST['date']; $times = $acuity->request('/availability/times', array('query' => array('date' => $date, 'appointmentTypeID' => $_SESSION['appointmentType']['id']))); } elseif ($_POST['time']) { // Time selected. Now fill in appointmente details. $time = $_SESSION['time'] = $_POST['time']; } else { // Create the appointment: $appointment = $acuity->request('/appointments', array('method' => 'POST', 'data' => array('appointmentTypeID' => $_SESSION['appointmentType']['id'], 'datetime' => $_SESSION['time'], 'firstName' => $_POST['firstName'], 'lastName' => $_POST['lastName'], 'email' => $_POST['email']))); } } $options = array('start' => $start, 'appointmentTypes' => $appointmentTypes, 'dates' => $dates, 'times' => $times, 'appointmentType' => $_SESSION['appointmentType'], 'date' => $_SESSION['date'], 'time' => $_SESSION['time'], 'appointment' => $appointment ? json_encode($appointment, JSON_PRETTY_PRINT) : null); echo $engine->render('index', $options);
/** * Tests that exception is thrown if arguments are invalid. * * @expectedException InvalidArgumentException */ public function testInvalidArguments() { $helpers = new \Handlebars\Helpers(array('repeat' => new RepeatHelper())); $engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); $engine->render('{{#repeat -10}}+{{/repeat}}', array()); }
/** * Test 'root' special variable * * @param string $template template text * @param array $data context data * @param string $results The Expected Results * * @dataProvider rootSpecialVariableProvider * * @return void */ public function testRootSpecialVariableHelpers($template, $data, $results) { $engine = new \Handlebars\Handlebars(); $res = $engine->render($template, $data); $this->assertEquals($res, $results); }
/** * Test partial loader */ public function testPartialLoader() { $loader = new \Handlebars\Loader\StringLoader(); $partialLoader = new \Handlebars\Loader\FilesystemLoader(realpath(__DIR__ . '/../fixture/data')); $engine = new \Handlebars\Handlebars(); $engine->setLoader($loader); $engine->setPartialsLoader($partialLoader); $this->assertEquals('test', $engine->render('{{>loader}}', array())); }
/** * Renders a handlebars template specified by $path, using scope variables defined inside $locals. * This function uses config('dispatch.views') for the location of the templates and partials, unless overridden by config('handlebars.views'). * Settings for 'handlebars.charset' and 'handlebars.layout' are also pulled from config(), if present. * * @param string $path name of the .handlebars file to render * @param array $locals scope variables to load inside the template * @param string $layout layout file to use, or flag to not use one * * @return string rendered code for the template */ function handlebars_template($path, $locals = array()) { static $engine = null; // create the engine once if (!$engine) { $views_path = config('handlebars.views') ?: config('dispatch.views'); // // Handlebars // $opts = array('loader' => new \Handlebars\Loader\FilesystemLoader($views_path), 'partials_loader' => new \Handlebars\Loader\FilesystemLoader($views_path, array('prefix' => config('handlebars.partials_prefix') ?: '_')), 'charset' => config('handlebars.charset') ?: 'UTF-8'); if ($cache_path = config('handlebars.cache')) { $opts['cache'] = $cache_path; } $engine = new Handlebars\Handlebars($opts); // // Handlebars Helpers // $helpers = config('handlebars.helpers'); if ($helpers) { foreach ($helpers as $helper => $callback) { $engine->addHelper($helper, function ($template, $context, $args, $source) use($callback) { return call_user_func($callback, $template, $context, $args, $source); }); } } $engine->addHelper('capitalize', function ($template, $context, $args, $source) { return ucwords($context->get($args)); }); $engine->addHelper('upper', function ($template, $context, $args, $source) { return strtoupper($context->get($args)); }); $engine->addHelper('lower', function ($template, $context, $args, $source) { return strtolower($context->get($args)); }); $engine->addHelper('url', function ($template, $context, $args, $source) { return config('dispatch.url') . $context->get($args); }); } // render partial using $locals if (config('handlebars.minify')) { return handlebars_minify($engine->render($path, $locals)); } return $engine->render($path, $locals); }