I am experimenting with kostache, "mustache for kohana framework".
Is there any way I can use simple PHP functions in mustache template files.
I know logic and therefore methods are against logic-less design principle, but I'm talking about very simple functionality.
For example:
gettext('some text') or __('some text')
get the base url; in kohana -> Url::site('controller/action')
Bobthecow is working on an experimental feature that will allow you to call a function as a callback.
Check out the higher-order-sections branch of the repository and the ticket to go with it.
You could use "ICanHaz" http://icanhazjs.com/
and then you can declare your mustache templates as
<script id="welcome" type="text/html">
<p>Welcome, {{<?php echo __('some text') ?>}}! </p>
</script>
Well, you can do this now with Bobthecow's implementation of Mustache Engine. We need anonymous functions here, which are passed to the Template Object along with other data.
Have a look at the following example:
<?php
$mustache = new Mustache_Engine;
# setting data for our template
$template_data = [
'fullname' => 'HULK',
'bold_it' => function($text){
return "<b>{$text}</b>";
}
];
# preparing and outputting
echo $mustache->render("{{#bold_it}}{{fullname}}{{/bold_it}} !", $template_data);
In the above example, 'bold_it' points to our function which is pasalong withwith other data to our template. The value of 'fullname' is being passed as a parameter to this function.
Please note that passing parameters is not mandatory in Mustache. You can even call the php function wothout any parameters, as follows:
<?php
# setting data for our template
$template_data = [
'my_name' => function(){
return 'Joe';
}
];
# preparing and outputting
echo $mustache->render("{{my_name}} is a great guy!", $template_data); # outputs: Joe is a great guy!
Credits: http://dwellupper.io/post/24/calling-php-functions-for-data-in-mustache-php
Related
Can I add new nodes to mustache template at run-time in PHP? Let's say below is a code where ProductDetails will contain few single products:
{{#ProductDetails}}
{{#SingleProduct}}
{{OldDetail}}
{{/SingleProduct}}
{{/ProductDetails}}
I want to add a new node like {{NewDetail}} just after {{OldDetail}} through some function in run-time(i.e just before I am compiling the template as these templates have been shipped to customers in such a way that only code to compile can be changed but not the template)? I don't want to do string manipulation(customers created few new templates with above parameters present at least but the spacing may change & few new entries can be added by them around nodes). Does mustache library provide any functions for that?
If I were you, I will try Lambdas.
Template.mustache will be like this:
{{#ProductDetails}}
{{#SingleProduct}}
{{OldDetail}}
{{/SingleProduct}}
{{/ProductDetails}}
And codes will be like:
$Mustache = new Mustache_Engine(['loader' => new Mustache_Loader_FilesystemLoader($YOUR_TEMPLATE_FOLDER),]);
$Template = $Mustache->loadTemplate('Template');
$OriginalOldDetail = '<h1>this is old detail</h1>';
echo $Template->Render([
'ProductDetails' => true,
'SingleProduct' => true,
'OldDetail' => function($text, Mustache_LambdaHelper $helper){
// Render your original view first.
$result = $helper->render($text);
// Now you got your oldDetail, let's make your new detail.
#do somthing and get $NewDetail;
$NewDetail = $YourStuff;
// If your NewDetail is some mustache format content and need to be render first.
$result .= $help->render($NewDetail);
// If is some content which not need to be render ? just apend on it.
$result .= $NewDetail;
return $result;
}
]);
Hope that will help.
(English is not my first language so hope you can understand what I'm talking about.)
I want to implement localization. Once I get the language code, for example, en, what would be the best way to retrieve the text? For example:
en:
welcome-text = "Hello!"
es:
welcome-text = "Hola!"
also, what would be the most efficient and the fastest way? should i store them with php variables and get the text via php variables in an array or switch statement OR should i store them as json and get them via json?
If you are not using some framework (most of them have translation functions and I really suggest you use one) I suggest you arrays as your translation files. It is a simple and fast way:
en.php
<?php return array(
'Welcome' => 'Welcome',
...
);
es.php
<?php return array(
'Welcome' => 'Holla',
...
);
lang.php
<?php
$mapping = require($_SESSION['user-lang-code'].'.php');
funciton _t($text)
{
global $mapping;
return isset($mapping[$text])?$mapping[$text]:$text;
}
Your page:
<?php
require 'lang.php';
echo _t('Welcome');
I'm trying to figure out a way that I can pass PHP code directly within the templates in TWIG without having to create separate extensions for each function. Basically a simple function that could parse out the PHP and run the code in the template.
Example:
$function = new Twig_SimpleFunction('php_parse_function', function () {
//parse php code here
});
$twig->addFunction($function);
Use-case Example:
{{ php_parse_function | php code }}
The problem with doing something like this is that I would have to include an entire code-block in a string, which is to print html encapsulated in another string, which will have class/other-attributes in another layer of quotes.
Example:
{{php_parse_function | "echo '<section class=\'Yo\' id=\'2\'>'</section>" }}
So is there a workaround for something like this?
EDIT (after question edit)
No, It's not possible to execute php code directly from Twig. You can create filters or functions and pass strings as arg.
How? This way (orginal answer):
Anywhere inside the controller you're loading Twig:
// ...
$twig = new Twig_Environment($loader, $params); // load Twig env
$tsf = new Twig_SimpleFunction('fooTwig', function ($str) {
return eval($str);
});
$twig->addFunction($tsf);
// ...
Then:
{{ fooTwig('echo "Hello, World! What kind of sorcery is this? F in hex is "; echo hexdec("F");') }}
And remember you can use filters too!
Controller:
// ...
$twig = new Twig_Environment($loader, $params); // load Twig env
$tsf = new Twig_SimpleFilter('fooTwig', function ($str, $fn) {
$args = array_merge(array($str), array_slice(func_get_args(), 2));
return call_user_func_array($fn, $args);
});
$twig->addFilter($tsf);
// ...
Then:
{{ 'F'|fooTwig('hexdec') }}
If you are using Symfony2 or any other framework, is the same logic. You should just investigate where to add the Twig simple function/filter.
I have just started working with Mustache template engine. I am currently using PHP implementation of it (https://github.com/bobthecow/mustache.php/wiki). I am using helpers to manipulate the way data is rendered.
$data = array("name" => "abhilash");
$template = "Hello {{name}}, {{#bold}}Welcome{{/bold}}";
$m = new Mustache_Engine(array(
"helpers" => array(
"bold" => function($content) {
return "<b>$content</b>";
})));
$html = $m->render($template, $data);
With the help of this I am able to render 'Welcome' with bold font. I would like to know if it is possible to manipulate $data with the help of helper function. For example if the template is like below and I have a helper function registered as dataSource, I would like to use it to collect some data (say key-value pair) from datasource_func_name() and append it to $data.
{{#dataSource}}datasource_func_name{{/dataSource}}
Hi {{name}}
That's normally not how you would use helpers. However, Mustache basically expects a data souce, so why not inject it directly?
$html = $m->render($template, $dataSource);
I created element in form object:
function createElement()
{
$template = new Zend_Form_Element_Hidden('field');
$template->addDecorator('ViewScript', array('placement' => 'prepend', 'viewModule' => 'admin', 'viewScript' => 'values.phtml'))
$this->addElement($template);
}
function setViewTemplate($values)
{
$view = new Zend_View();
$view->setScriptPath(APPLICATION_PATH . '/scripts/');
$view->assign('values', $values);
$this->getElement('field')->setView($view);
}
But in the view script 'values.phtml' I cannot get access to values like $this->values.
What I'm doing wrong here?
I know that it would be good to add own decorator, but it is interesting to use zends' decorators.
From the Zend Framework Documentation: Standard Form Decorators Shipped With Zend Framework Section
Zend_Form_Decorator_ViewScript
Additionally, all options passed to
the decorator via setOptions() that
are not used internally (such as
placement, separator, etc.) are passed
to the view script as view variables.
function setViewTemplate($values)
{
$this->getElement('field')
->getDecorator('ViewScript')
->setOptions('values', $values);
}
you can reslove it with using attribs
$template->setAttrib('key', 'value');
and in template
<?php echo $this->element->getAttrib('key'); ?>