Strange Symfony syntax - php

I've joined new project of website in Symfony3 framework and now I'm struggling an issue, trying to figure out this particular syntax in Twig:
<li>{{ 'site.template.menu.contact'|trans }}</li>
Arguments in path() twig function have name of route in my SiteController but i totally don't know what does code between <a/> tags, except 'trans' filter. I don't have any variables in my twig template file.
Have you seen something like this before? Where I should find information about this in docs or how to name syntax like this to find some information?

It is just the twig "internationalization" (often abbreviated i18n).
Docs for it are here.
The quotes around the object shouldn't be there. I'm assuming that an object called site is being passed to the view, so it should be {{ site.template.menu.contact|trans }}
To explain the dot notation in twig;
If your PHP array is something like;
$site['template']['menu']['contact'] = 'fubar';
If it is an object then it is just attributes of the object.

Related

symfony 3 url issue

Hi I'm coming for a very simple question (I think) but i didn't found the answer or a similar case.
I'm using symfony 3 and trying to build a second menu for my administration pannel.
However, I have a problem about how I have to declare the relative url in my "href", For my main menu i used to do like this
{{ url ( 'admin' ) }}
and it worked. The fact is that now I have subfolders and many levels in my url.
The url i try to reach is myapp/admin/gameadmin, this url work when I'm going on it but when i try to put it in 'href' I have an error message which says that the route is not working.
i declared it like that ->
{{ url(admin/gameadmin) }}
I tried with different character -> admin:gameadmin, admin\gameadmin ... etc and with path instead of url i don't know if it's not the good way to declare it or if I have a problem with my controllers.
In my bundle it's organised like that :
->Controllers(folder)
->admin(folder) (You can also find my main controllers on this level)
->admingamecontroller (Where the page I try to reach is routed)
I hope i gave you all the informations, thank you for your help and sorry for my english !
The url parameter is not the the url per se (ie: admin/gameadmin), this is the route name, defined in your routing.yaml file, or in your controller annotation.
If your action is something like this:
/**
* #Route("/admin/gameadmin", name="gameadmin")
*/
public function gameAdminAction()
{
...
}
Then, to generate the route, you have to do this:
{{ url('gameadmin') }}
By doing this, all the links on your website will be up to date if you change the gameadmin url, as long as you don't change the route name.
I suggest you to read this documentation on the Symfony website: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html
Edit: As pointed by user254319, if you're not using annotations, you'll have to edit your routing.yaml config file.
gameadmin:
path: /admin/gameadmin
controller: App\Controller\Admin\AdminGameController::gameadminAction
The route name is the yaml key: gameadmin.
Related Symfony documentation: https://symfony.com/doc/current/routing.html

Pass two parameters as one to TWIG url

I'm using a framework (specifically EC-Cube3, if interested) that uses a Silex application with TWIG templates.
It has a navigation sidebar that is configured with PHP object arrays. I can inject an item array that looks something like this:
$item = array(
"id" => "item_id"
"url" => "named_url"
);
The url is created in typical Silex fashion:
$app->match('/item', 'Controller::method')->bind('named_url');
The framework eventually uses the url field and evaluates it in TWIG like so:
{{ url(item.url) }}
And this is usually fine! But the problem comes when I have a URL that includes a parameter, like this:
$app->match('/item/{id}', 'Controller::method')->bind('named_url'); // Needs 'id' to be evaluated.
In this scenario, setting the URL by the same array won't work because TWIG's url function needs the id for the second argument.
Since I can't change the application's TWIG files (because then I can't safely update the application), I need to be able to set the item's url to include the id as well.
After investigating, I don't think this is possible. Is it true that this is impossible? Or is there some method that I'm unaware of?
You can use mutiple vars like this
{{ path('path_in_route', {'id': article.id,'slug': article.slug}) }}
And
path_in_route
must be in your router file.
Hope that helps you.
Can you try the following in your Twig file:
{{ url(item.url,{'id': id}) }}
I believe that should work, but not certain.
EDIT #2
What if you use something like this:
$app->match('/item', 'Controller::method($id)')->bind('named_url');
Where $id is the id you need to pass in. I'm not really familiar with Silex, but maybe it's something like that?

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

VueJS variable inside URL::action in Laravel

I am new to VueJS.
I have a link using Laravel Helpers:
{{ $tournament->id }}
As it will be inside a VueJS v-for, now I need to refer to Vue variables that I load with AJAX.
So, I tried to write it like that:
#{{ tournament.id }}
But I get
Parse error: syntax error, unexpected '{' (View: /home/vagrant/Code/resources/views/tournaments/index.blade.php)
Any Idea how to mix Laravel Helper and VueJS?
First of all it looks like you're missing quotes around #{{ tournament.id }}.
But after playing around with it myself it doesn't seem possible at the moment due to routes using the same brackets as placeholders.
A workaround would be to create a simple function somewhere that generates the url with a unique placeholder and afterwards you do a str_replace call on the generated url.
That's the thing I dislike in the helpers and I have completely stopped using them. IMHO the best way to go is to create a route for that (or a Route::controller) and map those urls to a controller. and then use something like:
#{{ tournament.id }}
Your routes could be something like:
Route::controller('tournament','TournamentController');
and your function inside the controller:
function getEdit($tournament_id){
// something something
}
And something like this would work

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