PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.smarty.general > assign template var with number of elements of an array
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
assign template var with number of elements of an array

Réponse
 
LinkBack Outils de la discussion
Vieux 20/10/2006, 20h40   #1
Nuno Mendes
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut assign template var with number of elements of an array

Hi,

I'm trying to create a variable inside a template (banners_count) and
it's value needs to be the number of elements of an array. I am using
this but it's not working.

{assign var=banners_count value=count($banners)}

How can it be done?

Thanks!

Nuno
  Réponse avec citation
Vieux 20/10/2006, 22h03   #2
Ken Snyder
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] assign template var with number of elements of an array

Hello Nuno,

From what I understand, developers are encouraged to assign counts
beforehand, but I have found myself in the same situation as you.

You are correct that some functions like count can be used ***within if
statements*** or ***as modifiers*** if they are allowed within Smarty's
security_settings property (see
http://smarty.php.net/manual/en/vari...y.settings.php).

I use a version of the following plugin when I need a count within a
template. Use: {sizeof item=$banners assign=banners_count}

--Ken

<?php
// function.sizeof.php
function smarty_function_sizeof($params, &$smarty)
{
$mixed = $params['item'];
$assign = isset($params['assign']) ? (string)$params['assign'] : '';

if( is_array($mixed) || is_object($mixed) )
$out = count($mixed);

if( $assign )
$smarty->assign($assign,$out);
else
return $out;
}



Nuno Mendes wrote:
> Hi,
>
> I'm trying to create a variable inside a template (banners_count) and
> it's value needs to be the number of elements of an array. I am using
> this but it's not working.
>
> {assign var=banners_count value=count($banners)}
>
> How can it be done?
>
> Thanks!
>
> Nuno
>



  Réponse avec citation
Vieux 20/10/2006, 22h08   #3
Owen Cole
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [SMARTY] assign template var with number of elements of an array

Hello Nuno,

You can use php functions as modifiers in Smarty. Also since this is an
array you need to use the @ syntax to work on the whole array.

If you are just trying to display the count:

{$banners|@count}

If you really need to assign it to a variable for some other purpose:

{assign var="banners_count" value=$banners|@count}

Hope this s!

Nuno Mendes wrote:
> Hi,
>
> I'm trying to create a variable inside a template (banners_count) and
> it's value needs to be the number of elements of an array. I am using
> this but it's not working.
>
> {assign var=banners_count value=count($banners)}
>
> How can it be done?
>
> Thanks!
>
> Nuno
>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 21h06.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,07804 seconds with 11 queries