Jump to content

Truncate Via Templates


TDBF

Recommended Posts

I would like to ask that IPs stops truncating posts/comments via the theme templates.

At the moment, the theme function doesn't actually truncate the post but instead just hides it from view after a certain amount of characters. This will increase the amount of memory and add a further hit on page rendering which really isn't necessary.

I would suggest truncating text long before it gets to the templating engine (from PHP) or fix the theme function to actually truncate the text properly.

Link to comment
Share on other sites

  • 3 years later...
3 hours ago, bfarber said:

There's a truncated() method for content items that can be used.

I would like to truncate the content field of pages records to show only 3 lines of it in the records list page. JavaScript works nice, but the full content of several database records is loaded to be removed afterwards.

How could I use the truncated() for this approach? Is there any guidelines about it? 

Link to comment
Share on other sites

$record->truncated( FALSE, 500 );

The first parameter is a boolean indicating if you intend to only show one line of text. We will swap line breaks for spaces to make the returned text a little more useful.

The second parameter is how many characters to return. You'll need to play with that if you intend to show "3 lines of text" dependent upon the viewports you are targeting and so forth.

Link to comment
Share on other sites

14 hours ago, Rikki said:

If you're working in templates there's also a {truncate} plugin that'll do it for you. Just do {truncate="$content" length="200"}.

How to use raw with truncate?

I am using:

{$row->_content|raw}

And I am trying without success:

{truncate="$row->_content" length="50"}

Shows html code.

{truncate="$row->_content|raw" length="50"}

Does not work.

{truncate="$row->_content" length="50"|raw}

Does not work either.

Link to comment
Share on other sites

You can't. Imagine that you have a string like "<b>some text here</b>" and the truncating would fall in between the HTML tags. If you allowed raw HTML to be retained, you would have an opening bold tag but no closing tag, resulting in invalid HTML that would make the entire rest of your page bold.

Link to comment
Share on other sites

16 hours ago, bfarber said:

You can't. Imagine that you have a string like "<b>some text here</b>" and the truncating would fall in between the HTML tags. If you allowed raw HTML to be retained, you would have an opening bold tag but no closing tag, resulting in invalid HTML that would make the entire rest of your page bold.

You a right.

I am using the code below and did what I needed:

$record->truncated(TRUE, 250 );

Thanks!

Agt GIF by America's Got Talent

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...