Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Secure Internet File-Sharing with PHP, MySQL, and JavaScript : Page 4

Use PHP to create a secure Internet file-sharing application that employs a database abstraction layer and separates HTML design from PHP programming.


advertisement
What Smarty Is and How It Works
The technical description of Smarty is a template engine. Basically, it is a framework that helps developers easily maintain a standard site layout and separate logic from presentation. While many online discussion forums hold debates about whether or not Smarty is a valid minimum implementation of the MVC paradigm, I consider it a loophole for less experienced developers to achieve the separation between code and presentation.

Smarty itself is a programming language with basic syntax—some of the above-mentioned discussions are just about the need for a presentation engine to have its own logic. My opinion is that a business logic and a presentation logic both exist, and you might as well acknowledge both even if it is not a good idea to abuse the latter. In fact, PHP code has to deal as much as possible with the final variable assignments, and ultimately Smarty templates must remain HTML files.

To install Smarty, you must create two directories (templates and configs). Normally, you should place these two folders outside the web server document root, but in this example I put them under the application root (smarty/templates and smarty/configs). In the smarty/templates directory, I collect all Smarty files with .tpl extensions. After all, Smarty files are just HTML files whose content is assigned and passed by PHP code.



As previously stated, Smarty also has a basic syntax that is useful for lightly managing presentation logic. One useful construction is {include ...}, which allows you to embed common parts of templates in a master one. I adopted the same naming schema for PHP files, using the suffix inc for files representing partial parts of HTML pages. This modular mechanism requires you to write down page layouts before starting to write Smarty/HTML templates. For example, the schema in Figure 3 is the one the example uses for the pages files.all.tpl and files.tag.tpl. The organization and naming rules of the Smarty files reflect this schema.

Figure 3. Templates Layout Schema: The organization and naming rules of the Smarty files reflect this schema.

To see if Smarty really works, let's analyze the passing of an indexed array of associative arrays from PHP to Smarty. In particular, the function files in inc.util.php deal with the listing of files stored in the database. In the line $file_array=db_query($dsn,$sql), $file_array is the indexed array returned by SQL query; every element is a row that is an associative array with keys equal to the query fields. A bit further in the code, you will find lines that fill the Smarty structure. One of these is:

$smarty->assign('file_array', $file_array);

At the end, you find:

$smarty->display('files.all.tpl');

These instructions transfer to Smarty the control that renders HTML pages according to the rules in file files.all.tpl. This file embeds inc.file_list.tpl, where you will find the following lines:

<ol class="core"> {foreach item=file from=$file_array name=file_loop} <li class="core_left"> {if isset($file.protection)} ... <input class="core" type="checkbox" name="select-{$file.id}" value="yes" disabled="disabled"/> ...

You can easily identify some syntax constructions, such as {foreach ... }, that cycle on the indexed arrays. You also can see the way to refer to elements in associative arrays: {$file.protection} corresponds to $file['protection'], and {$file.id} corresponds to $file['id'].



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap