Symfony 5.4 Twig misinterpreted and displays {{ - php

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

Related

How to pass a subview to a master view in Laravel?

I have a master view and depending on the URL and controller, it will load in another subview to a variable called $content, that's the idea.
Currently I am trying with:
return view("master")->with(["content" => view("pages.group")]);
So for example, if the URL is https://example.com/group/1 I am trying to get the subview included on my master template. Currently, it just gets escaped for XSS but I feel like this isn't the right way to do this?
I assume you are trying to display the sub-view content in the follwing way:
{{ $content }}
Change your syntax from {{ }} (Escaped output) to {!! !!} (Non escaped output).
{!! $content !!}
In the end after #lagbox mentioned it, using Laravel sections enabled me to use a master view and extend it when needed. https://laravel.com/docs/6.x/blade#extending-a-layout

Laravel blade "{{ command }}" not working correctly

I have this weird problem with my Laravel 5.5 version... I created the auth views using
php artisan make:auth
This command created the views controllers and everything I need to lets get stared to work. But I'm having this visualization problem
As you can see on the register view I have this problem.
The real thing is that "{{ any_command }}" is printing the code that its supose to generate instead of interprating like part of the code. But if I use {!! any_command !!} instead it seems to work propertly. What can happend to my laravel is screwed up. It has nothing to be with the artisan auth method, because I tried to create a new form (using laravel collective form helper) and get the same result.
{{ }} will escape all data before printing. So, if you write any HTML tag inside, it will be escaped and printed as is. Just like your example.
{!! !!} will print unescaped data. This will print the tags correctly, but you have to take care where you use it, because someone could inject unwanted data there.
So, in your case, you should use {!! !!}.
Please, refer to this question: What is the difference between {{ }} and {!! !!} in laravel blade files?

Stack trace of form function inside twig template in Symfony

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 #}

Laravel routing - Variables show only curly braces

I will try to be as descriptive as possible, as I haven't seen this specific problem anywhere on the Internet.
I have been unsuccessful in getting routing to work on this server without use of the public folder. Only /public/index.php is capable of displaying any sort of content, and it has been static for me.
On the tutorial I was going through (http://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers) he was easily able to use his code to get variables to display. When I look at my live site, I end up with static data that looks like this:
{{ $value->id }} {{ $value->name }} {{ $value->email }} {{ $value->nerd_level }}
All of my files are the exact same as his. My app/config/database.php also has proper credentials.
Apple Server 10.9.4; PHP 5.4.24; Laravel 4.2.8; mysql 5.6.20;

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

Categories