how to call codeigniter helpers or php functions in mustache.php - php

hi i am working on mustache.php with codeigniter it is parsing mustache tag really nice now how can i use CI helpers or php functions with in mustache tags like
{{ anchor("http://www.google.com","Google") }}
//php function
{{ date() }}
i have tried mustache helpers but no luck as per this post github mustache
i this case i have to add extra opening and closing mustache tags. which i don't want just passing function in tags and get output.

You cannot call functions directly in your Mustache template (logic-less templates, remember?)
{{ link }}
{{ today }}
Instead, this functionality belongs in your rendering context, or your ViewModel. At a minimum, this means preparing your data in advance:
<?php
$data = array(
'link' => anchor('http://www.google.com', 'Google'),
'today' => date(),
);
$mustache->loadTemplate('my-template')->render($data);
An even better approach is to encapsulate all of the logic required for my-template.mustache in a ViewModel class, lets call it MyTemplate:
<?php
class MyTemplate {
public function today() {
return date();
}
public function link() {
return anchor('http://www.google.com', 'Google');
}
}
$mustache->loadTemplate('my-template')->render(new MyTemplate);

Related

Symfony Twig {{render}} handling passed arguments

Over a template I do a render method:
{{ render(controller('AppBundle:Widgets:myCapsWidget'),{'somestring':someString}) }}
Over the controller I have the following method:
public function myCapsWidgetAction($somestring)
{
return new Response(strtoupper($somestring));
}
I also looked over theese links:
symfony twig render controller argument array
https://knpuniversity.com/screencast/symfony2-ep3/render-controller
But still cannot find any light to my path.
If I read the first link you gave us, you should use:
{{ render(controller('AppBundle:Widgets:myCapsWidget',{'somestring':someString})) }}

WP Masterslider PHP shortcode in Timber/Twig?

I have <?php masterslider(1); ?> and I need that to render in a .twig file, can anyone let me know the formatting for timber to get this function to work.
With Timber you can call functions in your Twig files:
{{ function('function_to_call', 'param_1', 'param_2', '…' ) }}
The first argument will be the name of the function to call, the following arguments will be all the parameters you want/need to pass to that function.
You could translate your function <?php masterslider(1); ?> to {{ function('masterslider', 1) }} if you want to use it in a Twig file.
To learn more about calling functions in Twig when using Timber, refer to the the Timber documentation on functions.

Twig include with dynamic data

I'm writing a website using Slim Framework and Twig.
The sidebar on the website will have dynamically created links and i want to get the links from the database/cache every time a page is rendered, i can do this in each controller action but i would like to do this in my base template so i don't have to get and render the data manually in every controller action.
I have looked through the twig documentation and I haven't seen anything that could be of use besides the include function/tag but i don't see how i could get the data easily.
Is my only option to write an twig extension to do this or is there an easier way?
First of all: templates usually shouldn't contain any type of bussines logic but only render the data you provide it.
However you could write a custom twig function and use that to aquire the menu data from the DB in order to render it in your base template.
Alternativeley you could write some Slim middleware that aquires the data which might be able to inject the data into the template.
Hope that helps.
After reading Anticoms answer i was able to do it by writing a Twig extension.
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('render', array($this, 'render'))
);
}
public function render($template, $controller, $action) {
$app = Slim::getInstance();
return $app->$controller->$action($template);
}
Now i can simply write
{{ render('Shared/sidebar.twig', 'Controller', 'Index') }}
Into my twig template to render the template with the data i want.
Note that my render() function uses slim dependency injection to instanciate the controller at runtime.

Laravel develop Blade template Engine

i want to use custom code in blade template engine such as custom function for use into that
my function:
public function viewCurrentDate( $arg=0 ){
return 'date is'.date('y');
}
in Blade template use like with this code
#< viewCurrentDate >#
how to develop blade for custom actions?
Add in the filters.php file
Blade::extend(function ($view) {
return str_replace("#dateY", 'date is'.date('y'), $view);
});
in template :
<h1>#dateY</h1>
Full tutorial about blade extension here : http://blog.zerilliworks.net/blog/2013/04/03/blade-extensions-in-laravel/
This is possible (see markcial's answer) but I think there is a much better way to do it.
Why not just set up a HTML helper class.
class HTMLHelper {
public static function viewCurrentDate($arg = 0){
return 'date is'.date('y');
}
}
You can then add this to echo the date in your blade file:
{{ HTMLHelper::viewCurrentDate() }}
It is slightly more characters (compared to what you wanted), but will be so much more flexible for you as you can add as many helper methods to your class as you like and use them anywhere, not just in blade.
Edit: Whilst markcial's answer is what you are after (tells you how to create a new template string in blade) I'd don't think that is the simplest way to do things. The helper file is much more flexible and reusable. For example, you can use this helper file ANYWHERE in your app. With markcial's answer, you are only allowing blade to use what you have written. To me, it doesn't seem worth it when a more flexible, easier solutions is available.

How use php "use" function in twig

In php I can use in my template:
use My\WebBundle\Classes\Util;
How can I put the same in Twig tempate?
Thanks.
You can't create objects in twig template, but you can pass them in twig template from controller:
public function postsAction() {
return $this->render('AcmeBlogBundle:User:posts.html.twig', array(
'util' => new My\WebBundle\Classes\Util(),
));
}
And in twig template can use it:
{{ util.anyProperty }}
You don't want PHP in your twig template. All you need to do is tell twig what variables you use. It's part of the MVC.
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Categories