PDA

View Full Version : how to use substr() for {$scat.TITLE}


Yury
07-24-2005, 01:57 PM
how can i use php substr() in the main.tpl file?

if the $scat.TITLE is longer then ~50 letters
Content visible to registered users only.

yktan
07-24-2005, 02:28 PM
Hi Yury, you can use Smart templates' {php}{/php} tag to write php code. In your case, you can do something like this:

{* Display subcategories *}
<p class="subcats">
{if $cat.SUBCATS}
{foreach from=$cat.SUBCATS item=scat name=scategs}
<a class="small" href="{if $smarty.const.ENABLE_REWRITE}{$cat.TITLE_URL}/{$scat.TITLE_URL}/{else}index.php?c={$scat.ID}{/if}">
{php}
global $scat;
if (strlen($scat.TITLE) > 50){
// Reset title to 50 characters with ... trailing
$scat.TITLE = substr($scat.TITLE,0,50) . "...";
}
{/php}
{$scat.TITLE}</a>, {/foreach} ...
{/if}
</p>


Note: This code is not tested, just giving you an idea on what you can do.

Another way is to do it in the php script instead of the template or even the SQL select statement (using the SUBSTRING method in the select statement).

Yury
07-24-2005, 02:55 PM
i have the same idea but it wont work :/

i became a parse error at this line
Content visible to registered users only.

yktan
07-24-2005, 04:56 PM
Hi Yury, I haven't actually tried the code yet but can you post the error that you got?

Regards,
YK

Ap0s7le
07-24-2005, 07:28 PM
Actually, that's a lot of wasted time.

Do this instead.

Content visible to registered users only.

later

Yury
07-25-2005, 05:02 PM
thx @ Ap0s7le

this work fine :)

Ap0s7le
07-25-2005, 07:08 PM
Not a problem

later

jensos
08-09-2007, 10:29 AM
Is it possible to trucate by words and not by chars? So that it will display the first 50 words for example?

James
08-09-2007, 08:46 PM
it tries to do so to make sure use it like this :

truncate:50:"...":false