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.
Related
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})) }}
I'm trying to call a controller from a twig extention.
I do not want to call the controller as a service since I'd lose all the fancy shortcuts.
Want I want to achieve is to call the controller like twig do it when you do :
{{ render(controller(
'AppBundle:Article:recentArticles',
{ 'max': 3 }
)) }}
I looked at the sourcecode of the "render" and tried to find the "controller" twig's functions, but I did not managed to understand how to do.
From now I achieved an unsatisfying but functionnal code :
In my twig extention :
return $environment->render('FooBundle:TwigExtension/CmsExtension:cmsRenderHook.html.twig', [
'hook' => $hook,
]);
In the CmsExtension:cmsRenderHook.html.twig template :
{{ render(controller(hook.stringControllerAction, hook.arrayParameters)) }}
I think (maybe wrongly) that it would be faster to call it without the template step.
EDIT : I finally successed to code this :
$environment->getExtension('Symfony\Bridge\Twig\Extension\HttpKernelExtension')->renderFragment(
$environment->getExtension('Symfony\Bridge\Twig\Extension\HttpKernelExtension')->controller(
$hook['action'],
$hook['jsonParameters']
)
);
(I did a grep in twig's cache and reproduced it compiled version).
my only concern is about referring to Symfony\Bridge\Twig\Extension\HttpKernelExtension, i'd rather let twig handle this choice, but I can't find out how.
I have two questions:
- do you think that Symfony\Bridge\Twig\Extension\HttpKernelExtension is stable enought to refere explicitly to it?
- if not how would you do to let twig handle it?
You could also get the Twig_SimpleFunction from the Twig_Environment:
$renderFunction = $environment->getFunction('render'); // get the env via initRuntime(..) in your extension
$callable = $renderFunction->getCallable();
However, I would not recommend relying on Twig internals. You should probably extract the functionality into a service.
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
Twig's documentation for tag include looks very similar to that of function include.
Tag include:
{% include 'header.html' %}
Function include:
{{ include('template.html') }}
Can somebody point out in what circumstances, one is preferred over the other? Thanks!
{{ include() }} Was introduce in Symfony 2.2:
Using a function allows you to do whatever you want with the output (which is not possible with a tag), like a simple:
{{ set content = include('some_template') }}
But as Fabien Potentier (twig founder) said
the function and the tag does indeed the exact same thing
You can find the discution about it's introduction here: https://github.com/twigphp/Twig/pull/926
Twig 3.x documentation for the include tag now states:
It is recommended to use the include function instead as it provides the same features with a bit more flexibility:
The include function is semantically more “correct” (including a template outputs its rendered contents in the current scope; a tag should not display anything);
The include function is more “composable”:
The include function does not impose any specific order for arguments thanks to named arguments.
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);