| 1 | This is a simple guide to get Smarty setup and running quickly. The online |
|---|
| 2 | documentation includes a very thorough explanation of a Smarty installation. |
|---|
| 3 | This guide is meant to be a quick and painless way of getting Smarty working, |
|---|
| 4 | and nothing more. The guide assumes you are familiar with the UNIX system |
|---|
| 5 | environment. Windows users will need to make adjustments where necessary. |
|---|
| 6 | |
|---|
| 7 | INSTALL SMARTY LIBRARY FILES |
|---|
| 8 | |
|---|
| 9 | Copy the Smarty library files to your system. In our example, we place them in |
|---|
| 10 | /usr/local/lib/php/Smarty/ |
|---|
| 11 | |
|---|
| 12 | $> cd YOUR_DOWNLOAD_DIRECTORY |
|---|
| 13 | $> gtar -ztvf Smarty-2.6.7.tar.gz |
|---|
| 14 | $> mkdir /usr/local/lib/php/Smarty |
|---|
| 15 | $> cp -r Smarty-2.6.7/libs/* /usr/local/lib/php/Smarty |
|---|
| 16 | |
|---|
| 17 | You should now have the following file structure: |
|---|
| 18 | |
|---|
| 19 | /usr/local/lib/php/Smarty/ |
|---|
| 20 | Config_File.class.php |
|---|
| 21 | debug.tpl |
|---|
| 22 | internals/ |
|---|
| 23 | plugins/ |
|---|
| 24 | Smarty.class.php |
|---|
| 25 | Smarty_Compiler.class.php |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | SETUP SMARTY DIRECTORIES |
|---|
| 29 | |
|---|
| 30 | You will need four directories setup for Smarty to work. These files are for |
|---|
| 31 | templates, compiled templates, cached templates and config files. You may or |
|---|
| 32 | may not use caching or config files, but it is a good idea to set them up |
|---|
| 33 | anyways. It is also recommended to place them outside of the web server |
|---|
| 34 | document root. The web server PHP user will need write access to the cache and |
|---|
| 35 | compile directories as well. |
|---|
| 36 | |
|---|
| 37 | In our example, the document root is /web/www.domain.com/docs and the |
|---|
| 38 | web server username is "nobody". We will keep our Smarty files under |
|---|
| 39 | /web/www.domain.com/smarty |
|---|
| 40 | |
|---|
| 41 | $> cd /web/www.domain.com |
|---|
| 42 | $> mkdir smarty |
|---|
| 43 | $> mkdir smarty/templates |
|---|
| 44 | $> mkdir smarty/templates_c |
|---|
| 45 | $> mkdir smarty/cache |
|---|
| 46 | $> mkdir smarty/configs |
|---|
| 47 | $> chown nobody:nobody smarty/templates_c |
|---|
| 48 | $> chown nobody:nobody smarty/cache |
|---|
| 49 | $> chmod 775 smarty/templates_c |
|---|
| 50 | $> chmod 775 smarty/cache |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | SETUP SMARTY PHP SCRIPTS |
|---|
| 54 | |
|---|
| 55 | Now we setup our application in the document root: |
|---|
| 56 | |
|---|
| 57 | $> cd /web/www.domain.com/docs |
|---|
| 58 | $> mkdir myapp |
|---|
| 59 | $> cd myapp |
|---|
| 60 | $> vi index.php |
|---|
| 61 | |
|---|
| 62 | Edit the index.php file to look like the following: |
|---|
| 63 | |
|---|
| 64 | <?php |
|---|
| 65 | |
|---|
| 66 | // put full path to Smarty.class.php |
|---|
| 67 | require('/usr/local/lib/php/Smarty/Smarty.class.php'); |
|---|
| 68 | $smarty = new Smarty(); |
|---|
| 69 | |
|---|
| 70 | $smarty->template_dir = '/web/www.domain.com/smarty/templates'; |
|---|
| 71 | $smarty->compile_dir = '/web/www.domain.com/smarty/templates_c'; |
|---|
| 72 | $smarty->cache_dir = '/web/www.domain.com/smarty/cache'; |
|---|
| 73 | $smarty->config_dir = '/web/www.domain.com/smarty/configs'; |
|---|
| 74 | |
|---|
| 75 | $smarty->assign('name', 'Ned'); |
|---|
| 76 | $smarty->display('index.tpl'); |
|---|
| 77 | |
|---|
| 78 | ?> |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | SETUP SMARTY TEMPLATE |
|---|
| 82 | |
|---|
| 83 | $> vi /web/www.domain.com/smarty/templates/index.tpl |
|---|
| 84 | |
|---|
| 85 | Edit the index.tpl file with the following: |
|---|
| 86 | |
|---|
| 87 | <html> |
|---|
| 88 | <head> |
|---|
| 89 | <title>Smarty</title> |
|---|
| 90 | </head> |
|---|
| 91 | <body> |
|---|
| 92 | Hello, {$name}! |
|---|
| 93 | </body> |
|---|
| 94 | </html> |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | Now go to your new application through the web browser, |
|---|
| 99 | http://www.domain.com/myapp/index.php in our example. You should see the text |
|---|
| 100 | "Hello Ned!" in your browser. |
|---|
| 101 | |
|---|
| 102 | Once you get this far, you can continue on to the Smarty Crash Course to learn |
|---|
| 103 | a few more simple things, or on to the documentation to learn it all. |
|---|