In this video, we are going to create a utility function to be able to truncate post description.
So inside the utils, I'm going to create one file and I'll call it as helpers.
This file is going to contain our custom function to be used, and the first function I'm going to create
is called truncate.
Is equal to cost.
Try and kit post and I'll pass in the post that I want to truncate.
And I'm going to check the length of the post.
So if the post does length is more than 100, then I'm going to retain the post.
Dot substring.
And this method takes into argument that is a starting point and where we want to end.
I want to start at zero and end at 100, and then I want to contact with this ellipsis.
Ask that could dot, dot, dot, dot.
Else.
I'm going to return the original post.
Lastly, I need to export this one.
TRUNCATE.
So how are you going to use it?
We are going to use this method inside the template.
So how are you going to use it?
You may be thinking that we can require this file inside the templates, but it will not work.
We are going to take an advantage of the app look as we did for this one.
So anything we add to the low cost, we're going to have it inside the template.
So above here I'm going to add what is called helpers.
And I'm going to make use of app dot low cost.
And then I want to add this method called truncate post.
I need to require the function as truncate post, which is this one.
So I'm assigning a variable to the function call as truncate post.
So inside my template I have access to this.
So where I want to use it, I can go ahead and then use that.
So let's locate the home page, which is a views folder, and here we go for this.
Let's look at the description, which is this.
So I'm going to remove this one and make this of truncate post.
It must be the same and outpace the description.
So now when I refresh it, let's have a look.
I have this error meaning that it couldn't find the variable called truncate post.
So let me save it and let me copy the name.
And in here, let me replace with that.
I think I made some typing mistake.
So let's refresh it and you can see that it has truncated the description.
And where I want to use the truncate is inside the profile.
And I also want to truncate this one.
So I'm going to copy this one.
And let's go to the profile.
And let's locate the description.
So I'm going to paste this one.
All right, so let's refresh it.
And we have it as that.
And for the image, we can use cases by providing what is called minimum height of the image.