About rootmst

rootmst's picture

First Name
Marco

Last Name
Mosetti

Birthday
10/12/1973

Recent comments

Who's online

There are currently 0 users and 0 guests online.

Rate this article

4.75
Average: 4.8 (4 votes)
Your rating: None

Custom teasers in Drupal 6 with views 2

19 Jan

Update: With the new releases of Views it is possible to do all the trimming and html stripping directly from the views UI.


While I was developing this website I run into the need of having total control over the teasers of my contents especially while using views to extract them.  Probably if you have just one content type you can manage it with the default option in drupal administration.

The simplest solution I have found is to use the neat_trim function. So what you have to do is just copy and paste the following function into your theme template.php file (just remember not to copy the "?>" php closure):

 

<?php
function neat_trim($str, $n, $delim='&hellip;') {
$len = strlen($str);
if ($len > $n) {
preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);
return rtrim($matches[1]) . $delim;
}
else {
return $str;
}
}
?>

 

Now you just need to go on each template file where you want this function (you can easily find and create them inspecting your views from the views admin panel) and put the following code:

 

<?php
$string = neat_trim($output, 100);
print $string;
?>

 

This will print out the content limited to 100 characters or the full content in case it has less then 100 characters. A small extra tip if you are using a WYIWYG editor or if you have some HTML code in your page when you create your content is to add a strip_tags so as to remove any html tag in you text to avoid unwanted results on your page. So the code will look like that:

 

<?php
$output = strip_tags($output);

$string = neat_trim($output,100);
print $string;
?>

 

This is how the news on the home page and the blog block teaser were created in this website.

Comments

On Monday 19 Jan 2009

thanks. it works now. this is so cool. however for the longest i have been trying to apply it to views using fields and it did not work untill to applied stiptag first and then apply function like so:

$title_output=strip_tags($fields['title']->content);
$string_title = neat_trim($title_output, 30);

print ($string_title);

 

from this page one can learn how to reapply link after striptag

 

thanks again:)

rootmst's picture
On Monday 19 Jan 2009

Thanks for pointing it out.

if ($len > $n) is the right syntax, just my wysiwyg converted the > to &gt; (which is the character version of it).

Hope it will fix your error.

PS: also '&hellip;' in the first line is a "special charachter", if it gives any problem just put '...' (three dots) instead.

On Monday 19 Jan 2009

looks like cool stuff. i tried the code but i keep getting this error:

Parse error: syntax error, unexpected ';' in /var/www/html/drupal-6.1/sites/... template.php on line 4.

i placed the code in like this:

<?php
function neat_trim($str, $n, $delim='&hellip;') {
$len = strlen($str);
if ($len &gt; $n) {
preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);
return rtrim($matches[1]) . $delim;
}
else {
return $str;
}
}

it does not seem to like this line: if ($len &gt; $n) {

this is just what i'm lookin for but just cant get past that error

Copyright © 2008 Mosetti Ltd. All rights reserved.

Website language