I would like to know how to apply filters with Routing.generate().
In my template i can do the following:
var remotePathPlayerSearch = "{{ path('_api_get_players_search', {searchterm: '%QUERY', limit: 5}) |url_decode|raw }}";
The equivalent in JS is
Routing.generate('_api_get_players_search', {'searchterm':'%QUERY', 'limit': 5}, true);
Now how can i apply url_decode and raw to the url generation?
You can't. Using FOSJsRoutingBundle the URL is generated via JavaScript on client machine, not on the server.
Related
I'm trying to migrate an existing application from Symfony 4 to Symfony 5 with Stimulus.
One thing I'm trying to migrate is datatable initialisation which are handled by JS.
So I've created a stimulus controller that import datatable scripts and use the connect function to initialise the table.
It's working fine but I need to use the translator to localise some part of the datatable options.
In the previous version, the JS code embedded in twig template was looking at that :
const paramsTable = {
search: {
caseInsensitive: true
},
language: {
search: '',
zeroRecords: '{{ "datatable.label.noentries" | trans }}',
searchPlaceholder: '{{ "pholder.search" | trans }}',
sInfoEmpty:'{{ "datatable.label.noentries" | trans }}',
},
}
So I was using Twig filter to translate some labels.
So I'm wondering how I can do the same in a Stimulus Controller without having to pass all the translations from twig to Controller by Values ?
I'm trying to add custom domain into the project.
I have regions.locale.yaml file.
I'm trying load it in twig:
{{'united.kingdom'|trans|raw}}
But this doesn't work.
I think it has to be somehow declared that this file exists.
I found this in documentation:
// ...
$translator->addLoader('xlf', new XliffFileLoader());
$translator->addResource('xlf', 'messages.fr.xlf', 'fr_FR');
$translator->addResource('xlf', 'admin.fr.xlf', 'fr_FR', 'admin');
$translator->addResource(
'xlf',
'navigation.fr.xlf',
'fr_FR',
'navigation'
);
But where should I put this to declare my regions.locale.yaml files globally?
Thanks
If you are using Symfony Standard, you don't have to declare your translation files, you just put them in app/Resources/translations.
The key is that when you want to translate using your custom domain, you just specify your domain, like this :
{{'united.kingdom'|trans({}, 'regions')|raw }}
or somewhere else in your code :
$translator->trans('united.kingdom', [], 'regions');
I got error
No route found for "POST
/module/getinfo/0/0/1454306400000/1455256800000"
The code on index.html.twig:
var desde_=1454306400000;
var hasta_=1455256800000;
var url = "{{ path('module_getinfo') }}"+desde_+"/"+hasta_
to get something like this:
url = /module/getinfo/1454306400000/1455256800000
the routing.yml is:
module_getinfo:
pattern: /getinfo/{desde}/{hasta}/
defaults: { _controller: AcmeDemoBundle:Module/Module:getInfo,desde:0,hasta:0}
I want to create a custom variable on javascript, what can I do ?
Thank you !
PD. Sorry for my english, I'm still learning jejeje
If you don't pass the values of the route placeholders to the path() function, it'll use the default values (both set to 0).
If you can't pass the values, because they are only available in JavaScript, consider using string replacing techniques:
var url = "{{ path('module_getinfo', { desde: '%desde%', hasta: '%hasta%' }) }}"
.replace('%desde%', desde_)
.replace('%hasta%', hasta_)
;
A simple solution is to use what #wouter J said
But a cleaner solution is to use something like fos js routing which allows you to generate the routes from java script
I would like to use the handlebars-helpers node module with my handlebars templates. I'm using hapi as my framework which supports handlebars. I haven't found any documentation or examples that shows how to use handlebars-helpers with hapi using handlebars as the view engine.
Is it possible and if so, what is the solution?
after start hapi server
// hapi v17
try {
await server.start();
} catch (err) {
throw err;
}
//......
// add some handlebars helpers
let hbs = server.realm.plugins.vision.manager._engines.hbs;
// console.log('handlebars_helpers', handlebars_helpers);
if (handlebars_helpers) {
for (let key in handlebars_helpers) {
if (key) {
// console.log('key', key, helpers[key]);
hbs.module.helpers[key] = handlebars_helpers[key];
}
}
}
//check your helper is registered
// console.log('hbs.module.helpers', hbs.module.helpers);
// add some handlebars helpers
I don't think it's currently possible according to hapijs api docs for views:
http://hapijs.com/api#serverviewsoptions
helpersPath - the directory path where helpers are located. Helpers are functions used within templates to perform transformations and other data manipulations using the template context or other inputs. Each '.js' file in the helpers directory is loaded and the file name is used as the helper name. The files must export a single method with the signature function(context) and return a string. Sub-folders are not supported and are ignored. Defaults to no helpers support (empty path). Note that jade does not support loading helpers this way.
Looks as though handlebars-helpers has a different signature than what is required by hapi
I'm trying to use params on my main route. But in fact the params are not set and they are used in the path :
Router.map(function funcClientRouterMap(){
this.route('home', {
path: '/:_redirect?',
action: function funcClientRouterMapAction(){
console.log(this.path, this.params);
}
})
});
now if i try manual redirection here is what i get :
Router.go('home'); // it redirects on / => ok
Router.go('home', {_redirect: test}); // this.path = /test, and this.params is empty
How can i use _redirect like a params and not a route ?
Thanks
Router.go accepts a path as its first argument (per the docs). So if you were trying to programmatically cause the same result as the user going to /redirectMeSomewhere, you just use:
Router.go('/redirectMeSomewhere');
And this.params._redirect should be 'redirectMeSomewhere'.
Note that like #apendua implies, if you have other routes it could cause chaos to have a route defined as /:anything, because the other routes may never get triggered. If the above doesn't do the trick, try commenting out all your other routes to see if that changes anything.