TPL is a template file which is a common text file that contains user-defined variables that are entitled to be overridden by user-defined output content when a PHP web application parses the template file. These are used by web applications with server script PHP(but not restricted to) as a template file. Template code is the main content of a .tpl file, which comprises of mainly plain HTML texts tpl language is stored in .tpl files as plain text, so, it’s possible to edit a TPL file with simple text editors.
Situation to use:
While it may seem easy to integrate PHP codes within HTML, it’s not considered a good code management practice as they become difficult for the design team to fairly maintain the files with a mixture of PHP tags and HTML tags. In such a scenario, template codes are used to separate the design from the development. Template files insulate PHP by offering a much simpler tag-based syntax. No PHP knowledge is essential to manage a template file. It is commonly more essential to web designers than PHP developers.
Few common areas where template files are added
- Re-usability of code where the same design is shared between 3rd parties. Example – An online course portal where colleges make their own page to put their own courses.
- In Content Management Systems or CMS .
- A project where web designing team and the PHP development teams are different.
Procedure to use: To use web design with a template with PHP, we will use a framework called Smarty. Smarty is an open-source free framework that comes with simple tag-based template statements to provide clean maintainable presentation codes for a PHP web application.
Steps to get started with Smarty in Apache.
- Download the latest Smarty from https://github.com/smarty-php/smarty/archive/master.zip.
- Unzip the file and add two folders template_c and cache.
- Configure the php.ini file by adding include_path=”.;path to where the libs folder is present in step 3″.
- Make a folder in htdocs(root) named smarty and copy the folder configs from the original downloaded folder in step 3.
- Add another folder named templates and a PHP file index.php.
- Within templates folder add the template file index.tpl
Hence, in templates/index.tpl one can write all his template codes to handle presentations and in index.php all the php codes to handle data processing.
Example: We are demonstrating here a simple example to display data from a PHP file.
// create object
// assign some content. This would typically come from
// a database or other source, but we'll use static
// display it
Advantages of using .tpl file:
- Flexibility for custom development.
- Clean code presentation.
- Fast development or deployment for the programmers and the designers.
- Quick and easy to maintain.
- Easy re-usability of code
- Security: insulation from PHP.
Practical Application of template files with PHP: A practical example of .tpl file in PHP is the template files used by Phorum. It is a PHP and MySQL-based open source message board system. The .tpl files display data which is dynamically generated by Phorum, such as information on messages, search-related results, and strings of private messages.
The .tpl files have a specific format of template language which generally comprises HTML and simple Phorum template statements. Four data types are supported by these statements, which may be integers, strings, PHP constants, amd template variables.
Importance of .tpl file with PHP in Phorum: The Phorum uses .php files if they are placed in the template directory and are named with the appropriate basename besides using TPL files for storing simple template code. A TPL file comprises mainly style data and other information regarding the web pages of the PHP application.
If Phorum is displaying the header footer template, it first searches for header.php in the template directory and if the file does not exist, will then look for header.tpl. It’s not about just Phorum. Many other parsers and more often custom solutions use .tpl. Also if custom, one can place PHP codes inside the .tpl files too as they are quite robust. OpenCart is a good example of this and vBulletin, where inside the .tpl one can see use PHP codes inside it. That is the reason NGINX and many other servers come pre-built with precautions that prevent people from reading the .tpl files .
- How to reset selected file with input tag file type in Angular 9?
- How to get file input by selected file name without path using jQuery ?
- User Experience or UX Design
- Design first Application using Express
- HTML | Design Form
- Difference between MVC and MVT design patterns
- 8 Ways to Shipwreck Your Next Website Design
- How to Design Color Picker using jQuery UI ?
- UI vs UX Design : Which Career Option Should You Choose?
- 6 Best CSS frameworks You should Know to design Attractive Websites
- OOPs | Object Oriented Design
- Introduction of Control Unit and its Design
- Design and Implement Calculator using jQuery
- How to Design Image Slider using jQuery ?
- Post/Redirect/Get (PRG) Design Pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.