|
|

|
Sumber daya
Plugin sumber daya diartikan sebagai cara umum atas penyediaan sumber
template atau komponen naskah PHP untuk Smarty. Beberapa contoh
sumber daya:
database, LDAP, memori berbagi, soket, dan seterusnya.
Ada empat fungsi yang perlu didaftarkan untuk setiap tipe sumber daya.
Setiap fungsi akan menerima sumber daya yang diminta sebagai paramneter
pertama dan obyek Smarty sebagai parameter terkahir. Parameter sisanya
tergantung pada fungsi.
bool smarty_resource_name_source (string $rsrc_name, string &$source, object &$smarty)
bool smarty_resource_name_timestamp (string $rsrc_name, int &$timestamp, object &$smarty)
bool smarty_resource_name_secure (string $rsrc_name, object &$smarty)
bool smarty_resource_name_trusted (string $rsrc_name, object &$smarty)
Fungsi pertama, source() is supposed to retrieve
the resource. Its second parameter $source is a
variable passed by reference where the result should be
stored. The function is supposed to return TRUE if
it was able to successfully retrieve the resource and FALSE otherwise.
Fungsi kedua, timestamp() is supposed to
retrieve the last modification time of the requested resource, as a UNIX
timestamp. The second parameter $timestamp
is a variable passed by reference where the timestamp should be stored.
The function is supposed to return TRUE if the timestamp could be
succesfully determined, or FALSE otherwise.
Fungsi ketiga, secure()is supposed to return
TRUE or FALSE, depending on whether the requested resource is secure
or not. This function is used only for template resources but
should still be defined.
Fungsi keempat, trusted() seharusnya mengembalikan
TRUE atau FALSE, tergantung pada apakah sumber daya yang diminta
dipercaya atau tidak. Fungsi ini dipakai hanya untuk komponen naskah PHP
yang diminta oleh tag
{include_php} atau tag
{insert}
dengan atribut src. Akan tetapi, ini masih harus
didefinisikan meskipun untuk sumber daya template.
Teladan 16-10. plugin sumber daya
<?php /* * Smarty plugin * ------------------------------------------------------------- * File: resource.db.php * Tipe: sumber daya * Nama: db * Kegunaan: Mengambil template dari database * ------------------------------------------------------------- */ function smarty_resource_db_source($tpl_name, &$tpl_source, &$smarty) { // lakukan pemanggilan database di sini untuk mengambil template anda, // mempopulasikan $tpl_source $sql = new SQL; $sql->query("select tpl_source from my_table where tpl_name='$tpl_name'"); if ($sql->num_rows) { $tpl_source = $sql->record['tpl_source']; return true; } else { return false; } }
function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, &$smarty) { // lakukan pemanggilan database di sini untuk mempopulasikan $tpl_timestamp. $sql = new SQL; $sql->query("select tpl_timestamp from my_table where tpl_name='$tpl_name'"); if ($sql->num_rows) { $tpl_timestamp = $sql->record['tpl_timestamp']; return true; } else { return false; } }
function smarty_resource_db_secure($tpl_name, &$smarty) { // menganggap semua template aman return true; }
function smarty_resource_db_trusted($tpl_name, &$smarty) { // tidak dipakai untuk template } ?>
|
|
LIhat juga
register_resource(),
unregister_resource().
|
|
|