ZOO Teaser Text - Overwrite Protected Method

We already looked at a method of implementing teaser text here but that method is susceptible to being overwritten if ZOO is updated. In this method, we'll look at how we can do the same thing while keeping it safe from deletion.

In essence, we use the same code but we'll make our own override with it.

Create a new folder called custom_helpers in templates/[templatename]/html/

and a php file called ptphelpers.php Copy the below code into it and save it in your newly created custom_helpers folder: templates/[templatename]/html/custom_helpers

class PTPHelperHelper {
	/* Creates teaser text with html intact */
	public static function truncatewtags($text, $length, $suffix = '…', $isHTML = true){
		$i = 0;
		$tags = array();
			preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
			foreach($m as $o){
				if($o[0][1] - $i >= $length)
				$t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
				// test if the tag is unpaired, then we mustn't save them
				if($t[0] != '/' && (!isset($simpleTags[$t])))
					$tags[] = $t;
				elseif(end($tags) == substr($t, 1))
				$i += $o[1][1] - $o[0][1];
		// output without closing tags
		$output = substr($text, 0, $length = min(strlen($text),  $length + $i));
		// closing tags
		$output2 = (count($tags = array_reverse($tags)) ? '' : '');
		// Find last space or HTML tag (solving problem with last space in HTML tag eg. )
		$pos = (int)end(end(preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE)));
		// Append closing tags to output
		// Get everything until last space
		$one = substr($output, 0, $pos);
		// Get the rest
		$two = substr($output, $pos, (strlen($output) - $pos));
		// Extract all tags from the last bit
		preg_match_all('/<(.*?)>/s', $two, $tags);
		// Add suffix if needed
		if (strlen($text) > $length) { $one .= $suffix; }
		// Re-attach tags
		$output = $one . implode($tags[0]);
		//added to remove  unnecessary closure
		$output = str_replace('','',$output); 
		return $output;

Like the last tuorial, this sets up the function we're going to use called truncatewtags.

But in this instance, it's inside it's own class called PTPHelperHelper. When we call it, we'll call the class and then the function inside the class - as you'll see next.

We really only want this code to be executed on the teaser view within the zoo component (frontpage and category views), so we're only going to load it in the /media/zoo/applications/[yourapplication]/templates/[yourtemplatename]/renderer/item/teaser.php

The below code is what we'll want to insert into our teaser.php file. In this article we'll be putting into the content position of our teaser file but it can be used in any other places that use content from ZOO (limited to text obviously). Put the following code around line 14 in the teaser.php file.

require_once(JPATH_THEMES .'/[yourtemplate]/html/custom_helpers/ptphelpers.php');

Then we are going to replace

<?php if ($this->checkPosition('content')) : ?>
    <div class="pos-content">
        <?php echo $this->renderPosition('content', array('style' => 'block')); ?>
<?php endif; ?>


<?php if ($this->checkPosition('content')) : ?>
    <div class="pos-content">
            <?php $teaserText = $this->renderPosition('content', array('style' => 'block'));
            $teaserText = PTPHelperHelper::truncatewtags($teaserText, 250, '...', true); //$teasertext is the content we want to truncate and 250 is the limit of the characters
            echo $teaserText;?>
<?php endif; ?>

As you can see, we are calling the PTPHelperHelper class and then the function inside it: truncatewtags. If we wanted to, we could have 100 different functions inside this class and we'd call them like this.

To view more about this and how to set up allowed tags, refer to the alternative method here

Also just a little side note, every now and then if there is an image in the text area it will have a heart attack, we could fix this with a strip tags, but generally we just delete it


You may be interested in...

Joomla Development

Joomla Development Development, tools, support...

Had enough browsing?

Get in touch

Get in touch

We wont charge you for the inital project meeting. So really you have nothing to lose.

Fill out the form below with as much information as you can provide and we will be in touch with you asap to discuss.

Please type your full name. Also only allowing alpha numeric characters here. Why do you have all this fancy stuff in your name anyway?

Invalid email address.

Please enter a valid phone number.

Enquiry Type(*)
Invalid Input

Please only use alphanumeric characters in your message

Invalid Input