Symfony4 translation in twig - php

As for Symfony4 translation, thanks to this article. It works well in Controller.
public function index(TranslatorInterface $translator)
{
$translated = $translator->trans('test');// it works
print $translated;exit;
in messages.en.yaml
test: englishtest
However I can't translate message in twig.
<br>
{{ test|trans }}
<br>
It shows the error Variable "test" does not exist.
I need to do something in advance for translation in twig???

The method signature looks like this:
{{ message|trans(arguments = [], domain = null, locale = null) }}
See https://symfony.com/doc/current/reference/twig_reference.html#trans
So if test is not a variable, then {{ 'test'|trans }} should work (as zalex already pointed out).

Related

How can I set the route parameter name using a variable in Twig?

I am trying to use the url Twig function with Silex to generate a route, but when I use the variable name that I have passed to the template it generates a warning that I have not supplied the parameter.
This is the array I am passing to the template:
[
"total_pages" => $pages,
"current_page" => $page,
"route_name" => "gallery_album",
"route_parameter" => "groupname",
"route_value" => $groupname
]
And in the template I am trying to use:
{{ url(route_name, {route_parameter: route_value, 'page': page} ) }}
(The page variable value is worked out in the template)
This is part of a pagination template that I am building so I need the parameter to be a variable so it can be applied to different pages. This is the error I get when I run this:
I feel this is something that is very simple, I am just missing something fundamental.
It thinks that route_parameter is a string key name and not a variable:
You can do for example:
{% set params = {'page': page, (route_parameter): route_value } %}
{{ url(route_name, params) }}
You can use {{ app->path}} or {{ app->url }}
if you using Silex\Application\UrlGeneratorTrait in you Application class
or alternative using this
{{ app.url_generator.generate('homepage') }}

SlimPHP v3 how to display flash message on view

In their new documentation there isn't anything for flash messages.
I installed the flash extension from their github repository (slimphp/Slim-Flash). Everything works fine, I can add messages and can also get these messages.
// Adding a message
$this->flash->addMessage('test', 'This is a message');
// Getting a message
$this->flash->getMessage('test')[0];
But this only works inside routes. Of course I want to have these messages displayed on my view.
But I just don't know how to get this message on the twig view.
I have already tried:
{{ container.flash.message('test')[0] }}
{{ container.flash.getMessage('test')[0] }}
{{ this.flash.message('test')[0] }}
{{ this.flash.getMessage('test')[0] }}
{{ flash.message('test')[0] }}
{{ flash.getMessage('test')[0] }}
{{ app.flash.message('test')[0] }}
{{ app.flash.getMessage('test')[0] }}
{{ container.flash.test }}
Thanks for help!
You can add the flash message inside the data attribute from the render method:
$this->view->render($res, 'path/to/template.twig', [
'flash' => $this->flash
]);
Or you could add a middleware and add your flash instance to the twig parameters
$app->add(function ($request, $response, $next) {
$this->view->offsetSet("flash", $this->flash);
return $next($request, $response);
});
then it should be possible to access the messages inside the twig template with
{{ flash.getMessage('test') }}

How do I use the POST of Laravel's Route::resource?

Below is my code for a Laravel 4 project.
Going to the authors/create URL and submitting the form gives me a 405 error.
However, if I prepend the routes.php file with Route::post('authors/store', 'AuthorsController#store');, basically doubling what it already should do, everything works like a charm!
Why do I need do prepend said line in my code to work? I can only assume I'm doing something wrong here.
routes.php:
Route::resource('authors', 'AuthorsController');
AuthorsController.php:
public function create() {
$view = View::make('authors.create');
return $view;
}
public function store() {
//
}
authors/create.twig:
{{ form_open({'url':'authors/store'},{"method" : "post"}) }}
<p>
{{ form_label("Name", "name") }}
{{ form_text("name") }}
</p>
<p>
{{ form_submit("Add Author") }}
</p>
{{ form_close() }}
The store action get's trigger when you POST to the resource. So just authors and not authors/store:
{{ form_open({'url':'authors'},{"method" : "post"}) }}
See this table on more information what URL corresponds to what controller action.
Also I think it should be like this:
{{ form_open({'url':'authors', 'method' : 'post'}) }}
And you can pass the route name Laravel automatically generates to make your life a bit easier:
{{ form_open({'route':'authors.store', 'method' : 'post'}) }}
Oh and one more, post is the default method so this should do as well:
{{ form_open({'route':'authors.store'}) }}

Laravel form sort of submitting in debug mode but doesn't work in normal mode

so I have a selection box that gives a dropdown menu to give messages a manager from the dropdown. It takes the input and then changes to a column in the database called manager for it's respective column. When I try to submit the selection menu it gives me the regular error for Laravel. But then when I put ?debug=1 at the end it submits but gives the row's manager column a value of just blank.
Here is what I have in the routes.php
Route::get('foo/{id}', 'fooController#bar');
Route::post('foo/{id}', 'fooController#bar');
This is the form.
{{ Form::open(array('url' => '/admin/foo' . $message->id)) }}
{{ Form::select('handler[]', array('unassigned', 'foo', 'bar'), null, array('style' => 'width: 127px')); }}
{{ Form::submit('Change manager') }}
{{ Form::close() }}
{{ $message->manager }}
and here is what is in the fooController
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler;
$message->save();
return Redirect::action('AdminController#foo_bar');
}
I had a problem like this the other day, I have zero recollection of what I did. I really appreciate any help, thanks! The database is postgresql if that's any help
Try a dd(Input::all()) at the beginning of your controller and make sure you're seeing what you expect.
Also since you're sending an array perhaps you have to do Input::get('handler.0') -- see here right below the Input::only() and Input::except() code block.
It would seem as though because you are naming your select handler[], PHP is grabbing it as part of an array.
When setting up your message model, try this...
public function bar($id = null)
{
$message = Message::find($id);
$handler = Input::get('handler[]');
$message->manager = $handler[0];
$message->save();
return Redirect::action('AdminController#foo_bar');
}
Usually, you'd only use names in your forms post-fixed with [] when you are accepting multiple values like checkboxes/multi-selects etc... Otherwise, it's probably best to stick with not using it because it may cause confusion.
I managed to fix it in a almost frustratingly simple way by just changing the method to PUT.
like this
Form::open(array('url' => 'foo/bar', 'method' => 'put'))

Blade helper, get variable from view without parameter

maybe someone know, how can I get variable with name "foo" from blade template get to static function which is used in this template.
For example I have:
<div class="collumns large-8">
{{ Helpers::setLetters('variable_name') }}
</div>
<div class="collumns large-8">
{{ $variable_name }}
</div>
Both divs should set same string because in setLetters function will be
return ${$name};
But, that should be instead $name, because in case about, will be error.
I use laravel 4.1
This doesn't go in your View, it should be in your controller.
Controller
$name = Helpers::setLetters('variable_name');
return \View::make('yourview', array( 'variable_name' => $name);

Categories