I've started learning Twig today and wanted to know how I would redirect to another page in my template.
This is my index.php where I also load my homepage.
<?php
require '../vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('../recourses/views');
$twig = new Twig_Environment($loader);
echo $twig->render('pages/home.twig', array(
'project_title' => getProjectTitle()
));
Now my question is: How could I reach this page with an ?
I can't just use <a href ="pages/home.twig"> since it will show the code instead of the page itself.
I've tried searching but couldn't find my sollution.
Hope anyone can explain this to me.
If I unterstand, you are not using Symfony Standard Edition, but some Symfony components + Twig ?
In that case, Twig is only a a templating engine and Symfony functions are missing.
The path function (ShinDarth answer) is provided by a twig extension defined in the Symfony Standard Edition :
http://symfony.com/doc/current/reference/twig_reference.html#path
All the Symfony Standard Edition function are defined at the same page :
http://symfony.com/doc/current/reference/twig_reference.html
Using Twig like you do, you can only pass the path as variable and render it as {{ path_variable }}
Supposing that you have your route named my_route,
then via twig you simple do:
Link to my_route
Documentation: http://symfony.com/doc/current/book/templating.html#linking-to-pages
Related
I am using twig seperately, not from within Symfony.
I have base layout base.html. Other layouts extend that one and that seems to work fine.
If I have a controlleraction BlogPosts which passes an array of blogposts, I render the blog layout, which extend from the base layout to see that content. That works as intended
Now, I would like to pass variables to the base template, so it is visible on every page. How can I do this?
I found this article, but that mentions it for symfony. On its own, twig does not have the render function.
If you are using twig without symfony, I assume your project directory structure is similar like this:
project
- templates
- base.html.twig
- index.html.twig
- vendor
- index.php
So, your index.php code should be like:
require_once 'vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('templates', getcwd());
$twig = new Twig_Environment($loader);
echo $twig->render('index.html.twig', array('name' => 'Hello'));
So, if you want pass some variable to your base template, twig has method to add value for using globally
require_once 'vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('templates', getcwd());
$twig = new Twig_Environment($loader);
$twig->addGlobal('title', 'HomePage');
echo $twig->render('index.html.twig', array('name' => 'Hello'));
I hope this help
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.
i am currently working on a symfony project,
what i have:
app/Resources/views/mytemplate/
the folder mytemplate contains all of the important twig-views for my web app.
My question is, is there any possibility that third party members can create their own templates which override my "mytemplate" without creating controllers pointing to them ?
Like:
i have this template:
app/Resources/views/mytemplate/home/index.html.twig
An other person could create a new template in the same views directory like:
app/Resources/views/thirdparty/home/index.html.twig
to override my template.
is there any possibilty like this?
Greetings!
Well, to me, you have two possibilities :
The template that you want to be able to be redefined is the one specified with the method renderView() or similar in your controller : in this case, the possibilities are limitless. It's up to you to define the logic layer determining which template has to be rendered. You could for example force the user redefining the template to name it with a specific additional pattern, and then parse the right template to use thanks to a method inherited in all your controllers.
$content = $this->renderView(
$this->getInheritedTemplate('AcmeHelloBundle:Hello:index.html.twig'),
array('name' => $name)
);
The template that has to be redefined is one inherited in another twig template : In this case, it's almost the same. You could imagine writing your own Twig filter/function in order to retrieve the right template. The code should be very similar to the first case.
Hope this helped.
I am trying to insert a latest tweets "widget" on to a Symfony2 project. I have found an ideal script written in PHP that will do the job perfectly.
However, I don't know where the best place to put 3rd party PHP files in a Symfony2 project. I have placed them in the same folder as all my twig files reside, changed the name to read tweets.php.twig, and even located them in the web folder. When I try to include the file in the twig file that needs the Twitter feed it comes up with an error saying that it can't find the file.
Do I have the right idea, or do I have to convert the PHP in to a twig file or write the PHP script in to a controller?
I believe the recommended way would be to create a Symfony2 bundle that encapsulates all the logic for the tweet widget. You would then call your bundle controller and pass the response to your twig template.
If that is too complicated or you want something more quick and dirty - you can create a controller like TweetWidgetController.php and put the code into there as an action like widgetAction. Just make sure you return the tweet widget output in a Symfony response object.
Then from your main controller - you can do something like
$widget = $this->forward('YourBundle:TweetWidget:widget', array('twitterid' => 'yourtwitterid'));
return $this->render('YourBundle:yourtemplate.html.twig',array('widget' => $widget->getContent()));
Now in your twig template you can put it wherever you'd like by referencing it as:
{{ widget }}
I've just started looking into Twig and I'm wondering how I would accomplish the following.
I have a variable $logged_in that I need to have access to in every single page on my site, I was hoping that rather than passing this to the twig renderer every single time in the data array, there would be a way for me to declare this somewhere, and for every template to have access to it.
Do I need to build an extension to accomplish this / or is it even possible? I have looked through every page of the documentation but I'm having trouble having tried to extend the base template as described here...
Twig Documentation | Recipes | Making the Templates aware of the Context Dead link
Is this the right approach?
Thanks
Just read about the new features in the 1.0RC release which should help.
Taken from the blogpost:
Globals:
PHP
// a global can be a constant
$twig->addGlobal('pi', 3.14);
// or any other valid PHP expression, like an object
$twig->addGlobal('request', new Request());
Template
{{ pi }}
{{ request.params('name') }}