Writing JS code in blade templating engine - laravel-5

I have a Laravel 5.0 project and inside of that I have a foo.blade.php file,in here I want to use some Javascript to get the pathname of the link the user currently in like so
window.location.pathname
Is it possible? Or blade templating allows only php to be written on the file?

First, Javascript runs client side, so whatever you write in <script> tag will be totally ignored by laravel.
But if you want to access path (route) in laravel, you can check this question.

Related

How to Use Vuejs on Laravel blade

everyone, I want to make a dormitory management system with laravel and vuejs but I don't know how to display my data instead of "welcome.blade.php" file but instead of "blade" use vue component if someone knows this please help me how to do this. I use API route for this
thanks everyone
You don't use it instead of blade you mostly use it with or inside blade.
Use Vue like normally would just treat your blade template as your plain HTML.

Laravel - how to access public folder from JS file

I am using the exact same Ajax call in multiple blade templates.
If Ajax fails, I show an error jpg located in public/images/danger.png.
When I had my JS in the blade file written inline in <script> tags, it was easily accessible as I had blade helpers to access public with:
{{asset('/images/danger.png')}}
Yet now, accessing /public with:
http://"+location.host +"/public/images/danger.png
is impossible.
It would be unDRY to put the exact same AJAX call in 12 blades, no need to elaborate why..
How do you guys counter this issue?
remove /public from your route,you can directly use /images/imgname.jpg
You can just use absolute routes in your javascript files. This will however not work if your Laravel application sits in a sub folder.
"/public/images/danger.png"
just the same as used in html (in blade)
like
$('.abc').attr("src", "{{asset('imgs/male_b.svg')}}");

Laravel: Using a mix between Blade and Vuejs

I'm developing a website using Laravel and I will need a mix between Blade templates and Vuejs components.
Basically the requirements of the project state that in the customer pages we can only use Blade but in the admin pages we are free to use anything, so we will use Blade + Vuejs.
I know is possible to create a form in Blade, and then require or #include that form in any other Blade file. Is it also possible require that form inside a Vuejs component?
For example, Is it possible to do something like this:
<my-form-component>
#include('path/to/blade/form')
</my-form-component>
If you want just use vue js in blade template, you can definetely use,
Hope you aware that blade and vue share similar syntax {{ }} for variable interpolation, to avoid vue and blade conflicting, you should add # notations ex: #{{ .. }} so blade engine will skip it,
But if you want to use vuejs component and blade template mixing, you shouldnot do it..

Using Laravel includes inside a vue component?

I have a set of components, some components include other components via #include('my-sub-component')
The issue is that when I want to use vue I have to put the entire component in the vue.js file inside the template tags.
But inside the template I want to use the #include so I can include some sub components. This does not work.
How can I include blade components inside a vue component?
As far as I know, it's impossible. You can't use PHP (Laravel Blade) .blade.php tags inside Vue Component .js, of course Vue can't execute PHP script in this case. The possible use case is you use the Vue Component inside Laravel Blade.
The closest thing you can get to this is to use inline templates. You can't access blade directives it if you are writing your templates in single file components and compiling them with webpack or something because they are not being rendered with PHP.
You could put the style and scripts components in the SFC though and but in order to use blade directives the template would need to be in a .blade.php file

Can Laravel Blade be used in a sandbox environment?

Can Laravel Blade be used in a sandbox environment, similar to Twig's sandbox extension?
I have the need to allow users to use a template system but obviously do not want them executing arbitrary PHP code on the server.
I would like to use Blade since it's already part of Laravel but suspect this isn't possible.
The short answer is no, by default.
You could probably implement this yourself however, by extend blade to add custom functionality. See the Blade Documentation.

Resources