-
Notifications
You must be signed in to change notification settings - Fork 134
registerHelpers as external method #12
Comments
As a followup, the $h = new Handlebars_Engine;
$h->addHelper('_i18n', function($template, $context, $args, $source) {
$dictionary = array(
"Hello." => "Hola.",
"My name is {{ name }}." => "Me llamo {{ name }}.",
);
$source = array_key_exists($source, $dictionary) ? $dictionary[$source] : $source;
echo $source; // Only to show what the modified version should look like
$buffer = $template->render($context);
return $buffer;
});
$tpl = $h->loadTemplate('{{#_i18n}}Hello.{{/_i18n}} {{#_i18n}}My name is {{ name }}.{{/_i18n}}');
$tpl->render(array('name' => 'Josh')); The desired output from this would be |
@joshpangell it can be done with another engine instance like: $buffer = $template->getEngine()->render(array_key_exists($source, $dictionary) ? $dictionary[$source] : $source, $context); have a look at 600215d. but it would be better if we handle such situations without creating another engine instance. what do you think @fzerorubigd ? |
Modify the source, need to rebuild the Tokens array. |
Need some help in this. $h->addHelper('_i18n', function($template, $context, $args, $source) {
$dictionary = array(
"Hello." => "Hola.",
"My name is {{ name }}." => "Me llamo {{ name }}.",
);
$source = array_key_exists($source, $dictionary) ? $dictionary[$source] : $source;
return $source;
}); But there is a problem, which way is better : 1- should I change the return value? for example if Helper return a string the result is same as always, but (for example) return an array of string to tokenize every element in the array. |
@fzerorubigd I like the second idea (php example) because it's simple to use and simple to implement but what about performance? also what'll happen to cache I mean which one of these'll be cached?: Hello, Hola or _i18n('Hello")? |
1- Caching is done in _tokenize ,the Holla {{name}} is cached (in array form), and calling with another name, make use of that cache. |
Use this : $h->addHelper('_i18n', function($template, $context, $args, $source) {
$dictionary = array(
"Hello." => "Hola.",
"My name is {{ name }}." => "Me llamo {{ name }}.",
);
$source = array_key_exists($source, $dictionary) ? $dictionary[$source] : $source;
return new Handlebars_String($source);
}); |
Hey guys, thanks for looking into this. However, the outcome is not quite correct. Given the $h = new Handlebars_Engine;
$h->addHelper('_i18n', function($template, $context, $args, $source) {
$dictionary = array(
"Hello." => "Hola.",
"My name is {{ name }}." => "Me llamo {{ name }}.",
);
$source = array_key_exists($source, $dictionary) ? $dictionary[$source] : $source;
return new Handlebars_String($source);
});
$tpl = $h->loadTemplate('{{#_i18n}}Hello.{{/_i18n}} {{#_i18n}}My name is {{ name }}.{{/_i18n}}');
$tpl->render(array('name' => 'Josh')); The output from this is However, the solution that @everplays pointed out works like a charm. $h = new Handlebars_Engine;
$h->addHelper('_i18n', function($template, $context, $args, $source) {
$dictionary = array(
"Hello." => "Hola.",
"My name is {{ name }}." => "Me llamo {{ name }}.",
);
$buffer = $template->getEngine()->render(array_key_exists($source, $dictionary) ? $dictionary[$source] : $source, $context);
return $buffer;
});
$tpl = $h->loadTemplate('{{#_i18n}}Hello.{{/_i18n}} {{#_i18n}}My name is {{ name }}.{{/_i18n}}');
$tpl->render(array('name' => 'Josh')); Output: |
But I get the correct result with this code : require("vendor/autoload.php");
$h = new Handlebars_Engine;
$h->addHelper('_i18n', function($template, $context, $args, $source) {
$dictionary = array(
"Hello." => "Hola.",
"My name is {{ name }}." => "Me llamo {{ name }}.",
);
$source = array_key_exists($source, $dictionary) ? $dictionary[$source] : $source;
//$buffer = $template->render($context);
return new Handlebars_String($source);
});
$tpl = $h->loadTemplate('{{#_i18n}}Hello.{{/_i18n}} {{!comment }} {{#_i18n}}My name is {{ name }}.{{/_i18n}}');
echo $tpl->render(array('name' => 'Josh')); Output is :
-- I think you should update your copy, with composer or whatever you using. |
Hey guys,
I see that there is a helper registration method, but it appears to only be an internal method as of yet. What extending this so that it could be used externally?
Here is an example usage with javascript: http://jsfiddle.net/teklocust/DA5Y6/1/
The text was updated successfully, but these errors were encountered: