Truncate string in Laravel blade templates

谁说我不能喝 提交于 2019-11-27 05:13:20

问题


Is there a truncate modifier for the blade templates in Laravel, pretty much like Smarty?

I know I could just write out the actual php in the template but i'm looking for something a little nicer to write (let's not get into the whole PHP is a templating engine debate).

So for example i'm looking for something like:

{{ $myVariable|truncate:"10":"..." }}

I know I could use something like Twig via composer but I'm hoping for built in functionality in Laravel itself.

If not is it possible to create your own reusable modifiers like Smarty provides. I like the fact that Blade doesn’t overkill with all the syntax but I think truncate is a real handy function to have.

I'm using Laravel 4.


回答1:


In Laravel 4 & 5, you can use str_limit, which limits the number of characters in a string.

{{ str_limit($string, $limit = 150, $end = '...') }}

For more Laravel helper functions http://laravel.com/docs/helpers#strings




回答2:


Laravel 4 has Str::limit which will truncate to the exact number of characters, and also Str::words which will truncate on word boundary.

Check out:

  • http://laravel.com/api/4.2/Illuminate/Support/Str.html#method_limit



回答3:


Edit: This answer is was posted during the Laravel 4 beta, when Str class did not exist. There is now a better way to do it in Laravel 4 - which is Dustin's answer below. I cannot delete this answer due to SO rules (it wont let me)

Blade itself does not have that functionality.

In Laravel 3 there was the Str class - which you could do:

{{ Str::limit($myVariable, 10) }}

At this stage I do not believe the Str class is in Laravel 4 - but here is a port of it that you can include in composer to add to your own project




回答4:


You can Set the namespace like:

{!! \Illuminate\Support\Str::words($item->description, 10,'....')  !!}



回答5:


To keep your code DRY, and if your content comes from your model you should adopt a slightly different approach. Edit your model like so (tested in 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, '...');
    }
}
?>

Then in your view :

{{ $comment->short_description }}



回答6:


Laravel 5.8 Update

{!! Str::limit('Lorem ipsum dolor sit amet, consectetur adipisicing elit', 10, ' ...') !!}

Output

Lorem ipsu ... 



回答7:


This works on Laravel 5:

{!!strlen($post->content) > 200 ? substr($post->content,0,200) : $post->content!!}



回答8:


You can set string limit as below example:

<td>{{str_limit($biodata ->description, $limit = 20, $end = '...')}}</td>

It will display only the 20 letters including whitespaces and ends with ....

Example image




回答9:


For simple things like this I would prefer to make a helper - for example:

create a helpers.php file in your /app/helpers.php with following content:

<?php
if (! function_exists('short_string')) {
    function short_string($str) {
            $rest = substr($str, 0, 10);
            return $rest;
    }
}

Register the helper.php at autoload in your composer.json

   "autoload": {
        "files": [
            "app/helpers.php"
        ],
        "psr-4": {
            "App\\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    }

After that you can use in your blade file for example:

{{ short_string($whatever_as_text) }}

You can use this simple function, then, globally in your app.



来源:https://stackoverflow.com/questions/15012712/truncate-string-in-laravel-blade-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!