我尝试excerpt
一些文字并添加Read More
链接。到目前为止,我能够剪切文本并添加Read More
,但现在我可以在文本之间插入几行并阅读更多链接。
以下是我截断文字
的方法{{ str_limit($product['description_small'], $limit = 250, $end = ' Read More') }}
如何在文字下面添加$end = ' Read More'
几行并将其链接起来?
Laravel 4 +刀片模板
答案 0 :(得分:0)
您可以做的最好的事情是创建自己的函数来创建excerpt
并添加Read More
链接。
首先在composer.json
文件的autolaod
部分添加files
数组:
{
"autoload": {
"files": [
"app/helpers.php"
]
}
}
现在在app/
文件夹中创建helpers.php
文件。
然后加入app\start\global.php
:( NB:这仅适用于Laravel 4 )
// at the bottom of the file
require app_path().'/helpers.php';
然后:
composer dump-autoload
通过这种方式,Laravel将为您加载文件。
现在,在此文件中,您可以创建onw函数来管理excerpt
:
<?php
if( ! function_exists('createExcerptAndLink')){
function createExcerptAndLink($text, $limit, $url, $readMoreText = 'Read More') {
$end = "<br><br><a href=\"$url\">$readMoreText</a>";
return str_limit($text, $limit, $end);
}
}
现在,在每个视图中,您都可以使用新助手:
{{ createExcerptAndLink($product['description_small'], 250, "http://www.google.it", 'Read more here.') }}