public function testTemplate() { $template = "Welcome {{name}} , You win \${{value}} dollars!!\n"; $phpStr = LightnCandy::compile($template); $renderer = LightnCandy::prepare($phpStr); $this->assertEquals(trim($renderer(array('name' => 'John', 'value' => 10000))), 'Welcome John , You win $10000 dollars!!'); }
/** * Compile the view at the given path. * * @param string $path * @param array $options * @param bool $raw * @return void */ public function compileString($path, $raw = false) { $options = $this->options; // set partials directory if (!$raw) { $options['basedir'][] = dirname($path); } // set raw option array_set($options, 'compile_helpers_only', $raw); // set language helper functions if ($this->languageHelpers) { if (!$raw) { $helpers = array_merge($this->getLanguageHelpers(), $options['helpers']); } elseif ($this->translateRawOutput) { $helpers = $this->getLanguageHelpers(); } else { $helpers = []; } array_set($options, 'helpers', $helpers); } // compile with Handlebars compiler $contents = $this->lightncandy->compile($this->files->get($path), $options); if (!is_null($this->cachePath)) { $this->files->put($this->getCompiledPath($path, $raw), $contents); } }
/** * @dataProvider jsonSpecProvider */ public function testSpecs($spec) { global $tmpdir; $flag = LightnCandy::FLAG_MUSTACHE | LightnCandy::FLAG_ERROR_EXCEPTION | LightnCandy::FLAG_RUNTIMEPARTIAL; foreach (array($flag, $flag | LightnCandy::FLAG_STANDALONE) as $f) { $php = LightnCandy::compile($spec['template'], array('flags' => $f, 'partials' => isset($spec['partials']) ? $spec['partials'] : null, 'basedir' => $tmpdir)); $renderer = LightnCandy::prepare($php); $this->assertEquals($spec['expected'], $renderer($spec['data']), "[{$spec['file']}.{$spec['name']}]#{$spec['no']}:{$spec['desc']} PHP CODE: {$php}"); } }
/** * @dataProvider issueProvider */ public function testIssues($issue) { global $tmpdir; $php = LightnCandy::compile($issue['template'], isset($issue['options']) ? $issue['options'] : null); $context = LightnCandy::getContext(); if (count($context['error'])) { $this->fail('Compile failed due to:' . print_r($context['error'], true)); } $renderer = LightnCandy::prepare($php); $this->assertEquals($issue['expected'], $renderer($issue['data'], $issue['debug']), "PHP CODE:\n{$php}"); }
public function getTemplate($templateName) { if (!isset($this->templates[$templateName])) { $phpFile = $this->basePath . $templateName . '.php'; $templateFile = $this->basePath . $templateName . '.template'; $cacheOk = file_exists($phpFile) && filemtime($phpFile) >= filemtime($templateFile); if (!$cacheOk) { if (!file_exists($templateFile)) { throw new TemplatingException("Unable to load {$templateName} from {$templateFile}"); } $templateStr = file_get_contents($templateFile); $phpStr = LightnCandy::compile($templateStr, $this->getCompileOptions()); file_put_contents($phpFile, $phpStr); } $renderFunction = (include $phpFile); $this->templates[$templateName] = $renderFunction; } return $this->templates[$templateName]; }
/** * Generate HTML from Handlebars template * @param string $tpl * @param array $data */ public static function tpl($tpl, $data, $isPlugin = false) { $raw = self::get_template_contents($tpl, $isPlugin); $hash = md5($raw); $cachedPath = ABSPATH . 'cache/HB/compiled/' . $hash . '.php'; if (!file_exists($cachedPath)) { $contents = \LightnCandy::compile($raw, array('flags' => \LightnCandy::FLAG_ERROR_LOG | \LightnCandy::FLAG_STANDALONE | \LightnCandy::FLAG_HANDLEBARS, "helpers" => array("const" => function ($args) { //single argument call return constant($args[0]); }, "i18n" => function ($args) { return _t($args[0]); }, "hide" => function ($args) { return ""; }))); file_put_contents($cachedPath, $contents); } $renderer = (include $cachedPath); return $renderer($data); }
function render($path, $locals = array()) { // default views and cache directory $views_path = './app/views/'; $views_cache = config('views.cache') ?: './tmp/cache'; //get content from view $template = file_get_contents($views_path . $path . '.html'); require 'app/helpers.php'; $config_template = array('flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_SPVARS | LightnCandy::FLAG_INSTANCE | LightnCandy::FLAG_ERROR_EXCEPTION, 'basedir' => array($views_path), 'fileext' => array('.html'), 'hbhelpers' => $helpers); if (PHASE == 'development') { //compiling light candy $config_template['flags'] = LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_SPVARS | LightnCandy::FLAG_INSTANCE | LightnCandy::FLAG_ERROR_EXCEPTION; $phpStr = LightnCandy::compile($template, $config_template); //make cache file, with name $path, convert user/test to user-test $php_tmp = $views_cache . '/cache_' . str_replace('/', '-', $path) . '.php'; file_put_contents($php_tmp, $phpStr); } $renderer = (include $php_tmp); return $renderer($locals); }
/** * Do the rendering. Can be made protected when we're off PHP 5.3. * * @param string $fileName full path to template file * @param array $data rendering context * @param array $options options for LightnCandy::compile function * @return string rendered template */ public static function render($fileName, $data, $options = array()) { $defaultOptions = array('flags' => LightnCandy::FLAG_ERROR_EXCEPTION); $options = $options + $defaultOptions; $template = file_get_contents($fileName); if ($template === false) { throw new RuntimeException("Template file unavailable: [{$fileName}]"); } // TODO: Use MW-core implementation once it allows helper functions $code = LightnCandy::compile($template, $options); if (!$code) { throw new RuntimeException('Couldn\'t compile template!'); } if (substr($code, 0, 5) === '<?php') { $code = substr($code, 5); } $renderer = eval($code); if (!is_callable($renderer)) { throw new RuntimeException('Can\'t run compiled template!'); } $html = call_user_func($renderer, $data, array()); return $html; }
private function engine() { $template = "Welcome {{name}} , You win \${{value}} dollars!!\n"; $phpStr = LightnCandy::compile($template); // compiled PHP code in $phpStr // Step 2A. (Usage 1) use LightnCandy::prepare to get rendering function // DEPRECATED , it may require PHP setting allow_url_fopen=1 , // and allow_url_fopen=1 is not secure . // When allow_url_fopen = 0, prepare() will create tmp file then include it, // you will need to add your tmp directory into open_basedir. // YOU MAY NEED TO CHANGE PHP SETTING BY THIS WAY $renderer = LightnCandy::prepare($phpStr); // Step 2B. (Usage 2) Store your render function in a file // You decide your compiled template file path and name, save it. // You can load your render function by include() later. // RECOMMENDED WAY file_put_contents($php_inc, $phpStr); $renderer = (include $php_inc); // Step 3. run native PHP render function any time echo "Template is:\n{$template}\n\n"; echo $renderer(array('name' => 'John', 'value' => 10000)); echo $renderer(array('name' => 'Peter', 'value' => 1000)); }
/** * Compile the Mustache code into PHP code using LightnCandy * @param string $code Mustache code * @return string PHP code (with '<?php') * @throws RuntimeException */ protected function compile($code) { if (!class_exists('LightnCandy')) { throw new RuntimeException('LightnCandy class not defined'); } return LightnCandy::compile($code, ['flags' => LightnCandy::FLAG_ERROR_EXCEPTION, 'basedir' => $this->templateDir, 'fileext' => '.mustache']); }
<?php require 'src/lightncandy.php'; $template = '{{> (partial_name_helper type)}}'; $data = array('type' => 'dog', 'name' => 'Lucky', 'age' => 5); function partial_name_helper($type) { switch ($type[0]) { case 'man': case 'woman': return 'people'; case 'dog': case 'cat': return 'animal'; default: return 'default'; } } $php = LightnCandy::compile($template, array('flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_RUNTIMEPARTIAL | LightnCandy::FLAG_ERROR_EXCEPTION, 'helpers' => array('partial_name_helper'), 'partials' => array('people' => 'This is {{name}}, he is {{age}} years old.', 'animal' => 'This is {{name}}, it is {{age}} years old.', 'default' => 'This is {{name}}.'))); $renderer = LightnCandy::prepare($php); echo "Data:\n"; print_r($data); echo "\nTemplate:\n{$template}\n"; echo "\nCode:\n{$php}\n\n"; echo "\nOutput:\n"; echo $renderer($data); echo "\n";
/** * @dataProvider errorProvider */ public function testErrors($test) { global $tmpdir; $php = LightnCandy::compile($test['template'], $test['options']); $context = LightnCandy::getContext(); // This case should be compiled without error if (!isset($test['expected'])) { return; } $this->assertEquals($test['expected'], $context['error'], "Code: {$php}"); }
/** * @dataProvider compileProvider */ public function testUsedFeature($test) { LightnCandy::compile($test['template'], $test['options']); $context = LightnCandy::getContext(); $this->assertEquals($test['expected'], $context['usedFeature']); }
<?php require 'src/lightncandy.php'; $template = "Hello! {{name}} is {{gender}}.\nTest1: {{@root.name}}\nTest2: {{@root.gender}}\nTest3: {{../test3}}\nTest4: {{../../test4}}\nTest5: {{../../.}}\nTest6: {{../../[test'6]}}\n{{#each .}}\neach Value: {{.}}\n{{/each}}\n{{#.}}\nsection Value: {{.}}\n{{/.}}\n{{#if .}}IF OK!{{/if}}\n{{#unless .}}Unless not OK!{{/unless}}\n"; $php = LightnCandy::compile($template, array('flags' => LightnCandy::FLAG_RENDER_DEBUG | LightnCandy::FLAG_HANDLEBARSJS)); $renderer = LightnCandy::prepare($php); error_reporting(0); echo $renderer(array('name' => 'John'), LCRun3::DEBUG_TAGS_ANSI);
/** * Compile the Mustache code into PHP code using LightnCandy * @param string $code Mustache code * @return string PHP code (with '<?php') * @throws RuntimeException */ protected function compile($code) { if (!class_exists('LightnCandy')) { throw new RuntimeException('LightnCandy class not defined'); } return LightnCandy::compile($code, array('flags' => LightnCandy::FLAG_ERROR_EXCEPTION)); }
public function render(Entity $entity, $template) { $code = \LightnCandy::compile($template); $id = uniqid('', true); $fileName = 'data/cache/template-' . $id; $this->fileManager->putContents($fileName, $code); $renderer = (include $fileName); $this->fileManager->removeFile($fileName); $data = $this->getDataFromEntity($entity); $html = $renderer($data); $html = str_replace('?entryPoint=attachment&', '?entryPoint=attachment&', $html); $html = preg_replace('/\\?entryPoint=attachment\\&id=(.*)/', 'data/upload/$1', $html); return $html; }
require_once "require.php"; if (property_exists($data, "event") && property_exists($data->event, "person") && property_exists($data->event->person, "id") && property_exists($data, "notification_id") && property_exists($data, "cta_id")) { $person_id = $data->event->person->id; $person = json_decode($firebase->get("{$api_key}/people/{$person_id}"), true); unset($person->events); $data = json_decode(json_encode($data), true); $notification = json_decode($firebase->get("{$api_key}/ctas/{$data['cta_id']}/notifications/{$data['notification_id']}"), true); if (!isset($person["info"]["name"])) { $person["info"]["name"] = $person["info"]["firstName"] . " " . $person["info"]["lastName"]; } if (isset($person) && isset($person["info"])) { $data["event"]["person"] = array_merge($person["info"], $data["event"]["person"]); } $data["event"]["data"] = prepDataTable($data["event"]); foreach ($notification as $part => $v) { $notification[$part] = LightnCandy::compile($notification[$part]); $notification[$part] = LightnCandy::prepare($notification[$part]); $notification[$part] = $notification[$part]($data["event"]); } if ($notification["replyTo"] != "") { $replyto = explode("<", $notification["replyTo"]); if (count($replyto) == 1) { $replyto = trim(str_replace(">", "", $replyto[1])); $mail->addReplyTo($replyto); } else { $replyto = trim($notification["replyTo"]); $mail->addReplyTo($replyto, trim($replyto[0])); } } $mail->setFrom('*****@*****.**', 'Remetric'); $mail->Subject = $notification["subject"];