Smarty is a popular templating engine for PHP that allows you to separate the business logic layer and the presentation layer of your application or website. This post discusses what Smarty is, its benefits, and how to use it.
When we create web pages using PHP, they typically contain a mix of HTML and PHP code. That means you can use PHP language within HTML. For this reason, PHP is also called an embedding language. Normally, any webpage containing PHP code needs to be saved with the .php extension so the web server can recognize that it contains PHP code to be run by a separate PHP parser.
You can use both PHP and HTML together on the same page, which is extremely convenient. This is one of the biggest reasons for PHP’s immense popularity. However, in many cases, this benefit can turn into a problem. How?
Imagine there’s a web project with a designer who doesn’t know any programming (PHP) and a PHP programmer who isn’t good at design, though both are skilled in their own fields. If a change or update needs to be made to the page’s PHP code (the business logic layer), the developer will need to hunt for PHP code blocks mixed within the HTML. On the other hand, the designer might want to change the page’s appearance (the presentation layer). This is where the real issue arises. Since the designer doesn’t know programming, they might accidentally or unknowingly break the PHP code. For example, suppose the menu bar was previously on the left, and the designer wants to move it to the right. If the menu code was created in PHP, there’s no way for the designer to know which block of code to move to change the menu’s position.
These types of issues frequently occur in real-world scenarios. To solve these problems, the Smarty engine was developed—it allows designers and developers to work independently without interfering with each other’s work.
Smarty: What is it? Smarty is the most popular and powerful templating engine for PHP. With Smarty, the issues described above can be easily and elegantly solved. In other words, Smarty makes it simple to separate your application’s business logic layer (PHP code) from HTML/design (presentation logic). As a result, team members can smoothly perform their own tasks without disrupting others.
This is Smarty’s main feature, but it’s capable of much more.
Here’s an example: suppose you want to display a table on your webpage where rows 1, 3, 5, 7, and so on have one background color, and rows 2, 4, 6, 8, etc., have another. While this can be done using HTML and CSS, it can be time-consuming, especially if the table is dynamic. But using Smarty, you can do this in just a few lines.
Let’s say you want to encrypt some parts of a page, but the parts to be encrypted are dynamic (they change for different reasons). Here too, Smarty can be a real lifesaver. And there are many more such benefits with Smarty.
Have you ever seen open-source applications like phpBB? If so, you might have noticed hundreds of available templates; yet, using a different template doesn’t require changing anything in the core application. Just specifying the template name is enough. This is possible because templating engines separate the business logic layer from the presentation layer.
Hopefully, this brief discussion has given you a decent understanding of what Smarty does and its advantages. If so, and you want to use Smarty in your application or website, the next question is: How? First, download Smarty from http://smarty.php.net/download.php. Then unzip and install it. Detailed installation instructions can be found at http://smarty.php.net/quick_start.php. Smarty has an excellent manual covering every topic in detail. You can download the manual from http://smarty.php.net/distributions/manual/en/Smarty-2.6.7-docs.chm. There’s also an outstanding book on Smarty by a Bangladeshi programmer, which you can learn about here: http://www.packtpub.com/book/smarty. With these two manuals, Smarty should present no difficulties to anyone. Even so, if you run into any problems, the Smarty forum is always there to help: http://www.phpinsider.com/smarty-forum. So, get started with Smarty and create your own smart website now.
————–
The HungryCoder
http://www.hungrycoder.xenexbd.com/

আপনাকে অনেক ধন্যবাদ এত দারুন এক্তা বেপার শেয়ার করার জন্য।