Instalasi DasarInstalasi 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:
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 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: Teladan 2-2. Setel konstan SMARTY_DIR secara manual
Teladan 2-3. Sertakan path absolut ke file librari Teladan 2-4. Tambah path librari ke file php.ini Teladan 2-5. Menambahkan path include dalam naskah PHP dengan ini_set() Sekarang file librari itu di tempatnya, waktunya menyiapkan direktori Smarty untuk aplikasi anda:
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.
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/
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!
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 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\'); ?>'); ?>
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! |