We will report in this article the steps to install Smarty on a local server on Windows. For those who do not know, Smarty is a template system for PHP, we can separate the presentation (layout) of the code (programming), ie, separating the business logic created by scripts in PHP, HTML used to display information on the website. Thus, we have the PHP scripts that perform programmers, independently of the presentation made by designers. Smarty also offers many other advantages, such as caching generated pages, which can increase server performance.
Steps for installing Smarty
The first step is to enter the page Smarty to download the latest version of the product.We will have to unzip the downloaded file and then place it in any directory where you want, provided it is outside the directory publishing pages, bone, outside the document root. In my case, I have installed WAMP (Apache + package providing PHP + MySQL, with simple installation), I put Smarty in the Wamp installation directory but not in the directory "www", which is the directory publishing the pages.The directory where I unpacked file is C: wampsmarty
Now we have to add in the PHP include_path path of the directory "libs" is in the directory where we put files Smarty (include_path directive, which is in the PHP php.ini indicates the directories where you will find the files to be included in PHP include function both as to require). To edit the php.ini, the first thing we have to do is locate it on your hard drive.
In the php.ini have to search the include_path directive above. The best thing is to look at the file with a text search tool.Then you have to modify it to add the directory where you installed Smarty libs. For example, you may get something like this:
include_path = ".; C: wampsmartylibs"
Once you have edited and saved php.ini, you have to restart your web server for the changes made in the include_path to take effect.
Now you have to create multiple directories using Smarty to store the source code templates, templates processed and the cache. There are 4 directories:
On the one hand we have two folders that must be stored somewhere on the server publishing directory. My publishing directory is: C: wampwww then, these two directories would be:
C: wampwwwsmartytemplates (This is the directory where you saved the templates)
C: wampwww smartyconfigs (This directory settings are saved Smarty)
On the other hand we have two files that should be kept out of our publishing directory.
C: wampsmartytemplates_c (in this directory are saved Smarty compiled templates)
C: wampsmartycache (in this directory cache stores the pages generated by Smarty)
Test the installation of Smarty
Once we have finished installing and configuring the system, we can do a test to see if everything works fine.To do this we must create two files: 1) a PHP file that uses the template and 2) the code itself to generate a Smarty template page.First we will create the PHP file with the code needed to load, configure and display the template. This file can be placed anywhere in our directory publication. The PHP code is as follows:
<? Php
/ / Load the smarty library
require ('Smarty.class.php');
$ Smarty = new Smarty;
$ Smarty-> template_dir = 'C: wampwwwsmartytemplates';
$ Smarty-> config_dir = 'C: wampwwwsmartyconfig';
$ Smarty-> cache_dir = 'C: wampsmartycache';
$ Smarty-> compile_dir = 'C: wampsmartytemplates_c';
/ / Assign values to customize the template to replace the variables in the same
$ Smarty-> assign ('name', 'DesarrolloWeb.com');
$ Smarty-> assign ('title', 'Title of the page that I put in PHP to customize!');
$ Smarty-> display ('index.tpl');
?>
As we can see, using a Smarty template, called "index.tpl" (the name of the template you are using can be seen in the line of code $ smarty-> display ('index.tpl');) , we also have to create and save in the directory you have created to store the templates C: wampwwwsmartytemplates.
<html>
<body>
<h1>} {$ title </ h1>
Hello {$ name}!
</ Body>
</ Html>
To see if everything is working perfectly we must open the file where we put the first code PHP. I kept it in the root directory of the publication and the name "pruebosmarty.php." Then to open it through the web server, I have access to the following URL:
http://localhost/pruebosmarty.php
Troubleshooting the installation of Smarty
I can think of two errors that you can go.The first is that an error will include Smarty.class.php. If you encounter this error, you may not have edited the php.ini correctly, setting the include_path in the correct path of the directory "libs" Smarty files. Remember to restart the server whenever you make changes to the php.ini and the correct way to locate your php.ini on your hard drive (through the execution of the phpinfo ()).Another mistake is that you can leave the system has not located the template you have to open. In this case check the line where you specify the directory templates in PHP code:
$ Smarty-> template_dir = 'C: wampwwwsmartytemplates';
And check that this directory is the template you're working "index.tpl".
I hope this helps, inspired Smarty's own documentation and expanded with additional explanations, drawn from the tests performed by desarrolloweb. Com, you can install the Smarty templating system for PHP.
0 comments:
Post a Comment