Skip to content

Commit 6b9d0df

Browse files
committed
Remove option to change runtime class
1 parent 8408a29 commit 6b9d0df

File tree

9 files changed

+20
-98
lines changed

9 files changed

+20
-98
lines changed

README.md

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ LightnCandy
33

44
⚡🍭 An extremely fast PHP implementation of handlebars ( http://handlebarsjs.com/ ) and mustache ( http://mustache.github.io/ ).
55

6-
Package on packagist: [![Latest Stable Version](https://poser.pugx.org/zordius/lightncandy/v/stable.svg)](https://packagist.org/packages/zordius/lightncandy) [![License](https://poser.pugx.org/zordius/lightncandy/license.svg)](https://github.com/zordius/lightncandy/blob/master/LICENSE.md) [![Total Downloads](https://poser.pugx.org/zordius/lightncandy/downloads)](https://packagist.org/packages/zordius/lightncandy)
7-
86
Features
97
--------
108

11-
* Logicless template: mustache ( http://mustache.github.com/ ) or handlebars ( http://handlebarsjs.com/ ) .
129
* Compile template to **pure PHP** code. Examples:
1310
* <a href="https://github.com/zordius/HandlebarsTest/blob/master/fixture/001-simple-vars.tmpl">Template A</a> generated <a href="https://github.com/zordius/HandlebarsTest/blob/master/fixture/001-simple-vars.php">PHP A</a>
1411
* <a href="https://github.com/zordius/HandlebarsTest/blob/master/fixture/016-hb-eachthis.tmpl">Template B</a> generated <a href="https://github.com/zordius/HandlebarsTest/blob/master/fixture/016-hb-eachthis.php">PHP B</a>
@@ -19,11 +16,8 @@ Features
1916
* Detail performance test reports can be found <a href="https://github.com/zordius/HandlebarsTest">here</a>, go http://zordius.github.io/HandlebarsTest/ to see charts.
2017
* **SMALL!** all PHP files in 189K
2118
* **ROBUST!**
22-
* 100% supports <a href="https://github.com/mustache/spec">mustache spec v1.1.3</a>. For the optional lambda module, supports 4 of 10 specs.
2319
* Supports almost all <a href="https://github.com/jbboehr/handlebars-spec">handlebars.js spec</a>
2420
* Output <a href="https://github.com/zordius/HandlebarsTest/blob/master/FEATURES.md">SAME</a> with <a href="https://github.com/wycats/handlebars.js">handlebars.js</a>
25-
* **FLEXIBLE!**
26-
* Lot of <a href="#compile-options">options</a> to change features and behaviors.
2721
* Context generation
2822
* Analyze used features from your template (execute `LightnCandy::getContext()` to get it) .
2923
* Debug
@@ -332,46 +326,11 @@ function ($in) {
332326

333327
Please make sure the passed in `renderex` is valid PHP, LightnCandy will not check it.
334328

335-
Customize Rendering Runtime Class
336-
---------------------------------
337-
338-
If you want to extend `LightnCandy\Runtime` class and replace the default runtime library, you may use `runtime` when `compile()` . For example, this sample will generate render function based on your extended `MyRunTime`:
339-
340-
```php
341-
// Customized runtime library to debug {{{foo}}}
342-
class MyRunTime extends LightnCandy\Runtime {
343-
public static function raw($cx, $v) {
344-
return '[[DEBUG:raw()=>' . var_export($v, true) . ']]';
345-
}
346-
}
347-
348-
// Use MyRunTime as runtime library
349-
$php = LightnCandy::compile($template, array(
350-
'runtime' => 'MyRunTime'
351-
));
352-
```
353-
354-
Please make sure `MyRunTime` exists when compile().
355-
356329
Unsupported Feature
357330
-------------------
358331

359332
* `{{foo/bar}}` style variable name, it is deprecated in official handlebars.js document, please use this style: `{{foo.bar}}`.
360333

361-
Suggested Handlebars Template Practices
362-
---------------------------------------
363-
364-
* Prevent to use `{{#with}}` . I think `{{path.to.val}}` is more readable then `{{#with path.to}}{{val}}{{/with}}`; when using `{{#with}}` you will confusing on scope changing. `{{#with}}` only save you very little time when you access many variables under same path, but cost you a lot time when you need to understand then maintain a template.
365-
* use `{{{val}}}` when you do not require HTML escaped output on the value. It is better performance, too.
366-
* Prevent to use custom helper if you want to reuse your template in different language. Or, you may need to implement different versions of helper in different languages.
367-
* For best performance, you should only use 'compile on demand' pattern when you are in development stage. Before you go to production, you can `LightnCandy::compile()` on all your templates, save all generated PHP codes, and deploy these generated files (You may need to maintain a build process for this) . **DO NOT COMPILE ON PRODUCTION** , it also a best practice for security. Adding cache for 'compile on demand' is not the best solution. If you want to build some library or framework based on LightnCandy, think about this scenario.
368-
* Recompile your templates when you upgrade LightnCandy every time.
369-
* Persistant ESCAPING practice of `{` or `}` for both handlebars and lightncandy:
370-
* If you want to display atomic `}}` , you can just use it without any trick. EX: `{{foo}} }}`
371-
* If you want to display `}` just after any handlebars token, you can use this: `{{#with "}"}}{{.}}{{/with}}` . EX: `{{foo}}{{#with "}"}}{{.}}{{/with}}`
372-
* If you want to display atomic `{` , you can just use it without any trick. EX: `{ and {{foo}}`.
373-
* If you want to display `{{` , you can use `{{#with "{{"}}{{.}}{{/with}}`. EX: `{{#with "{{"}}{{.}}{{/with}}{{foo}}`
374-
375334
Detail Feature list
376335
-------------------
377336

@@ -450,22 +409,3 @@ Go http://handlebarsjs.com/ to see more feature description about handlebars.js.
450409
* `{{#> @partial-block}}` : access partial block content inside a partial
451410
* `{{#*inline "partial_name"}}...{{/inline}}` : Inline partial, provide a partial and overwrite the original one.
452411
* `{{log foo}}` : output value to stderr for debug.
453-
454-
Developer Notes
455-
---------------
456-
457-
Please read <a href=".github/CONTRIBUTING.md">CONTRIBUTING.md</a> for development environment setup.
458-
459-
Framework Integration
460-
---------------------
461-
462-
- [Slim 3.0.x](https://github.com/endel/slim-lightncandy-view)
463-
- [Laravel 4](https://github.com/samwalshnz/lightncandy-l4)
464-
- [Laravel 5](https://github.com/ProAI/laravel-handlebars)
465-
- [yii2](https://github.com/kfreiman/yii2-lightncandy)
466-
- [Symfony3](https://packagist.org/packages/jays-de/handlebars-bundle)
467-
468-
Tools
469-
-----
470-
471-
- CLI: https://github.com/PXLbros/LightnCandy-CLI

src/Compiler.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,13 @@ public static function composePHPRender($context, $code)
7979
{
8080
$flagProp = Expression::boolString($context['flags']['prop']);
8181
$flagPartNC = Expression::boolString($context['flags']['partnc']);
82+
$runtime = Runtime::class;
8283

8384
$constants = Exporter::constants($context);
8485
$helpers = Exporter::helpers($context);
8586
$partials = implode(",\n", $context['partialCode']);
8687
$debug = Runtime::DEBUG_ERROR_LOG;
87-
$use = "use {$context['runtime']} as {$context['runtimealias']};";
88+
$use = "use {$runtime} as LR;";
8889
$stringObject = $context['flags']['prop'] ? Exporter::stringobject($context) : '';
8990
$safeString = ($context['usedFeature']['enc'] > 0) ? "use {$context['safestring']} as SafeString;" : '';
9091
// Return generated PHP code string.
@@ -105,7 +106,6 @@ public static function composePHPRender($context, $code)
105106
'sp_vars' => isset(\$options['data']) ? array_merge(array('root' => \$in), \$options['data']) : array('root' => \$in),
106107
'blparam' => array(),
107108
'partialid' => 0,
108-
'runtime' => '{$context['runtime']}',
109109
);
110110
{$context['renderex']}
111111
{$context['ops']['array_check']}
@@ -124,9 +124,9 @@ public static function composePHPRender($context, $code)
124124
*
125125
* @return string compiled Function name
126126
*
127-
* @expect 'LR::test(' when input array('flags' => array('debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 'LR'), 'test', ''
128-
* @expect 'LL::test2(' when input array('flags' => array('debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 'LL'), 'test2', ''
129-
* @expect 'RR::debug(\'abc\', \'test\', ' when input array('flags' => array('debug' => 1), 'runtime' => 'Runtime', 'runtimealias' => 'RR', 'funcprefix' => 'haha456'), 'test', 'abc'
127+
* @expect 'LR::test(' when input array('flags' => array('debug' => 0)), 'test', ''
128+
* @expect 'LR::test2(' when input array('flags' => array('debug' => 0)), 'test2', ''
129+
* @expect 'LR::debug(\'abc\', \'test\', ' when input array('flags' => array('debug' => 1), 'funcprefix' => 'haha456'), 'test', 'abc'
130130
*/
131131
protected static function getFuncName(&$context, $name, $tag)
132132
{
@@ -140,7 +140,7 @@ protected static function getFuncName(&$context, $name, $tag)
140140
$dbg = '';
141141
}
142142

143-
return "{$context['runtimealias']}::$name($dbg";
143+
return "LR::$name($dbg";
144144
}
145145

146146
/**
@@ -227,7 +227,7 @@ protected static function getVariableNameOrSubExpression(&$context, $var)
227227
* @expect array('((isset($cx[\'scopes\'][count($cx[\'scopes\'])-1]) && is_array($cx[\'scopes\'][count($cx[\'scopes\'])-1]) && isset($cx[\'scopes\'][count($cx[\'scopes\'])-1][\'a\'])) ? $cx[\'scopes\'][count($cx[\'scopes\'])-1][\'a\'] : null)', '../[a]') when input array('flags'=>array('debug'=>0,'prop'=>0)), array(1,'a')
228228
* @expect array('((isset($cx[\'scopes\'][count($cx[\'scopes\'])-3]) && is_array($cx[\'scopes\'][count($cx[\'scopes\'])-3]) && isset($cx[\'scopes\'][count($cx[\'scopes\'])-3][\'a\'])) ? $cx[\'scopes\'][count($cx[\'scopes\'])-3][\'a\'] : null)', '../../../[a]') when input array('flags'=>array('debug'=>0,'prop'=>0)), array(3,'a')
229229
* @expect array('(($inary && isset($in[\'id\'])) ? $in[\'id\'] : null)', 'this.[id]') when input array('flags'=>array('debug'=>0,'prop'=>0)), array(null, 'id')
230-
* @expect array('LR::v($cx, $in, isset($in) ? $in : null, array(\'id\'))', 'this.[id]') when input array('flags'=>array('prop'=>true,'debug'=>0), 'runtime' => 'Runtime', 'runtimealias' => 'LR'), array(null, 'id')
230+
* @expect array('LR::v($cx, $in, isset($in) ? $in : null, array(\'id\'))', 'this.[id]') when input array('flags'=>array('prop'=>true,'debug'=>0)), array(null, 'id')
231231
*/
232232
protected static function getVariableName(&$context, $var, $lookup = null, $args = null)
233233
{

src/Context.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ public static function create($options)
104104
'helpers' => array(),
105105
'renderex' => isset($options['renderex']) ? $options['renderex'] : '',
106106
'prepartial' => (isset($options['prepartial']) && is_callable($options['prepartial'])) ? $options['prepartial'] : false,
107-
'runtime' => isset($options['runtime']) ? $options['runtime'] : '\\LightnCandy\\Runtime',
108-
'runtimealias' => 'LR',
109107
'safestring' => '\\LightnCandy\\SafeString',
110108
'safestringalias' => isset($options['safestring']) ? $options['safestring'] : 'LS',
111109
'rawblock' => false,

src/Exporter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public static function stringobject($context)
196196
*/
197197
public static function runtime($context)
198198
{
199-
$class = new \ReflectionClass($context['runtime']);
199+
$class = new \ReflectionClass(Runtime::class);
200200
$ret = '';
201201
$methods = static::getClassMethods($context, $class);
202202

src/Runtime.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class Runtime extends Encoder
5555
* @param string $f runtime function name
5656
* @param array<string,array|string|integer> $cx render time context for lightncandy
5757
*
58-
* @expect '{{123}}' when input '123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS), 'runtime' => 'LightnCandy\\Runtime'), ''
59-
* @expect '<!--MISSED((-->{{#123}}<!--))--><!--SKIPPED--><!--MISSED((-->{{/123}}<!--))-->' when input '123', 'wi', array('flags' => array('debug' => Runtime::DEBUG_TAGS_HTML), 'runtime' => 'LightnCandy\\Runtime'), false, null, false, function () {return 'A';}
58+
* @expect '{{123}}' when input '123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS)), ''
59+
* @expect '<!--MISSED((-->{{#123}}<!--))--><!--SKIPPED--><!--MISSED((-->{{/123}}<!--))-->' when input '123', 'wi', array('flags' => array('debug' => Runtime::DEBUG_TAGS_HTML)), false, null, false, function () {return 'A';}
6060
*/
6161
public static function debug($v, $f, $cx)
6262
{
@@ -66,7 +66,8 @@ public static function debug($v, $f, $cx)
6666
for ($i=2;$i<count($P);$i++) {
6767
$params[] = &$P[$i];
6868
}
69-
$r = call_user_func_array((isset($cx['funcs'][$f]) ? $cx['funcs'][$f] : "{$cx['runtime']}::$f"), $params);
69+
$runtime = self::class;
70+
$r = call_user_func_array(($cx['funcs'][$f] ?? "{$runtime}::$f"), $params);
7071

7172
if ($cx['flags']['debug'] & static::DEBUG_TAGS) {
7273
$ansi = $cx['flags']['debug'] & (static::DEBUG_TAGS_ANSI - static::DEBUG_TAGS);

tests/CompilerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public function testOn_getFuncName() {
1515
$method = new \ReflectionMethod('LightnCandy\Compiler', 'getFuncName');
1616
$method->setAccessible(true);
1717
$this->assertEquals('LR::test(', $method->invokeArgs(null, array_by_ref(array(
18-
array('flags' => array('debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 'LR'), 'test', ''
18+
array('flags' => array('debug' => 0)), 'test', ''
1919
))));
20-
$this->assertEquals('LL::test2(', $method->invokeArgs(null, array_by_ref(array(
21-
array('flags' => array('debug' => 0), 'runtime' => 'Runtime', 'runtimealias' => 'LL'), 'test2', ''
20+
$this->assertEquals('LR::test2(', $method->invokeArgs(null, array_by_ref(array(
21+
array('flags' => array('debug' => 0)), 'test2', ''
2222
))));
23-
$this->assertEquals('RR::debug(\'abc\', \'test\', ', $method->invokeArgs(null, array_by_ref(array(
24-
array('flags' => array('debug' => 1), 'runtime' => 'Runtime', 'runtimealias' => 'RR', 'funcprefix' => 'haha456'), 'test', 'abc'
23+
$this->assertEquals('LR::debug(\'abc\', \'test\', ', $method->invokeArgs(null, array_by_ref(array(
24+
array('flags' => array('debug' => 1), 'funcprefix' => 'haha456'), 'test', 'abc'
2525
))));
2626
}
2727
public function testOn_getVariableNames() {
@@ -92,7 +92,7 @@ public function testOn_getVariableName() {
9292
array('flags'=>array('debug'=>0,'prop'=>0)), array(null, 'id')
9393
))));
9494
$this->assertEquals(array('LR::v($cx, $in, isset($in) ? $in : null, array(\'id\'))', 'this.[id]'), $method->invokeArgs(null, array_by_ref(array(
95-
array('flags'=>array('prop'=>true,'debug'=>0), 'runtime' => 'Runtime', 'runtimealias' => 'LR'), array(null, 'id')
95+
array('flags'=>array('prop'=>true,'debug'=>0)), array(null, 'id')
9696
))));
9797
}
9898
public function testOn_addUsageCount() {

tests/RuntimeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class RuntimeTest extends TestCase
1414
public function testOn_debug() {
1515
$method = new \ReflectionMethod('LightnCandy\Runtime', 'debug');
1616
$this->assertEquals('{{123}}', $method->invokeArgs(null, array_by_ref(array(
17-
'123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS), 'runtime' => 'LightnCandy\\Runtime'), ''
17+
'123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS)), ''
1818
))));
1919
$this->assertEquals('<!--MISSED((-->{{#123}}<!--))--><!--SKIPPED--><!--MISSED((-->{{/123}}<!--))-->', $method->invokeArgs(null, array_by_ref(array(
20-
'123', 'wi', array('flags' => array('debug' => Runtime::DEBUG_TAGS_HTML), 'runtime' => 'LightnCandy\\Runtime'), false, null, false, function () {return 'A';}
20+
'123', 'wi', array('flags' => array('debug' => Runtime::DEBUG_TAGS_HTML)), false, null, false, function () {return 'A';}
2121
))));
2222
}
2323
public function testOn_v() {

tests/helpers_for_test.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
<?php
22

3-
// Class for customized Runtime
4-
class MyLCRunClass extends \LightnCandy\Runtime {
5-
public static function raw($cx, $v, $ex = 0) {
6-
return '[[DEBUG:raw()=>' . var_export($v, true) . ']]';
7-
}
8-
}
9-
103
// Classes for inputs or helpers
114
class myClass {
125
function test() {

tests/regressionTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -631,16 +631,6 @@ public static function issueProvider()
631631
'expected' => 'OK!, bar'
632632
),
633633

634-
array(
635-
'id' => 150,
636-
'template' => '{{{.}}}',
637-
'data' => array('hello' => 'world'),
638-
'options' => array(
639-
'runtime' => 'MyLCRunClass',
640-
),
641-
'expected' => "[[DEBUG:raw()=>array (\n 'hello' => 'world',\n)]]",
642-
),
643-
644634
array(
645635
'id' => 153,
646636
'template' => '{{echo "test[]"}}',

0 commit comments

Comments
 (0)