Get Smarty

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Check reviews of PHP Web Hosting, compatible with Smarty Templates

Chapter 8. Fonctions utilisateur

Smarty est livré avec plusieurs fonctions utilisateurs que vous pouvez appeler dans vos templates.

{assign}

{assign} est utilisé pour déclarer des variables de template durant l'exécution du template.

Nom attribut Type Requis Defaut Description
var chaîne de caractère Oui n/a Le nom de la variable assignée
value chaîne de caractère Oui n/a La valeur assignée

Example 8.1. {assign}


{assign var='name' value='Bob'}

La valeur de $name est {$name}.

  

L'exemple ci-dessus affichera :


La valeur de $name est Bob.

  

Example 8.2. {assign} avec quelques fonctions mathématiques

Cet exemple complexe doit avoir ces variables entre crochets.


{assign var=running_total value=`$running_total+$some_array[loop].some_value`}

  

Example 8.3. Accès aux variables {assign} depuis un script PHP

Pour accéder aux variables {assign} depuis le script PHP, utilisez get_template_vars(). Ci-dessous, le template qui crée la variable $foo.


{assign var='foo' value='Smarty'}

  

Les variables de template ne sont disponibles que après/durant l'exécution du template, comme dans le script ci-dessous.


<?php

// ceci n'affichera rien car le template n'a pas encore été exécuté
echo $smarty->get_template_vars('foo');

// Récupère le template dans une variable
$whole_page = $smarty->fetch('index.tpl');

// Ceci affichera 'smarty' car le template a été exécuté
echo $smarty->get_template_vars('foo');

$smarty->assign('foo','Even smarter');

// Ceci affichera 'Even smarter'
echo $smarty->get_template_vars('foo');

?>

  

Les fonctions suivantes peuvent optionnellement assigner des variables de template.

{capture}, {include}, {include_php}, {insert}, {counter}, {cycle}, {eval}, {fetch}, {math} et {textformat}.

Voir aussi assign() et get_template_vars().

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 5 plus 3? (Are you human?)

Sponsors [info]

Cheap Website Hosting @hostdone.com
Cheap Hosting @HostsVault
email marketing @ActiveCampaign
broadband deals @BroadbandGenie
Best UK Web Hosting @webhost.uk.net
Reliable UK and US Web Hosting @rshosting.com
Cheap US and UK Web Hosting @infrenion.com
I.T Certification Exams @pass-guaranteed.com
Certification Practice Tests @firsttrycertify.com
Certification Practice Exam Questions @ace-it-certifications.com
Broadband Speed Test @broadbandspeed.co.uk
bulk email software @americaint.com