I woke up this morning with a stupid question. Can you put blade inside a JavaScript variable:
var NoImg = '{{ asset('img/ni-img.png') }}';
that will output as a string, all I need is it to output an image
Yes, but it's best to use json_encode to make sure the resulting output is JavaScript-friendly:
var NoImg = {!! json_encode(asset('img/ni-img.png')) !!};
PHP will add the quotation marks (for strings), any necessary escaping characters (if you have a ' in your filename, for example), and will handle more complex data structures like arrays/objects too.
Yes. Blade is a server side template engine. If your have a blade view file, her will be executed before the javascript. In your case, NoImg will have the expanded value returned by the helper asset() when the html is returned from the server.
This will be work fine:
<?php
// variable from your controller...
$image = 'https://images.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png';
?>
// your js inside of a blade file...
var image = new Image(272, 92);
image.src = '{{ $image }}'; // or asset("path/to/your/image.png")
document.body.appendChild(image);
img.srcto the path.