Laravel 中的刀片模板是否有截断修饰符,很像 Smarty?
我知道我可以在模板中写出实际的 php,但我正在寻找更好的东西来编写(让我们不要进入整个 PHP 是模板引擎的辩论)。
所以例如我正在寻找类似的东西:
{{ $myVariable|truncate:"10":"..." }}
我知道我可以通过作曲家使用 Twig 之类的东西,但我希望 Laravel 本身具有内置功能。
如果没有,是否可以创建自己的可重用修饰符,如 Smarty 提供的。我喜欢 Blade 不会过度使用所有语法这一事实,但我认为 truncate 是一个真正方便的功能。
我正在使用 Laravel 4。
在 Laravel 4 和5(最高 5.7),您可以使用 str_limit
,它限制字符串中的字符数。
在 Laravel 5.8 以上版本中,您可以使用 Str::limit
助手。
//For Laravel 4 to Laravel 5.5
{{ str_limit($string, $limit = 150, $end = '...') }}
//For Laravel 5.5 upwards
{{ \Illuminate\Support\Str::limit($string, 150, $end='...') }}
更多 Laravel 辅助函数 http://laravel.com/docs/helpers#strings
Laravel 4 有 Str::limit
将截断到确切的字符数,还有 Str::words
将截断单词边界。
查看:
http://laravel.com/api/4.2/Illuminate/Support/Str.html#method_limit
Str::words
的等价物吗?
编辑:这个答案是在 Laravel 4 beta 期间发布的,当时 Str 类不存在。现在在 Laravel 4 中有更好的方法来做到这一点——这是达斯汀在下面的回答。由于 SO 规则,我无法删除此答案(它不会让我这样做)
Blade 本身没有该功能。
在 Laravel 3 中有 Str 类 - 你可以这样做:
{{ Str::limit($myVariable, 10) }}
在这个阶段,我不相信 Str 类在 Laravel 4 - but here is a port of it that you can include in composer 中添加到您自己的项目中
Laravel 7.* 的更新:Fluent Strings 即一个更流畅、面向对象的接口,用于处理字符串值,与传统的字符串操作相比,允许您使用更易读的语法将多个字符串操作链接在一起。
limit 示例:
$truncated = Str::of('The quick brown fox jumps over the lazy dog')->limit(20);
输出
The quick brown fox...
words 示例:
$string = Str::of('Perfectly balanced, as all things should be.')->words(3, ' >>>');
输出
Perfectly balanced, as >>>
Laravel 6.* 更新:你需要这个包才能工作所有 laravel 助手作曲家需要 laravel/helpers
对于在控制器中使用助手,不要忘记包含/使用类
use Illuminate\Support\Str;
Laravel 5.8 更新
这是用于处理字符串中的字符:
{!! Str::limit('Lorem ipsum dolor', 10, ' ...') !!}
输出
Lorem ipsu ...
这是用于处理字符串中的单词:
{!! Str::words('Lorem ipsum dolor', 2, ' ...') !!}
输出
Lorem ipsum ...
这是处理字符串 Laravel Helpers 的最新帮助文档
为了保持你的代码 DRY,如果你的内容来自你的模型,你应该采用稍微不同的方法。像这样编辑您的模型(在 L5.8 中测试):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Comment extends Model
{
public function getShortDescriptionAttribute()
{
return Str::words($this->description, 10, '...');
}
}
?>
然后在你看来:
{{ $comment->short_description }}
您可以设置命名空间,如:
{!! \Illuminate\Support\Str::words($item->description, 10,'....') !!}
对于像这样简单的事情,我更愿意做一个助手 - 例如:
在您的 /app/helpers.php
中创建一个 helpers.php
文件,其中包含以下内容:
<?php
if (! function_exists('short_string')) {
function short_string($str) {
$rest = substr($str, 0, 10);
return $rest;
}
}
在您的 composer.json
中自动加载时注册 helper.php
"autoload": {
"files": [
"app/helpers.php"
],
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
]
}
之后,您可以在刀片文件中使用,例如:
{{ short_string($whatever_as_text) }}
然后,您可以在您的应用程序中全局使用这个简单的功能。
这适用于 Laravel 5:
{!!strlen($post->content) > 200 ? substr($post->content,0,200) : $post->content!!}
您可以设置字符串限制,如下例所示:
<td>{{str_limit($biodata ->description, $limit = 20, $end = '...')}}</td>
它将仅显示 20 个字母,包括空格并以 ... 结尾。
https://i.stack.imgur.com/3f05O.png
在 Laravel 4 和 5(最高 5.7)中,您可以使用 str_limit,它限制字符串中的字符数。
在 Laravel 7 中,你可以使用 Str::limit 助手。
//For Laravel to Laravel 7
{{ Illuminate\Support\Str::limit($post->title, 20, $end='...') }}
您可以使用 sub_str:
{{substr($myVariable,10)}}
Str::words 方法限制字符串中的单词数。可以通过其第三个参数将附加字符串传递给此方法,以指定应将哪个字符串附加到截断字符串的末尾:
use Illuminate\Support\Str;
return Str::words('Perfectly balanced, as all things should be.', 3, ' >>>');
// Perfectly balanced, as >>>
您可以将第三个参数传递给该方法以更改将附加到截断字符串末尾的字符串:
use Illuminate\Support\Str;
$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20, ' (...)');
// The quick brown fox (...)
Laravel 6 更新:
@php
$value = 'Artificial Intelligence';
$var = Str::limit($value, $limit = 15, $end = '');
print_r($var);
@endphp
<p class="card-text">{{ Illuminate\Support\Str::limit($value, 7) }}</p>
<h2 class="main-head">{!! Str::limit($value, 5) !!}</h2>
下面的示例使用 laravel 8。
{!! Str::words("$post->content", 8, ' ...') !!}
{!! Str::limit("$post->content", 15, ' ...') !!}
你可以像这样使用 Laravel Blade 指令。
Str::limit('$yourString',numberOfElement).
例如 Str::limit('Test', 3) 将向您显示 Tes...
不定期副业成功案例分享
str
助手已被删除,您必须改用Illuminate\Support\Str::limit($string)