humaniOra …

Blog tempat corat – coret ,artikel Oracle dan artikel-artikel laennya ..

Smarty … !

with one comment

smartypada awalnya males banget nih belajar smarty , cuman karena tuntutan project yang lagi di kerjain dan juga karena tuntutan dari maintenance system yang sudah ada yang sudah pake smarty sejak doeloe jadinya belajar deh . untuk perta kita perkenalan dulu ..

1. Apa itu Smarty

Smarty adalah mesin template untuk PHP. Lebih khusus, ia memfasilitasi cara yang bisa diatur untuk memisahkan logika aplikasi dan konten dari penampilanny. jadi kalo bikin apliaksi web codinga php sama designnya bisa di kerjain secara terpisa. karena emang di pisah.

2. Beberapa Fitur Smarty

• Sangat cepat.
• Efisien karena pengurai PHP yang mengerjakan pekerjaan beratnya.
• Tidak ada kelebihan penguraian template, hanya sekali mengompilasi.
• Pintar mengenai rekompilasi hanya file template yang telah diubah.
• Anda dapat membuat dengan mudah fungsi kustom dan pengubah variabel, agar bahasa template bisa diperluas secaraekstrim.
• Template bisa mengkonfigurasi sintaks tag {pemisah}, agar anda dapat menggunakan {$foo}, {{$foo}},
<!–{$foo}–>, dll.
• Konstruksi {if}..{elseif}..{else}..{/if} dioper ke pengurai PHP, maka sintaks ekspresi {if…} bisa berupa
evaluasi sesederhana atau serumit yang anda inginkan.
• Membolehkan pengulangan tidak terbatas dari sections, if’s dll.
• Dimungkinkan untuk menyertakan kode PHP langsung dalam file template anda, meskipun ini mungkin tidak
diperlukan (ataupun direkomendasikan) karena mesin dapat dikustomisasi.
• Dukungan built-in caching
• Bebas sumber template
• Fungsi kustom penanganan cache
• Arsitektur Plugin

3. Instalasi Dasar

Instalasi file librari Smarty yang ada dalam sub direktori /libs/ dari distributsi. Ini adalah file .php yang TIDAK BOLEH
diedit. Ia berbagi diantara seluruh aplikasi dan hanya diubah ketika anda meingkatkannya ke versi Smarty baru.
Dalam contoh di bawah ini Smarty tarball telah diuraikan ke:
• /usr/local/lib/Smarty-v.e.r/ untuk mesin *nix
• dan c:\webroot\libs\Smarty-v.e.r\ untuk lingkungan windows.
Teladan 2.1. File librari Smarty yang Diperlukan
Smarty-v.e.r/
libs/
Smarty.class.php
Smarty_Compiler.class.php
Config_File.class.php
debug.tpl
internals/*.php (all of them)
plugins/*.php (all of them)
Smarty menggunakan konstan [http://php.net/define] PHP bernama SMARTY_DIR yang merupakan path file sistem lengkap
ke direktori libs/ Smarty. Pada dasarnya, jika aplikasi anda dapat menemukan file Smarty.class.php, anda tidak perlu
menyetel SMARTY_DIR karena Smarty akan mengetahui dirinya sendiri. Oleh karena itu, jika Smarty.class.php tidak
dalam include_path [http://php.net/ini.core.php#ini.include-path] anda, atau anda tidak menyertakan path absolut kepadanya
dalam aplikasi anda, maka anda harus mendefinisikan SMARTY_DIR secara manual. SMARTY_DIR harus menyertakan
akhiran garis miring/.
Ini adalah bagaimana anda membuat turunan Smarty dalam naskah PHP anda:
<?php
// NOTE: Smarty has a capital ‘S’
require_once(‘Smarty.class.php’);
$smarty = new Smarty();
?>
Coba menjalankan naskah di atas. Jika anda mendapatkan kesalahan yang mengatakan Smarty.class.php file could not
be found, anda perlu melakukan salah satu dari yang berikut:
Teladan 2.2. Setel konstan SMARTY_DIR secara manual
<?php
// *nix style (note capital ‘S’)
define(‘SMARTY_DIR’, ‘/usr/local/lib/Smarty-v.e.r/libs/’);
// windows style
define(‘SMARTY_DIR’, ‘c:/webroot/libs/Smarty-v.e.r/libs/’);
// hack version example that works on both *nix and windows
// Smarty is assumend to be in ‘includes/’ dir under current script
define(‘SMARTY_DIR’,str_replace(“\\”,”/”,getcwd()).’/includes/Smarty-v.e.r/libs/’);
require_once(SMARTY_DIR . ‘Smarty.class.php’);
$smarty = new Smarty();
?>
Teladan 2.3. Sertakan path absolut ke file librari
<?php
// *nix style (note capital ‘S’)
require_once(‘/usr/local/lib/Smarty-v.e.r/libs/Smarty.class.php’);
// windows style
require_once(‘c:/webroot/libs/Smarty-v.e.r/libs/Smarty.class.php’);
$smarty = new Smarty();
?>
Teladan 2.4. Tambah path librari ke file php.ini
;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;
; *nix: “/path1:/path2″
include_path = “.:/usr/share/php:/usr/local/lib/Smarty-v.e.r/libs/”
; Windows: “\path1;\path2″
include_path = “.;c:\php\includes;c:\webroot\libs\Smarty-v.e.r\libs\”
Teladan 2.5. Menambahkan path include dalam naskah PHP dengan ini_set() [http:/ / php.net/
ini-set]
<?php
// *nix
ini_set(‘include_path’, ini_get(‘include_path’).PATH_SEPARATOR.’/usr/local/lib/Smarty-v.e.r/libs/’);
// windows
ini_set(‘include_path’, ini_get(‘include_path’).PATH_SEPARATOR.’c:/webroot/lib/Smarty-v.e.r/libs/’);
?>

Sekarang file librari itu di tempatnya, waktunya menyiapkan direktori Smarty untuk aplikasi anda:
• Smarty memerlukan empat direktori yang secara standar bernama templates/, templates_c/, configs/ dan
cache/
• Setiap dari yang di atas tersebut bisa didefinisikan dengan properti kelas Smarty masing-masing $template_dir,
$compile_dir, $config_dir, dan $cache_dir
• It is highly recommended that you setup a separate set of these directories for each application that will use Smarty
For our installation example, we will be setting up the Smarty environment for a guest book application. We picked an
application only for the purpose of a directory naming convention. You can use the same environment for any application,
just replace guestbook/ with the name of your application.
Teladan 2.6. What the file structure looks like
/usr/local/lib/Smarty-v.e.r/libs/
Smarty.class.php
Smarty_Compiler.class.php
Config_File.class.php
debug.tpl
internals/*.php
plugins/*.php
/web/www.example.com/
guestbook/
templates/
index.tpl
templates_c/
configs/
cache/
htdocs/
index.php
Be sure that you know the location of your web server’s document root as a file path. In the following examples, the
document root is /web/www.example.com/guestbook/htdocs/. The Smarty directories are only accessed by the
Smarty library and never accessed directly by the web browser. Therefore to avoid any security concerns, it is recommended
(but not mandatory) to place these directories outside of the web server’s document root.
You will need as least one file under your document root, and that is the script accessed by the web browser. We will name
our script index.php, and place it in a subdirectory under the document root /htdocs/.
Smarty will need write access (windows users please ignore) to the $compile_dir and $cache_dir directories
(templates_c/ and cache/), so be sure the web server user account can write to them.
Catatan: This is usually user “nobody” and group “nobody”. For OS X users, the default is user “www” and group
“www”. If you are using Apache, you can look in your httpd.conf file to see what user and group are being
used.
Teladan 2.7. Permissions and making directories writable
chown nobody:nobody /web/www.example.com/guestbook/templates_c/
chmod 770 /web/www.example.com/guestbook/templates_c/
chown nobody:nobody /web/www.example.com/guestbook/cache/
chmod 770 /web/www.example.com/guestbook/cache/
Instalasi
Note: chmod 770 will be fairly tight security, it only allows user “nobody” and group “nobody” read/write access
to the directories. If you would like to open up read access to anyone (mostly for your own convenience of viewing
these files), you can use 775 instead.
We need to create the index.tpl file that Smarty will display. This needs to be located in the $template_dir.
Teladan 2.8. /web/www.example.com/guestbook/templates/index.tpl
{* Smarty *}
Hello {$name}, welcome to Smarty!
Technical Note: {* Smarty *} is a template comment. It is not required, but it is good practice to start all your
template files with this comment. It makes the file easy to recognize regardless of the file extension. For example,
text editors could recognize the file and turn on special syntax highlighting.
Now lets edit index.php. We’ll create an instance of Smarty, assign() a template variable and display() the
index.tpl file.
Teladan 2.9. Editing /web/www.example.com/docs/guestbook/index.php
<?php
require_once(SMARTY_DIR . ‘Smarty.class.php’);
$smarty = new Smarty();
$smarty->template_dir = ‘/web/www.example.com/guestbook/templates/’;
$smarty->compile_dir = ‘/web/www.example.com/guestbook/templates_c/’;
$smarty->config_dir = ‘/web/www.example.com/guestbook/configs/’;
$smarty->cache_dir = ‘/web/www.example.com/guestbook/cache/’;
$smarty->assign(‘name’,'Ned’);
//** un-comment the following line to show the debug console
//$smarty->debugging = true;
$smarty->display(‘index.tpl’);
?>

Menurut pengalaman saya sih , kalo bikin apliaksi pake smarty lebih enak, karena kita cuman bikin codingam php , trus kirim variablenya ke templatenya dan dari templatenya kirim lagi ke codingan php. Kalo yang udah biasa coding php tidak perlu waktu lama untuk menyesuaikan diri dengan smarty , paling tinggal buka ebooknya aja. dan tutorial di atas saya copy paste dari ebook “Smarty-2.6.14-indonesia-docs.pdf” saya lupa dari mana … , biar cepet tulis postingannya.

Written by humeora

Oktober 30, 2007 pada 3:48 pm

Ditulis dalam smarty

Satu Tanggapan

Subscribe to comments with RSS.

  1. mampir blogwalking

    sachroel

    November 5, 2007 at 12:09 pm


Tinggalkan Balasan