You need to enable JavaScript to run this app. Laravel Blade模板如何结合多语言配置循环渲染轮播项内容
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Blade模板如何结合多语言配置循环渲染轮播项内容

Laravel 多语言轮播动态渲染实现方案

步骤1:调整多语言配置文件结构

修改lang/对应语言目录/pages/index.php,将轮播相关文案统一放到可遍历的数组结构中:

<?php
return [
    // 轮播项配置,按顺序对应每个轮播 slide
    'carrossel_items' => [
        [
            'title' => 'Bem-vindo ao <span>SysSite!</span>',
            'description' => 'Mantenha-se por dentro de tudo que rola no mundo da tecnologia! <br />Atualizações diárias para te manter antenado.',
            'button_text' => 'Read sd'
        ],
        [
            'title' => 'Conecte-se e divulgue suas ideias!',
            'description' => 'Crie postagens, comente, intereja e o mais importante: <br />evolua seu conhecimento sobre suas tecnologias favoritas!',
            'button_text' => 'Read More'
        ],
        [
            'title' => 'Sequi ea ut et est quaerat',
            'description' => 'Ut velit est quam dolor ad a aliquid qui aliquid. Sequi ea ut et est quaerat sequi nihil ut aliquam. Occaecati alias dolorem mollitia ut. Similique ea voluptatem. Esse doloremque accusamus repellendus deleniti vel. Minus et tempore modi architecto.',
            'button_text' => 'Read More'
        ]
        // 后续新增轮播项直接在这追加数组元素即可
    ]
];

步骤2:Blade 模板中循环渲染

删除模板中写死的所有轮播项代码,替换为如下循环逻辑:

@foreach (__('pages/index.carrossel_items') as $item)
<div class="carousel-item {{ $loop->first ? 'active' : '' }}">
    <div class="carousel-container">
        <h2 class="animate__animated animate__fadeInDown">{!! $item['title'] !!}</h2>
        <p class="animate__animated animate__fadeInUp">{!! $item['description'] !!}</p>
        <a href="" class="btn-get-started animate__animated animate__fadeInUp">{!! $item['button_text'] !!}</a>
    </div>
</div>
@endforeach

注意事项

  • 不同语言只需要对应修改各语言目录下的pages/index.php配置即可,模板代码无需改动
  • 由于文案中包含<br>、<span>等HTML标签,必须使用{!! !!}语法输出,避免Laravel自动转义HTML标签导致样式异常
  • 后续新增/减少轮播项只需要调整多语言配置中的carrossel_items数组即可,无需修改模板代码

内容的提问来源于stack exchange,提问作者ridesip850

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 17:54:06