Memulai Apa itu Smarty? Smarty adalah mesin template untuk PHP. Lebih khusus, ia memfasilitasi cara yang bisa diatur untuk memisahkan logika aplikasi dan konten dari penampilannya. Ini jauh lebih baik dijelaskan dalam situasi di mana pemrogram aplikasi dan desainer template memainkan aturan yang berbeda, atau secara umum bukan orang yang sama. Sebagai contoh, katakanlah anda sedang membuat halaman web yang menampilkan artikel koran. Artikel $headline, $tagline, $author dan $body adalah elemen konten, tidak berisi informasi mengenai bagaimana akan ditampilkan. Ia akan dioper ke dalam Smarty oleh aplikasi. Kemudian desainer template mengedit template dan menggunakan kombinasi tag HTML dan tag template untuk membentuk presentasi terhadap variabel ini dengan elemen seperti tabel, div, warna latar belakang, ukuran font, style sheets, svg dll. Suatu hari pemrogram perlu mengubah cara konten artikel diambil (perubahan dalam logika aplikasi). Perubahan ini tidak mempengaruhi desainer template, konten masih akan muncul dalam template persis sama. Demikian juga jika desainer template ingin mendesain ulang template seutuhnya, ini tidak memerlukan perubahan logika aplikasi. Oleh karena itu, pemrogram dapat membuat perubahan terhadap logika aplikasi tanpa perlu merestrukturisasi template, dan desainer template bisa membuat perubahan terhadap template tanpa membongkar logika aplikasi. Satu tujuan desain Smarty adalah pemisahan logika bisnis dan logika presentasi. Ini berarti template tentu saja dapat berisi logika di bawah kondisi yang hanya untuk presentasi saja. Hal seperti menyertakan template lain, memilih warna baris tabel, membesarkan huruf variabel, mengulang terus sebuah data array dan menampilkannya adalah contoh dari logika presentasi. Ini tidak berarti bahwa Smarty memaksa pemisahan logika bisnis dan presentasi. Smarty tidak mengetahui yang mana adalah yang mana, maka menempatkan logika bisnis dalam template adalah anda sendiri yang melakukannya. Juga, jika anda menginginkan tidak ada logika dalam template, anda tentunya dapat melakukannya dengan menetapkan konten cukup ke teks dan variabel saja. Salah satu aspek unik mengenai Smarty adalah kompilasi template. Ini berartu Smarty membaca file template dan membuat naskah PHP darinya. Sekali dibuat, selanjutnya ia dieksekusi darinya. Oleh karenanya tidak ada beban menguraikan file template untuk setiap permintaan, dan setiap template dapat memanfaatkan solusi cache kompilator PHP seperti eAccelerator, ionCube mmCache atau Zend Accelerator adalah beberapa diantaranya. 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 secara ekstrim. 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 Instalasi Persyaratan Smarty membutuhkan server web yang menjalankan PHP 4.0.6 atau lebih tinggi. 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. File librari Smarty yang Diperlukan Smarty menggunakan konstan 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 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: ]]> 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: Setel konstan SMARTY_DIR secara manual ]]> Sertakan path absolut ke file librari ]]> Tambah path librari ke file <filename>php.ini</filename> Menambahkan path include dalam naskah PHP dengan <literal><ulink url="&url.php-manual;ini-set">ini_set()</ulink></literal> ]]> 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. What the file structure looks like 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. 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. Permissions and making directories writable 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. /web/www.example.com/guestbook/templates/index.tpl 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. Editing /web/www.example.com/docs/guestbook/index.php 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'); ?> ]]> Note In our example, we are setting absolute paths to all of the Smarty directories. If /web/www.example.com/guestbook/ is within your PHP include_path, then these settings are not necessary. However, it is more efficient and (from experience) less error-prone to set them to absolute paths. This ensures that Smarty is getting files from the directories you intended. Now naviagate to the index.php file with the web browser. You should see "Hello Ned, welcome to Smarty!" You have completed the basic setup for Smarty! Extended Setup This is a continuation of the basic installation, please read that first! A slightly more flexible way to setup Smarty is to extend the class and initialize your Smarty environment. So instead of repeatedly setting directory paths, assigning the same vars, etc., we can do that in one place. Lets create a new directory /php/includes/guestbook/ and make a new file called setup.php. In our example environment, /php/includes is in our include_path. Be sure you set this up too, or use absolute file paths. /php/includes/guestbook/setup.php Smarty(); $this->template_dir = '/web/www.example.com/guestbook/templates/'; $this->compile_dir = '/web/www.example.com/guestbook/templates_c/'; $this->config_dir = '/web/www.example.com/guestbook/configs/'; $this->cache_dir = '/web/www.example.com/guestbook/cache/'; $this->caching = true; $this->assign('app_name', 'Guest Book'); } } ?> ]]> Now lets alter the index.php file to use setup.php: /web/www.example.com/guestbook/htdocs/index.php assign('name','Ned'); $smarty->display('index.tpl'); ?> ]]> Now you see it is quite simple to bring up an instance of Smarty, just use Smarty_GuestBook() which automatically initializes everything for our application.