Stack trace of form function inside twig template in Symfony - php

I have a problem that a variable gets rounded up to 3 decimals after the form function is run on the view object. Literally before form: 1.222666 after form(when rendered) 1.223
So basically I need to know what else is happening inside of form function to locate the code that does this rounding up, I was thinking of doing stack trace of it. But how can I achieve that?
Advice on any other way to find out what is happening inside of form function is more than welcome. Symfony used is 2.6.8
EDIT: To make things more clear.
The view in question is built inside of controller, when going through the steps of it there is no sign of any rounding or number format function. By doing dump() on every individual step I have found that only after rendering this change happens. However in twig file there is only
{{ form(productViewObject) }}
What I want to do is stack trace on all the actions happening inside of form, so I can locate the piece of code that does the rounding. I am not deeply familiar with Symfony but it seems that there is some kind of event or hook, something that is connected to this form function and performs the change. That is what I want to do.

You have to read documentation twig round:
{{ 42.55|round }}
{# outputs 43 #}
{{ 42.55|round(1, 'floor') }}
{# outputs 42.5 #}

Related

Symfony 5.4 Twig misinterpreted and displays {{

for some views, I have twig that bugs,
some variables are displayed with two braces before
like this one in my code {{ version }}
displays on the interface {{ 1.0
I'm using symfony 5.4 which runs on PHP7.4
does anyone have an idea of how to fix it?
Thanks
this happens only for a few variables
I just want to display a text in a variable like this {{ myvar }}
know that myvar is 'foo'
In the browser it in the browser it returns me {{foo

What is a double underline for in Twig or in Bolt?

I was just going through one of the Twig files in the Bolt default theme template, and I came across the following line of code:
{{ __("Unfortunately, no content could be found. Try another page, or go to the homepage.", {'%paths_root%': paths.root} ) }}
Now I thought the way you echo something in Twig is as follows:
{{ 'hello there' }}
I googled double underline and found this, but I’m not sure that's the answer to my question.
So what exactly is the double underline in Twig or Bolt?
In the current stable Bolt version, __() is a Twig function to call our translation layer that sits on top of Symfony's.
The second parameter in that function is the value in the string that is variable, and the value you want inserted in its place for that specific translation string, at that point.
I’m not sure if I understand this correctly, but in the Symfony framework for translations of strings you should use the translator service.
Then you could use the trans and transchoice Twig tags.
Please see Symfony documentation for more details:
Symfony translations

render(controller()) throws Fatal error: Call to a member function getRelativePath() on a non-object

I've call it in common template for render google plus signin button, and this error do not appear only on homepage http://mydomain.local/ but when I go to any another page like http://mydomain.local/somepage this error appear.
any ideas what is that can be?
{{ render(controller('MyNamespaceMyBundle:MyController:loginButton')) }}
public function loginButtonAction()
{
return $this->render('MyNamespaceMyBundle:MyController:button.html.twig',array(
'client_key' => $this->config['client_key']
));
}
that's it, what else code you need?
I had the same problem. I was trying to render a twig template from a page that was included in the sonata_page.ignore_route_patterns, embedding a controller:
{% render(controller('AcmeDemoBundle:Controller:action', {param1 : 'value1'})) %}
And got the same exception, Call to a member function getRelativePath() on a non-object.
Sonata\PageBundle\Twig\Extension\PageExtension's controller function is checking if it's defined a pathInfo attribute, if not, it's retrieved from the relative path of the site selector. Since my route was excluded from sonata_page, it couldn't. Hence the exception.
So basically, I just passed the required attribute and problem solved:
{% set pathInfo = path(app.request.attributes.get('_route'),
app.request.attributes.get('_route_params')) %}
{% render(controller('AcmeDemoBundle:Controller:action',
{param1 : 'value1', pathInfo: pathInfo})) %}
It's revealed in the comments that you are trying to utilize Sonata's Page Bundle. I found that others have experienced this issue. According to the discussion thread you have either:
Not configured a site
Configured the site with a "bad host"
Make sure you follow the steps outlined in the Getting started docs. Specifically, it seems you probably need to execute this command:
php app/console sonata:page:create-site

How to pass an array from Symfony 2 controller to a TWIG template ?

I can`t pass an array from a symfony 2 controller to a TWIG template. I use this code in the controller:
$searchTerms['color'] = "Red";
return $this->render('TestBundle::search.html.twig',
array(
"searchTerms" => $searchTerms));
In the twig template, I am trying to access the variable like this:
{{ searchTerms['color'] }}
{{ searchTerms.color }}
Both output nothing, empty string, so it seems like array comes to template but its elements are empty.
What`s wrong?
Yes, this code works. The first thing to check is that your twig code is in the correct page (TestBundle::search.html.twig). This might sound silly but that happens sometimes...
If this is all good, I suggest that you try to debug within your template. Debugging is the most important thing. You will always have this kind of problem while programming, especially when you try something new. The better you are at debugging your code, the better you are as a programmer because there is no way you can get everything right the first time.
So, how can you debug?
To debug within your twig template, you can use the debug extension of twig. To activate the debug option, you will have to do a quick change in your config file. You can also read this thread if your lost.
You can debug any variable within your template like this:
<pre>
{% debug searchTerms %}
</pre>
This way, you can easily debug your variable and test what your problem is:
{% debug searchTerms['color'] %}
If you want to debug things quickly, I highly recommend that you use the LadyBugBundle. It is an awesome tool that will allow you to do something like that:
In your controller:
ladybug_dump($searchTerms);
In your TWIG template:
{{ searchTerms|ladybug_dump }}
Not that different from a classic var_dump option, but if you have long arrays or objects, ladybug will impress you. More importantly, in a controller, you will often have the need to stop the code at a certain point to avoid the page to load after your debug statement, this is fairly easy with ladybug:
ladybug_dump_die($searchTerms);
You can even ask ladybug to load the "debugged" variable into Symfony profiler with this simple statement.
$this->get('ladybug')->log($searchTerms);
You have now direct access of the variable from a tab of the Symfony2 profiler.
Ladybug can do a lot more, but for this, the doc is really good.
I think you must change template like this:
{% for item in searchTerms %}
{{ item.color }}<br/>
{% endfor %}
See official documentation: Creating and using Templates->embedding-controllers

How to get part of the page from other controller

My site I have some content can be voted (+/-). It is working fine now, when all content has its own voter.
Now I'm looking for a way to create a single voting bundle with a entity(votedModel,votedId, user, vote).
Basically the bundle is ready. My problem is how to use it. I'd like to be able to do something like:
class ... extends Controller {
function showAction(Request $request,$id) {
...
$voter=new Voter('myCOntentType',$id,$userid);
...
return $this->render('...', array('voter'=>$voter->getVoter(),...))
}
}
getVoter() would create the voter view.
but I'm stacked how exactly start. I tried to call for the other controller in this manner, but can't create the voter form.
It worked with $voter=$this->forward('VoterbundleNewAction', array('id=>$id,'user'=>$user)->getContent();But this wasn't I had in mind.
I think my approach is all wrong and I may need to do this as service. I cant find my way around.
You can use include or render in your twig template to get other templates' output. So you could create a template (say, voter.html.twig) that contains the HTML for your voting system, and in the Twig, in any place where you need a voter, you could use:
{% include "AcmeVoterBundle:Voter:voter.html.twig" %}
or
{% render "AcmeVoterBundle:Voter:voter" with {"item": item} %}
In the first example, you simply include another template (see also: http://symfony.com/doc/current/book/templating.html#including-other-templates), in the latter situation you actually execute another action method of a controller and the output of that is put into your current template (see also: http://symfony.com/doc/current/book/templating.html#embedding-controllers)

Categories