【问题标题】:Blade mixed with PHP doesn't work刀片与 PHP 混合不起作用
【发布时间】:2013-08-21 18:19:13
【问题描述】:

这是我正在尝试做的事情(我的代码):

@extends('base')
@section('title', 'Quests')
@stop
@section('main')

<? $quests = array (  
    1 => array ( "name" => "Quest 1", "level" => 100, "description" => "Write your description here", "reward" => "Shield, Boots"), 
    2 => array ( "name" => "Quest 2", "level" => 100, "description" => "4 Peoples needed!", "reward" => "Sword, Stonecutter Axe, Armor, Present(choose 1)"), 
    3 => array ( "name" => "Inferno Quest", "level" => 100, "description" => "Very hard quest. You need a big team to do this quest, the quest is long too!", "reward" => "bla bla") 
); 

?>

<div id="spaceholder">&nbsp;</div>
<div class="container">
    <div class="doc-content-box">
        <legend>Quests</legend>
        <p>The quests listed below are not all the quests on the server, there are many many other quests, but these are the ones with experience bonus. The expeirence bonus rewarded from quests is multiplied with your current experience stage.</p>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Quest</th>
                    <th style="width: 70%">Notes</th>
                </tr>
            </thead>
            <tbody>
                <? foreach($quests as $quest)
                echo '<tr>
                    <td>
                        <strong>' . $quest['name'] . '</strong><br>
                        <span class="label label-success">Experience bonus: 100 000</span><br>
                        <span class="label label-important">Level requirement: 35</span><br>
                    </td>
                    <td>This quest requires completing all missions.</td>
                </tr>';
                ?>
            </tbody>
        </table>
    </div>
@stop 

输出是这样的:

  1. 首先,任务的数组显示为纯文本(代码)
  2. 任务名称也显示为纯文本(代码)

如果有用 Laravel 做这件事,我想知道。

【问题讨论】:

  • 视图文件的名称应该以:.blade.php 结尾,以使 Blade 模板在 Laravel 中工作。
  • @RubensMariuzzo 它是......它被命名为 quests.blade.php
  • 当您说您的数组显示为纯文本时,它看起来如何?
  • @RubensMariuzzo 3.imgland.net/jS-GL.png
  • 您是否配置为使用 php 的短代码?当您使用 &lt;?php ?&gt; 而不是 &lt;??&gt; 时会发生什么?

标签: laravel laravel-4 blade


【解决方案1】:

不要使用 &lt;? 和 ?&gt; ,而是使用 &lt;?php 和 ?&gt;,这让我在 CodeIgniter 的日子里受苦了。

我也更喜欢以刀片方式使用 foreach 循环。

@extends('base')
@section('title', 'Quests')
@stop
@section('main')

<?php
$quests = array (  
    1 => array ( "name" => "Quest 1", "level" => 100, "description" => "Write your description here", "reward" => "Shield, Boots"), 
    2 => array ( "name" => "Quest 2", "level" => 100, "description" => "4 Peoples needed!", "reward" => "Sword, Stonecutter Axe, Armor, Present(choose 1)"), 
    3 => array ( "name" => "Inferno Quest", "level" => 100, "description" => "Very hard quest. You need a big team to do this quest, the quest is long too!", "reward" => "bla bla") 
); 

?>

<div id="spaceholder">&nbsp;</div>
<div class="container">
    <div class="doc-content-box">
        <legend>Quests</legend>
        <p>The quests listed below are not all the quests on the server, there are many many other quests, but these are the ones with experience bonus. The expeirence bonus rewarded from quests is multiplied with your current experience stage.</p>
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Quest</th>
                    <th style="width: 70%">Notes</th>
                </tr>
            </thead>
            <tbody>
                @foreach($quests as $quest)
                    <tr>
                        <td>
                            <strong>{{$quest['name']}}</strong><br>
                            <span class="label label-success">Experience bonus: 100 000</span><br>
                            <span class="label label-important">Level requirement: 35</span><br>
                        </td>
                        <td>This quest requires completing all missions.</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
@stop 

我在我的项目中使用了类似的方法,效果很好。

【讨论】:

    猜你喜欢
    • 2019-01-31
    • 2016-05-31
    • 2019-01-12
    • 2018-08-28
    • 1970-01-01
    • 2014-11-15
    • 2018-08-11
    • 2016-03-11
    • 2013-06-04
    相关资源
    最近更新 更多