htmlspecialchars()期望参数1是字符串,在Laravel中给出的数组

时间:2018-02-24 11:35:48

标签: php html5 laravel laravel-5.3 laravel-blade

我的Laravel刀片模板

中出现此错误
  

htmlspecialchars()期望参数1为字符串,给定数组为

我试图将数组转换为刀片模板中的字符串。

以下是代码

<script>
    var value = {{ $sliderImageDataArray }}.toString()
    window.MedicPressSliderCaptions = value;
</script>

$silderImageDataArray是存储在此处的变量和值。

以下是$silderImageDataArray

[{"title":"First title","text":"<p><span id=\"hs_cos_wrapper_module_1498510869515998\" class=\"hs_cos_wrapper hs_cos_wrapper_widget_container hs_cos_wrapper_type_widget_container\" data-hs-cos-general-type=\"widget_container\" data-hs-cos-type=\"widget_container\">First title<\\\/span><\\\/p>\n<p><a class=\"btn  btn-secondary\" href=\"http:\\\/\\\/localhost\\\/sencare\\\/book-appoinment\\\/\" target=\"_self\">  Make An Appointment <\\\/a>\\u00a0<a class=\"btn  btn-light\" href=\"http:\\\/\\\/localhost\\\/sencare\\\/our-doctors\\\/\" target=\"_self\"> Our Doctors<\\\/a><\\\/p>\n","is_video":false},{"title":"Second","text":"<p><span id=\"hs_cos_wrapper_module_1498510869515998\" class=\"hs_cos_wrapper hs_cos_wrapper_widget_container hs_cos_wrapper_type_widget_container\" data-hs-cos-general-type=\"widget_container\" data-hs-cos-type=\"widget_container\">Second<\\\/span><\\\/p>\n<p><a class=\"btn  btn-secondary\" href=\"http:\\\/\\\/localhost\\\/sencare\\\/book-appoinment\\\/\" target=\"_self\">  Make An Appointment <\\\/a>\\u00a0<a class=\"btn  btn-light\" href=\"http:\\\/\\\/localhost\\\/sencare\\\/our-doctors\\\/\" target=\"_self\"> Our Doctors<\\\/a><\\\/p>\n","is_video":false}]

1 个答案:

答案 0 :(得分:5)

{{}}将由Blade模板引擎转换为echo()。而你正在尝试将数组作为字符串回显。

您可以将其转换为JSON:

var value = '{{ json_encode($sliderImageDataArray) }}';

如果是Laravel系列或型号:

var value = '{{ $sliderImageData->toJson() }}';