简体   繁体   English

从JQuery附加Laravel Blade

[英]Appending Laravel Blade from JQuery

I am trying to append Blade syntax from Laravel but can't get the browser to understand Blade. 我试图从Laravel追加Blade语法,但无法使浏览器理解Blade。 When I append the Blade syntax from my js file it output the code onto the page. 当我从js文件追加Blade语法时,它将代码输出到页面上。

// 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

you can generate your view in your controller. 您可以在控制器中生成视图。 and append it to your jQuery 并将其附加到您的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 我不知道您是使用ajax调用还是只是页面加载,所以不要忘记将脚本包装在文档就绪或ajax函数中

Laravel Blade is processing on server side by PHP. Laravel Blade由PHP在服务器端进行处理。 Not in your browser like JS. 不像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. 因此,您的服务器使用Blade处理PHP / Laravel代码,并使用HTML,CSS和JS为浏览器提供静态代码。

If it will help you, you can try put your blade code in appropriate *.blade.php file. 如果有帮助,您可以尝试将刀片服务器代码放在适当的* .blade.php文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM