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!!'); }
/** * @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}"); }
/** * @dataProvider renderErrorProvider */ public function testRenderingErrorLog($test) { start_catch_error_log(); $php = LightnCandy::compile($test['template'], $test['options']); $renderer = LightnCandy::prepare($php); $renderer(null, LCRun3::DEBUG_ERROR_LOG); $e = stop_catch_error_log(); if ($e) { $this->assertEquals(array($test['expected']), $e); } else { $this->markTestIncomplete('skip HHVM'); } }
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)); }
<?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";
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"]; $mail->Body = $notification["message"];