【问题标题】:Display a comma separated list as a numbered list on html page在 html 页面上将逗号分隔的列表显示为编号列表
【发布时间】:2018-10-11 18:09:25
【问题描述】:

我正在 Laravel 中创建一个 foodlog 应用程序,有关如何制作食谱的说明存储在数据库中的逗号分隔列表中,如下所示:

cook the chicken, add peppers and onions, add sauce, serve with rice

我想让它在我的网站上显示为一个编号列表,例如:

1. cook the chicken
2. add peppers and onions
3. add sauce
4. serve with rice

我目前有字符串出现在“”标签中,但希望将其作为编号列表。有什么想法吗?

info.blade.php

<h1>{{ $recipes->recipe_name }}<h1>
<h2>Ingredients:</h2> <p>{{ $recipes->ingredients}}</p><br>
<h2>How to:</h2> <p>{{ $recipes->description }}</p><br>

RecipesController.php

public function showinfo($id) {     
    $recipes = Recipes::find($id);
    return view('recipes.info', compact('recipes'));
}

看起来像这样:

【问题讨论】:

    标签: arrays laravel html-lists


    【解决方案1】:

    希望这会有所帮助。

    info.blade.php

    $descriptionList = new \Illuminate\Support\Collection(explode(",", $description));
    
    echo "<ol>";
    $descriptionList->each(function($description){
         echo "<li>" . $description . "</li>";
    });
    echo "</ol>";
    

    $description = "cook the chicken, add peppers and onions, add sauce, serve with rice";
    
    $descriptionList = explode(",", $description);
    <ol>  
        @foreach ($descriptionList as $descriptionItem)
           <li>{{ $descriptionItem }}</li>
        @endforeach    
    </ol>
    

    您可以将其转换为使用 Laravel 刀片模板。

    【讨论】:

    • 不错的食谱:Prevent xss, &lt;script&gt;alert('xss')&lt;/alert&gt;, Whoops。请注意,第一个实现容易受到 xss 攻击。 Blade 会自动转义输出以防止这种情况发生。请使用第二种实现方式!
    猜你喜欢
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 2016-10-24
    • 2021-05-18
    • 2011-01-26
    • 2013-07-03
    相关资源
    最近更新 更多