Let's say I have a piece of jQuery javascript that binds to a div on load and dynamically defines an img in that div. Something like this:
$(document).ready(function() {
$("#container").html("<img href='/images/myimage.jpg/>');
});
If I were to use this inlined in a Laravel view, I'd be able to use HTML::image() and Blade templates to specify the location of image. Something like this:
$(document).ready(function() {
$("#container").html("{{ HTML::image('images/myimage.jpg', 'My image') }}");
});
If I were then to take that piece of javascript, and, instead of inlining it in the view, place it inside a separate .js file, say, public/js/image.js, I could have Laravel load it as an asset;
Asset::add('image.js', 'js/image.js');
However, since it's now treated only as an asset, neither the Laravel PHP nor the Blade templating code is processed, so we literally get the string {{ HTML::image('images/myimage.jpg', 'My image') }} in the resulting html, instead of the templated-in values.
Is there a good approach for something like this?
Let's say I have a piece of jQuery javascript that binds to a div on load and dynamically defines an img in that div. Something like this:
$(document).ready(function() {
$("#container").html("<img href='/images/myimage.jpg/>');
});
If I were to use this inlined in a Laravel view, I'd be able to use HTML::image() and Blade templates to specify the location of image. Something like this:
$(document).ready(function() {
$("#container").html("{{ HTML::image('images/myimage.jpg', 'My image') }}");
});
If I were then to take that piece of javascript, and, instead of inlining it in the view, place it inside a separate .js file, say, public/js/image.js, I could have Laravel load it as an asset;
Asset::add('image.js', 'js/image.js');
However, since it's now treated only as an asset, neither the Laravel PHP nor the Blade templating code is processed, so we literally get the string {{ HTML::image('images/myimage.jpg', 'My image') }} in the resulting html, instead of the templated-in values.
Is there a good approach for something like this?
Share Improve this question asked May 19, 2013 at 17:44 Andrey ButovAndrey Butov 2,27919 silver badges28 bronze badges 4- Not without sending it to the server so you could run it through the Blade parser and get the parsed string back. Or you could just use raw HTML, as you showed in your first code example. – Jason Lewis Commented May 19, 2013 at 17:51
- Are you only asking this to get the base URL appended? – Franz Commented May 19, 2013 at 18:33
- I just chose the specifics for the example. A general accepted technique would be useful. – Andrey Butov Commented May 19, 2013 at 18:58
- 1 Generally - don't mix php and JS. If you need to pass values to a JS script either output it as a meta/link tag in your HTML, or write some javascript out to the page that does such config - like google analytics might. – Phill Sparks Commented May 19, 2013 at 19:32
2 Answers
Reset to default 4I'm doing this here. The way I found was:
1 - create a 'js' file inside the view directory tree, something like
app\views\javascript\mycustomJS.blade.php
2 - then render it wherever you need:
<script>
@include('javascript.mycustomJS')
</script>
It's blade, it will be processed as it should.
This is far from ideal, I know, but it works for me, for now. :)
I think you can put it in a php file. I've done this before with css.
Asset::add('image.php', 'js/image.php');
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745622652a4636615.html
评论列表(0条)