在Laravel和ViewMore按钮中制作单个产品信息页面



Laravel和框架中真的很新,所以请耐心等待。我正在尝试学习它,我选择编写包含少量产品(标题,价格,图像,描述)的简单索引页面。到目前为止,一切都在那里。现在我正在尝试将ViewMore按钮放在描述部分,当我单击按钮为我加载新页面时,其中包含有关该产品的更多信息。

这是我显示所有产品的索引页面的视图,并希望使按钮查看更多...

@if($product['image'])
    <img class="max-150" src="{{ $product['image'] }}" alt="{{{ $product['title'] }}}" />
     <br /><br />
@endif
@if($product['description_small'])
     <p>{{ $product['description_small'] }}</p>
     <p>{{ str_limit($product['description_small'], $limit = 250, $end = '<br><br> <a href="{{ URL::to('/product/single/' . $product['product_id']) }}" class="btn btn-primary">View More</a>') }}</p> 
@endif

现在我在查看更多链接中出现错误,错误显示

生产。错误:异常"Symfony\Component\Debug\Exception\FatalErrorException",消息为"语法错误,意外的'product_id'(T_STRING)"。

当我只用 href 制作它时,它工作得很好......

 <p><br><br> <a href="{{ URL::to('/product/single/' . $product['product_id']) }}" class="btn btn-primary">View More</a></p>

你在回声中回声 ( {{ {{ }} }} )。

这有效:

{{ str_limit($product['description_small'], $limit = 250, $end = '<br><br><a href="' . URL::to('/product/single/' . $product['product_id']) . '" class="btn btn-primary">View More</a>') }}

虽然我发现这段代码有点草率且难以阅读,但更干净的方法是:

$button = sprintf('<a href="%s" class="btn btn-primary view-more">View More</a>', URL::to('product/single/' . $product['product_id']));
echo str_limit($product['description_small'], 250, $button);

。或类似的东西(我最喜欢的):

{{ str_limit($product->description, 250) }}
@if (strlen($product->description) > 250)
    <a href="{{ URL::to('product/single/' . $product->id) }} " class="btn btn-primary view-more">View More</a>
@endif

不要使用那些讨厌的内联<br>标签,使用一些 CSS 进行样式.btn.view-more可以解决问题:)

最新更新