Hey Raincity,

Great work on this theme. I'm using 2.0 for a project but for the life of me don't know how to use truncate_utf8 in a node template (or view template for that matter). I'm simply trying to truncate the text of certain teasers to various lengths.

So I put in:

print $content = truncate_utf8($content, 200, TRUE, TRUE);

in replacement of:

<?php echo $content; ?>

But nothing happens. I'm still struggling to theme for drupal 6 (especially views) since I'm not adept at php, and I can't quite my hands on a copy of the drupal 6 theming book in Canada. Any help is much appreciated.

Comments

Assigned:Unassigned»

The way to do it is like this :

<?php print truncate_utf8($content, 200, TRUE, TRUE); ?>

This should work. truncate_utf8 is a drupal function that you apply to the string you want to use. You can have a descritpion of the function here :

http://api.drupal.org/api/function/truncate_utf8/6

On the first line it tells you : truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE). So whatever you want to truncate, you have to put it instead of $string.

Feel free to ask any help for views2 :)

Thanks so much for your help.

I'm not sure what's going on but I replaced:

<?php echo $content; ?>

with:

<?php print truncate_utf8($content, 100, TRUE, TRUE); ?>

in node.tpl.php and I only get the "..." without the text when displaying any node. Should I be emptying any sort of cache?

As for views: I realize this is perhaps might not be appropriate to ask here but I really need the help truncating text in a view as well. Is Row-output-style (views-view-fields.tpl.php) the best way to get to the body field and, hence, truncate the body text (string) with truncate_utf8?

oh, I just figured how to truncate the body field by using:

<?php print truncate_utf8($output, 100, TRUE, TRUE); ?>

in views-view-field--body.tpl.php. That should suffice for most of what I need but I don't understand why this doesn't work for $content (or any other string) in node.tpl.php.

Just realised that if you want to truncate the BODY of a node, you have to use $node->body instead of $content. So it would look like this :

<?php print truncate_utf8($node->body, 100, TRUE, TRUE); ?>

as for views, here is a usefull link : http://views-help.doc.logrus.com/help/views/using-theme
Basically, if you want to truncate a view field, you can either edit the template of that specific field, or edit the field in the row output. In the field template, it should look like this :

<?php print $output; ?>

that you could replace like this : <?php print truncate_utf8($output, 100, TRUE, TRUE); ?>

In the row template, you have to call each field by its ID, so if your field ID is BODY (you can get the ID of each field by clicking on theme information) and output it like this :

<?php print $fields['body']->content; ?>

that you would then truncate like this :

<?php print truncate_utf8($fields['body']->content, 100, TRUE, TRUE); ?>

Makes sense ?

Thanks couzinhub,

<?php print truncate_utf8($output, 100, TRUE, TRUE); ?>

definitely works for me in views-view-field--frontpage--body.tpl.php. However, I can't figure out

<?php print $fields['some_id']->content; ?>

or views-view-fields.tpl. I replaced:

<?php print $field->content; ?>

with:

<?php print $fields['title']->content; ?><?php print truncate_utf8($fields['some_id']->content, 100, TRUE, TRUE); ?>

so that the bottom of views-view-fields looks like this:

<<?php print $field->element_type; ?> class="field-content"><?php print $fields['title']->content; ?><?php print truncate_utf8($fields['some_id']->content, 100, TRUE, TRUE); ?></<?php print $field->element_type; ?>></<?php print $field->inline_html;?>>

and successfully receive the title and body truncated however it repeats displaying each three times (title/body, title/body, title/body).

Also, using

  <?php print truncate_utf8($node->body, 100, TRUE, TRUE); ?>

in node.tpl.php still only gives me the ... as well. I'm going to install devel and learn how to use it. I really don't know why this doesn't work.

replace 'some_id' by the id of the field. (like 'body' or 'title')

About the views-view-fields, make sure you delete all the default code before output your fields. so what you add should be the only code in the template. The default code is meant to output each field automatically.
For the node template... that is kinda weird. It looks like the function is not found so it doesn't truncate anything. Devel should help you with this for sure. Try to isolate the string you want to truncate.

K,

I got the first part of your last reply, and now I get the second. I didn't realize that all of that outputted views code was not relevant. Still not sure why the code doesn't work in node.tpl.php after using devel. Thanks for all your help. Once I find a solution I will post it.

Status:Active» Fixed

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Thanks a lot, works with me very nice.
here are examples:

<?php
print truncate_utf8($node->content['body']['#value'], 100, TRUE, TRUE);
?>

<?php
print truncate_utf8( check_plain($node->title), 62, TRUE, TRUE);
?>

Hi every body

many thanks for all examples, mostly woody.

Peace.