doutao1171 2018-04-27 02:01
浏览 233
已采纳

Laravel - 带有刀片compileString的php eval

I'm trying to parse a blade template string into php format and use eval() to evaluate string as php code

 $array = [
     'foo' => 'bar',
     'bar' => 'foo'
 ];
 $content = '@foreach($array as $value){{$value}}@endforeach';
 $blade = Blade::compileString($content);
 $php = eval($blade);

This is my code for testing so far and it throws exception

ParseError: syntax error, unexpected '<', expecting end of file

value of $blade after compileString()

<?php $__currentLoopData = $array; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><?php echo e($value); ?><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

What causes this error? Is eval() not compatible with the way compileString() is parsing blade into php?

  • 写回答

1条回答 默认 最新

  • dsxd62219570 2018-04-27 02:21
    关注

    As stated in the manual:

    The code must not be wrapped in opening and closing PHP tags, i.e. 'echo "Hi!";' must be passed instead of '<?php echo "Hi!"; ?>'. It is still possible to leave and re-enter PHP mode though using the appropriate PHP tags, e.g. 'echo "In PHP mode!"; ?>In HTML mode!<?php echo "Back in PHP mode!";'.

    Your blade is wrapped in <?php tags, and the compilation fails. Remove the enclosing PHP tags, but leave the interstitial tags.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿