Jump to content

How to truncate title by # of characters, instead of # of words?


Recommended Posts

I have been modifying the truncate variable in homefeatured.tpl:

 {$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}  



It works most of the time, but occasionally it's not enough:

http://i48.tinypic.com/rap3lc.jpg


I think that truncating based on characters and not words might help with this issue, so I'm interested in experimenting.

EXAMPLE:

FULL TITLE: "LEVIS JEANS LARGE SIZE BLUE COLOUR"

CURRENTLY TRUNCATED LIKE: "LEVIS JEANS LARGE..."

WHAT I WOULD LIKE TO EXPERIMENT WITH: "LEVIS JEANS LA..."

Any ideas how I do this?

Link to comment
Share on other sites

You can use code like the following:

<a href="{$product.link}" title="{$product.name|substr:0:32|escape:'htmlall':'UTF-8'|trim}{if $product.name|strlen > 32}...{/if}">{$product.name|substr:0:27|escape:'htmlall':'UTF-8'|trim}{if $product.name|strlen > 27}...{/if}



This code will truncate the product title to 32 characters and link text to 27 characters, then trim any spaces, then add '...' on the end.

Link to comment
Share on other sites

Thanks Rocky, is there an IF tag missing from the front of that?

I got this error:

Fatal error: Smarty error: [in /home/testsite/public_html/modules/homefeatured/homefeatured.tpl line 25]: syntax error: unclosed tag \{foreach} (opened line 24). (Smarty_Compiler.class.php, line 317) in /home/testsite/public_html/tools/smarty/Smarty.class.php on line 1095

Link to comment
Share on other sites

That did the trick, thanks again Rocky!

To everyone else - I didn't know how this would look...but I just tested it with 20 products on my front page and it looks fine (I was worried truncating words in half might look unusual but nothing to worry about)

Link to comment
Share on other sites

Hi Rocky I also want to apply this to the "in same category" slide bar (productscategory.tpl).

I think I would need to change:

getProductLink($categoryProduct.id_product, $categoryProduct.link_rewrite, $categoryProduct.category)}" title="{$categoryProduct.name|htmlspecialchars}">
           {$categoryProduct.name|truncate:15:'...'|escape:'htmlall':'UTF-8'}




To:

getProductLink($categoryProduct.id_product, $categoryProduct.link_rewrite, $categoryProduct.category)}" title="{$categoryProduct.name|substr:0:32|escape:'htmlall':'UTF-8'|trim}"{if $categoryProduct.name|strlen > 32}...{/if}">{$categoryProduct.name|substr:0:22|escape:'htmlall':'UTF-8'|trim}{if $categoryProduct.name|strlen > 22}...{/if}



Does that look right?

Link to comment
Share on other sites

UPDATE: Oh it's working now, even a shift + refresh didn't seem to work. I had to navigate out of the page then back in again!



This is really unusual, I went to clean up the code and add some comments and now it doesn't work! Any idea's what's gone wrong? Code remains as above

Link to comment
Share on other sites

×
×
  • Create New...