Sign in Registration
ruen

Separating PHP and HTML code, using templates

php-html-template

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.

Comments (0)
For commenting sign in or register.
Оставить заявку

Latest articles

Popular sections

Eqsash (Tools)

Android app - VK LAST USER ID, отучитель от зависимости и т.д.:
Available on Google Play

Amessage (Communication)

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to

YouTube

Books

IT notes - In simple language about the most necessary things (HTML, CSS, JavaScript, PHP, databases, Drupal, Bitrix, SEO, domains, security and more), PDF, 500 p.