I have an ID of object in Symfony twig template. How can I transform this ID in the corresponding object? In EZ Publish there was a special FETCH function to do that, but in Symfony it does not exist. Thanks for any advice.
Twig is for the view only, your data must be returned by the controller, where you can find object by it's ID
I don't know if that is what you need, but in twig you can do
{{ render(controller('YourBundle:Controller:Action', {'objectId' : objectId})) }}
to render some other object/content.
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'm currently getting a very wired twig exception:
If I use the twig {% extends 'some:template' %}, I get the following twig exception:
An exception has been thrown during the rendering of a template ("The "_projectView" route has some missing mandatory parameters ("id").") in "xy:Project:view.html.twig".
But if I remove {% extends 'some:template' %}, the template is displayed correctly - this for my part rules out any problems with routing or the controller, it has to be a problem with the template but I can't figure it out. No variables are used in the parent templates.
Inside the template you're trying to extend you're trying to generate a url from a route '_projectView' but you don't provide all necessary parameters.
Either add a default id to your route ...
route_name:
pattern: /whatever/{id}
defaults: { id: 1 }
... or do something like this in your template:
{{ path('route', { 'id' : entity.id|default('1') }) }}
Quite simply, to display correctly, each twig needs to have all the variables it needs defined. They are either in the URL or passed into the render function. If any of these variables are missing, then Symfony will throw this error.
Given what you've said it seems to me that the twig you are extending has a variable in it that it wants. In all other places you have extended this twig, you have provided this variable already either in the URL or in one of the parent twigs and so it has not complained. In this case you try to use it and that variable is missing. So it complains.
Check to make sure your route passes in the right variable or that your controller passes the right variable into the render function.
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'm using symfony2 and have accessed an entity using a doctrine query, which I then pass to my twig template. This entity has variables that I would like to loop through without having to explicitly name each one. Is there a way I can do this with twig or should I try a different design?
Twig documentation says:
A sequence can be either an array or an object implementing the
Traversable interface.
So it should work as long as you implement one of the Traversable interfaces (IteratorAggregate or Iterator).
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.