How use php "use" function in twig - php

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

Related

How to assign variables in twig?

Im migrating my code from smarty to twig
but I dont seem to find how to do this in twig
smarty:
$smarty->assign('Config',$Core->settings);
how can I do this in Twig ?
You would use 'set'. http://twig.sensiolabs.org/doc/tags/set.html
Then you would have to pass in $Core->settings as a variable from your controller like so:
return $this->render('my_template.html.twig', array(
'core_settings' => $Core->settings,
));
Then in my_template.html.twig you would use set like this:
{% set 'Config' = core_settings %}
I'm not sure if that works for you, Twig is different than smarty (I'm not very familar with smarty) but Twig is powerful.

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.

Laravel and Twig Bridge printing Auth object

I'm using Laravel basic authentication but I am really struggling to use the Auth object in the views.
There are good examples using Blade, but I am using Twig (twig bridge) and cannot solve this.
I can vardump(Auth::user()->name) in my controller in regular php but how do I get the same in the twig file (my view file)?
How do I do something like;
{% if auth.guest %}
Or;
{{ Auth.user().name }}
I've tried so many different ways but just get nothing.
This has to do with the way twig works, accessing static variables /classes or static methods on classes is impossible without adding a function to the Twig Environment.
You can register a function like so:
$twig = new Twig_Environment($loader);
$twig->addFunction('staticCall', new Twig_Function_Function('staticCall'));
function staticCall($class, $function, $args = array())
{
if (class_exists($class) && method_exists($class, $function))
return call_user_func_array(array($class, $function), $args);
return null;
}
Then i think you should be able to do:
{% if staticCall('Auth', 'guest') %}
Hi there stranger, why don't you login?
{% endif %}
I took the code from this answer: twig template engine, using a static function or variable

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.

how to call codeigniter helpers or php functions in mustache.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);

Categories