Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate Javascript variable inside laravel blade curly braces

I want to concatenate javascript variable within blade {{ curly braces }} as following:

Currently:

ajax: "{{ route(api.colors) }},"

What I want:

var page='colors';
...
ajax: "{{ route(api."+page+") }},"

Is it possible?

like image 668
CairoCoder Avatar asked Nov 07 '17 12:11

CairoCoder


1 Answers

You cannot do this directly because the curly brackets are rendered on the server and javascript runs on the client side. You could put a placeholder in you route and then replace this part in your javascript code. Like so:

// Imagine the `api.page` route value is `/controller/{page}`:
ajax: "{{ route(api.page) }}".replace("{page}", page);
like image 182
Jerodev Avatar answered Oct 23 '22 02:10

Jerodev