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 4. Variabili

Smarty usa parecchi tipi diversi di variabili. Il tipo di variabile dipende da quale simbolo si usa come prefisso (o come delimitatore).

In Smarty le variabili possono essere visualizzate direttamente oppure usate come argomenti per gli attributi e i modificatori delle funzioni, oppure in espressioni condizionali, ecc. Per stampare una variabile, è sufficiente includerla fra i delimitatori in modo che sia l'unica cosa contenuta fra essi. Esempi:


{$Name}

{$Contacts[row].Phone}

<body bgcolor="{#bgcolor#}">

  

Variabili valorizzate da PHP

Le variabili valorizzate da PHP sono referenziate facendole precedere da un segno di dollaro $. Anche le variabili valorizzate internamente al template con la funzione assign vengono visualizzate in questo modo.

Example 4.1. variabili valorizzate


Hello {$firstname}, glad to see you could make it.
<br />
Your last login was on {$lastLoginDate}.

     

Questo visualizzerà:


Hello Doug, glad to see you could make it.
<br />
Your last login was on January 11th, 2001.

     

Array associativi

Potete fare riferimento ad array associativi valorizzati da PHP specificando l'indice dopo il punto '.'

Example 4.2. accesso ad array associativi


<?php
$smarty = new Smarty;
$smarty->assign('Contacts',
    array('fax' => '555-222-9876',
          'email' => 'zaphod@slartibartfast.com',
          'phone' => array('home' => '555-444-3333',
                           'cell' => '555-111-1234')));
$smarty->display('index.tpl');
?>

    

dove il contenuto di index.tpl è:


{$Contacts.fax}<br />
{$Contacts.email}<br />
{* ovviamente si possono usare anche array multidimensionali *}
{$Contacts.phone.home}<br />
{$Contacts.phone.cell}<br />

    

questo visualizzerà:


555-222-9876<br />
zaphod@slartibartfast.com<br />
555-444-3333<br />
555-111-1234<br />

    

Array con indici numerici

Potete referenziare gli array con il loro indice, come in PHP.

Example 4.3. accesso agli array per indice numerico


<?php

$smarty = new Smarty;
$smarty->assign('Contacts',
    array('555-222-9876',
          'zaphod@slartibartfast.com',
          array('555-444-3333',
                '555-111-1234')));
$smarty->display('index.tpl');

?>

    

dove index.tpl è:


{$Contacts[0]}<br />
{$Contacts[1]}<br />
{* anche qui si possono usare array multidimensionali *}
{$Contacts[2][0]}<br />
{$Contacts[2][1]}<br />

     

Questo visualizzerà:


555-222-9876<br />
zaphod@slartibartfast.com<br />
555-444-3333<br />
555-111-1234<br />

      

Oggetti

Le proprietà di oggetti valorizzate da PHP possono essere referenziate indicando il nome della proprietà dopo il simbolo '->'

Example 4.4. accesso alle proprietà degli oggetti


name: {$person->name}<br />
email: {$person->email}<br />

     

Questo visualizzerà:


name: Zaphod Beeblebrox<br />
email: zaphod@slartibartfast.com<br />

     

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 11 plus 7? (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