最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Appending Laravel Blade from JQuery - Stack Overflow

programmeradmin1浏览0评论

I am trying to append Blade syntax from Laravel but can't get the browser to understand Blade. When I append the Blade syntax from my js file it output the code onto the page.

// this is my code..
$("#"+Current_box).append("@if ($errors->has('testing'))<span class='help-block'><strong>{{ $errors->first('testing') }}</strong></span>@endif")

It would output this on to the page:

@if ($errors->has('testing'))
{{ $errors->first('testing') }}
@endif

I am trying to append Blade syntax from Laravel but can't get the browser to understand Blade. When I append the Blade syntax from my js file it output the code onto the page.

// this is my code..
$("#"+Current_box).append("@if ($errors->has('testing'))<span class='help-block'><strong>{{ $errors->first('testing') }}</strong></span>@endif")

It would output this on to the page:

@if ($errors->has('testing'))
{{ $errors->first('testing') }}
@endif
Share Improve this question edited May 20, 2016 at 8:57 Audite Marlow 1,0641 gold badge8 silver badges25 bronze badges asked May 7, 2016 at 14:37 SeanDp32SeanDp32 3095 silver badges17 bronze badges 1
  • 1 You don't "get the browser to understand blade". You render the blade template and return the rendered html. – Ram Commented May 7, 2016 at 14:47
Add a ment  | 

2 Answers 2

Reset to default 4

you can generate your view in your controller. and append it to your jQuery

//blade
$html = view('someView')->render();
retrun json_encode($html)

And in your blade you can do something like this

<script type="text/javascript">
@if( isset($html) )
 $("#"+Current_box).append({{$html}})
@endif

I don't know if you are using an ajax call or simply a pageload so dont forget to wrap you script in document ready or ajax function

Laravel Blade is processing on server side by PHP. Not in your browser like JS. Therefore if you do like you did, you get normal text in your browser. So your server process PHP/Laravel code with Blade and provide for your browser static code with HTML, CSS and JS.

If it will help you, you can try put your blade code in appropriate *.blade.php file.

发布评论

评论列表(0)

  1. 暂无评论