3
votes


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')
3
Mustache. Huh. Is there a Tom Selleck option? - Jared Farrish

3 Answers

3
votes

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.

0
votes

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>
0
votes

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