Truncate/ Limit Characters Blog Post Snippet

Permalink
Currently the blog_index.php template from the page_list block displays a snippet by area:

$a = new Area('Main');   $a->disableControls(); $a->display($cobj);

This requires the client to put text into 2 different content blocks. 1 for the snippet, 1 for the remaining content.

Its much more preferable for the client to enter all the text in 1 content block and truncate the text from this.

QUESTION:
1. How can you truncate characters from a block which you're pulling in to display?

I've messed around with:
//Snippet
   $textHelper = Loader::helper("text");
   $contentcontroller = Loader::controller('content'); 
   $content = $contentcontroller->getContent($a->display($cobj));
   $a = new Area('Main');   $a->disableControls(); $textHelper->shortText(strip_tags($content),300);

I.e - The "shortText" function from "text" helper. But naturally since you're pulling in a block not text this fails.

Any ideas how to convert the block to a string? (- without going via mySQL?)

Cheers!

View Replies:
jaredquinn replied on at Permalink Reply
jaredquinn
Blocks use a 'view' to render the contents of the block as HTML.

You have to remember that blocks aren't necessarily just textual content. You can have an image block, a form block, a Google Map block etc etc. If you were to convert a 'Google Map' block to a string and truncate it, the result would be messy, to say the least.

So I'll assume you only want to truncate the contents of a "Content" block. The best way to do this would be to custom template for the block you wish to customise (I'm assuming the "Content" block). Inside your custom template for the block you would use the text helper to do your truncation.

For documentation on Custom templates:

http://www.concrete5.org/documentation/general-topics/custom-templa...
websitehebben replied on at Permalink Reply
Hi Jared, thanks for your reply. I am strugeling with the same "problem".

I made a page_list custom template, but still don't know how to truncate the content (instead of description).

Any help would be great!

Also downloaded the "teaser block", wich doesn't seem to work for 5.5.
jordanlev replied on at Permalink Reply
jordanlev
I will be updating the page list teasers addon soon to work with 5.5.

To answer the original question, though, I have a custom template for the page list block that contains a lot of sample code for doing things like this:
https://github.com/jordanlev/c5_clean_block_templates/blob/master/pa...
(click the "raw" link at the top-right if you want to save / download the code file instead of just looking at it).

Here is the specific code you're looking for (note that as @jaredquinn said, it only works for content blocks -- also note that it doesn't work if you have "Area Layouts" on the page):
<?php
$excerpt = '';
$pageBlocks = $page->getBlocks('Main');
if (count($pageBlocks) > 0) {
    foreach ($pageBlocks as $pb) {
        if ($pb->btHandle == 'content') {
            $excerpt = $pb->getInstance()->getContent(); //NOTE: getContent() function is specific to the content block type -- it cannot be called on just any kind of block!
              if ($controller->truncateSummaries) {
                  //$excerpt = $th->shorten($excerpt, $controller->truncateChars); //Concrete5.4.2.1 and lower
                  $excerpt = $th->shortenTextWord($excerpt, $controller->truncateChars); //Concrete5.4.2.2 and higher
              }
            break;
          }
    }
}
RadiantWeb replied on at Permalink Reply
RadiantWeb
ProBlog does this for you. The list block lets you chose to use either the content or the page description, and then truncate those for you.

Just thought I'd let you know.

ChadStrat