In Symfony 2, I want to load 2 arbitrary files from the Base.html.twig
Base.html.twig
A.html.twig
B.html.twig
All the UI is written in Base and I must get some variables out of A and B.
With Twig, how can I load the two files so I can access their variables in Base.html.twig.
It would also be interesting to know how to {% set %} a variable as a property bag (A and B would add stuff to it)
The ->render call loaded Base.html.twig
To my knowledge you'll have to pass it through from the controller.
Otherwise if i misread this, if the A and B files need variables you don't already have, you're best bet would be to embed a controller.
http://symfony.com/doc/current/book/templating.html#embedding-controllers
Related
I'm a newbie and this is my first question so please be nice with me :)
In my Symfony 2 project I want to pass an $user object from my controller to the twig template. This object contains all relevant Information (like username, activepage ...) for rendering the view. My problem is that I would also like to put the result of mysqli database query inside the object. To be able to retrieve it in my twig template, I need two serialize the result object(array?) in the controller before passing. Unfortunately I dont know a way how to deserialize that object in twig( no twig filter available).
My questions:
Is this actually an elegant way or should i rather pass all objects in an array to the template?
Would it work to write a deserialize function inside the user class, which i can call in the twig template?
Will performance be ok?
How do the experienced people do this?
Thx for helping!
Built an array with your data in your controller and render it like this to your template :
return $this->render('yourbundle:example:index.html.twig', array(
'myArray' => $array,
Then in your template you have access to myArray like this :
{% for data in myArray %}
...
{% endfor %}
I made a service such a parameter bag which I can add parameters/values along the process, and render every parameters with twig so I can use them in javascript.
For that I had to use mostly JsonSerializable php interface (PHP 5.4+), and json_decode twig filter.
I think it is a nice way to pass variables to js like this because you can use this service when building your project.
I am using Symfony2 with the FOSUserBundle. I have created a method where I ask if the user has done a specific action, e.g. $user->hasVoted().
In my twig templates, I want to be able to ask
{% if user.hasVoted() %}
in every template (because it has an implication on which other partials to include), but I don't want to pass the whole $user object to each template.
Is there any way to make this user method accessible in all templates (or I could also pass the boolean value to all templates) without having to pass this explicitly to each template?
I know about injecting variables into all templates from the Symfony cookbook.
# app/config/config.yml
twig:
# ...
globals:
ga_tracking: UA-xxxxx-x
But the cookbook entry either tells how to inject static values (as in this example above), or how to inject services.
Is there an easy way how to include this one boolean value without having to define a service and inject it like this:
# app/config/config.yml
twig:
# ...
globals:
user_has_voted: "#acme_user.user_has_voted"
The authenticated user is available via app.user everywhere.
{% if app.user.hasVoted() %}
Or the cleaner way, you create a twig extension like is_granted. This enables you, to change the logic behind in future, without changing the templates.
Yes you can, in your bundle boot() method:
// src/Acme/DemoBundle/AcmeDemoBundle.php
public function boot()
{
$hasVoted = $this->container->get('security.context')->getToken()->getUser()->hasVoted();
$this->container->get('twig')->addGlobal('user_has_voted', $hasVoted);
}
Maybe you want to use the "attribute" twig function?
Something like that i guess :
{% if attribute(user, hasVoted) %}
Hope this helps!
(as the doc specifies it here)
How can I transfer variable to each twig template?
I need transfer rates to each template in my application.
You do that by using global variables.
How to Inject Variables into all Templates (i.e. Global Variables) (I know, searching the docs is hard...)
I currently am trying to make a variable using the current url for the view. To get the url, I am using {% set uri = app.request.uri %} (uri being my variable which is the current url for that particular view). The thing is, I am only interested in what makes the url unique (the end of it - a unique object from an array - happens to be a uri), and not the beginning (path to my application). I was thinking I could use a preg_replace to do so, but TWIG doesn't have this function. Just wondering if someone would know how to accomplish what I am trying to do?
I'm new to Symfony (and fairly new to PHP), so my explanations may not be clear (sorry).
Ex.
{% set uri = app.request.uri %}
output: http://website.com/http://item.org/1
I want to modify the uri variable to ONLY have http://item.org/1 (and not the path to my website).
I'm thinking creating a Twig Extension with the preg_replace will allow me to do this ..but not sure if it's the best way to go (inexperienced).
Overall goal:
The unique value for "uri" in the view is appended to the websites path by another view from an array of objects ($results) with attributes, one being "uri". My ultimate goal is to only display all associated attributes (or row) for an object in my $results array. I was thinking I could do this by first creating a key (my uri variable) in a foreach, and returning the row in the array which matches this key. This is why I am trying to create a variable with the url so that I can use it as a key for my foreach loop to iterate over $results. I am NOT using a database or Doctrine.
Thank you ahead of time for the help!
The best way is to move the logic from template to the controller.
If you need preg_replace in twig you must create custom extension.
Someone seems to have created a Twig Extension for preg_replace, see https://github.com/victor-in/Craft-TwigPCRE
You can do it like that. It's a bit ugly but it works.
uri|split('base_path')|join('')
I have a ParamterValue class which references ParamterDefinition class, by property ParamterValue->paramDef.
I created ParamterValueType to build a form.
How can I access ParamterValue->paramDef object in a template? I just need it for some logic in rendering a form, I actually don't need to render ParamterDefinition, that's why I don't include paramDef form field in ParamterValueType. Even if I would, how could I access underling object from the form view field?
So the general situation here looks like this: I have an object which I want to create from a form, that object has a reference to another object which has data that are needed to render the form (but I don't need a widget for it, just some data to perform logic). Do I need to include that referenced object in ParamterValueType to get access to it or not?
You can usually just do
{{ form.vars.data.paramDef }}
Actually, this only works as of Symfony 2.1.