Separating PHP and HTML code, using templates
When Writing PHP Code , you may need to create an HTML element in your code. Novice developers write HTML code directly in the file with PHP code, simply enclosing it in quotes and replacing variable data in it. Of course, this method is simple, it allows you not to be distracted by additional work on separating PHP and HTML. But this is wrong, you need to put all HTML code into separate files - HTML templates.
Why is it necessary to separate HTML from PHP ? There may be several reasons:
- mixing HTML and PHP code in one file looks ugly;
- no HTML syntax highlighting, PHP code is cluttered - after all, HTML code usually takes up a lot of space;
- HTML code cannot be reused elsewhere in the web application;
- when it will be necessary to make changes to HTML, you will have to search for this piece of code in the file with PHP code for a long time.
To date, various template engines have been developed to accomplish this task, which are just created to separate the code from the layout. But there is not always a desire to use something third-party, and besides, using PHP you can implement a simple templating system yourself. How to do it?
For example, let's create a file with the extension tpl , which will store the HTML code. This will be our template, you can make any file extension, tpl here just for an example. Accordingly, in the code editor, you can set HTML syntax highlighting for this extension (for example, in Notepad ++). The necessary variables are written in this file, which will need to be replaced with the required data. Example template:
& lt; div>
& lt; span> & lt;? = $ variable?> & lt; / span>
& lt; span> & lt;? = $ variable2?> & lt; / span>
& lt; / div>
Next, we create some function, for example - myfunction , which contains data and the path to the template:
function myfunction () {
$ template_path = '/templates/template.tpl';
$ data = array (
'variable' => 'Value',
'variable2' => 'Value 2',
);
$ html = get_html ($ template_path, $ data);
echo $ html;
}
Finally, you need to create a get_html function that will do the work of connecting the specified template and replacing the data in it. The function takes two parameters: the path to the template and the data. The loop traverses the entire contents of the data array and creates variables. Then buffering is turned on and a template is included in which the above-created variables are used. Finally, we stop the buffering process and return the finished HTML code. Sample code is below:
function get_html ($ template_path, $ data) {
foreach ($ data as $ key => $ value) {
$$ key = $ value;
}
ob_start ();
require ($ template_path);
$ html = ob_get_clean ();
return $ html;
}
Thus, it is not difficult to separate HTML from PHP code without using third-party template engines. Using templates will make project development more convenient and correct.
Latest articles
- 03.04.24IT / Уроки PHP Уроки простыми словами. Урок 3. Все операторы PHP с примерами, с выводом работы кода на экран.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 2. Типы данных в PHP с примерами.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 1. Коротко о языке веб-программирования PHP. Основы синтаксиса.
- 09.11.23IT / Database Errors when migrating from MySQL 5.6 to 5.7 and how to fix them - database dump import failed with an error or INSERT does not work. Disabling STRICT_TRANS_TABLES strict mode or using IGNORE
- 08.07.22IT / Misc Convert office files DOC, DOCX, DOCM, RTF to DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT formats without loss and markup changes