{eval}
{eval} dipakai untuk mengevaluasi sebuah variabel
sebagai template.
Ini bisa dgunakan untuk hal seperti menyertakan tag/variabel template
ke dalam variabel atau tag/variabel template ke dalam file config.
Jika anda menyertakan atribut assign, output dari
fungsi {eval} akan ditempatkan ke variabel template
ini daripada ke template.
Catatan Teknis:
Variabel yang dievaluasi diperlakukan sama seperti template. Mengikuti
fitur pengeluaran dan keamanan yang sama seolah-olah sebuah template.
Variabel yang dievaluasi dikompilasi setiap kali permintaan, versi
terkompilasi tidak disimpan! Akan tetapi jika anda menghidupkan
caching, output akan di-cache
dengan sisa template.
Teladan 8-6. {eval} Isi dari file config, setup.conf. emphstart = <strong>
emphend = </strong>
title = Welcome to {$company}'s home page!
ErrorCity = You must supply a {#emphstart#}city{#emphend#}.
ErrorState = You must supply a {#emphstart#}state{#emphend#}. |
Di mana template adalah:
{config_load file='setup.conf'}
{eval var=$foo}
{eval var=#title#}
{eval var=#ErrorCity#}
{eval var=#ErrorState# assign='state_error'}
{$state_error} |
Template di atas akan menampilkan:
This is the contents of foo.
Welcome to Foobar Pub & Grill's home page!
You must supply a <strong>city</strong>.
You must supply a <strong>state</strong>. |
|
Teladan 8-7. Contoh {eval} lainnya Ini menampilkan nama server (in uppercase) dan IP. Variabel yang
ditempati $str berasal dari query database.
<?php $str = 'The server name is {$smarty.server.SERVER_NAME|upper} ' .'at {$smarty.server.SERVER_ADDR}'; $smarty->assign('foo',$str); ?>
|
Di mana template adalah:
|