📜 ⬆️ ⬇️

Template Designer: Subversion + DOM-parser in action

“Fucking alarm clock!” Hacker cursed into the pillow and, without raising his head, reached out with his hand to the table next to him. The empty beer bottles standing on it rolled with a crash onto the floor while Hacker's hand felt the space on the table, trying to get this terrible device, but in vain.

Rising and straightening out the alarm clock, sitting on the edge of the bed, Hacker looked in the mirror hanging opposite. You look so mean, he thought.

Today, he must express his views on the template before the Supreme Hacker Council , however, there were no such considerations, just as there was no desire to attend the council.
')
Hacker rose from the bed. Having washed himself with cold water and walking back and forth across the room, he looked thoughtfully out the window. In the house, on the contrary, the workers, under the supervision of a lady with a doggie, changed the windows of the facade.

“Windows, windows ...” thought Hacker. - Windows is Windows; Windows, Windows .... Windows are brakes, brakes are a turtle; turtle, turtle ... turtle is a Subversion client ... Subversion ???

"Eureka! "Exclaimed Hacker. The decision came by itself. Now he knew what he would offer on the council.


So, we will talk about the next bike called template engine. The basis of our template engine will be Subversion version control system and its wonderful hook mechanism. In this case, we will need a post-commit hook that runs after the transaction (making changes to the repository) has been completed.

Let's try to figure out why we need another bike.

First, we want to maximize the automation of all workflows. Secondly, we want to make life as easy as possible for the coder and programmer who is directly involved in the workflow. Thirdly, we want to create a more powerful and simple solution to those that exist now.

Each typewriter wants to see as little as possible “non-HTML” markup. A person unfamiliar with such a well-known template engine like Smarty (there are a lot of such) will not immediately accept this markup: www.smarty.net/manual/en/language.basic.syntax.php

We give the coder a possibility and will use constructions in templates that meet the HTML syntax. The first advantage following from this: the maker-up can use static pages for work.

Try to see how the browser displays the example given from the link above and the following:
<! doctype html >
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html;charset=utf-8" />
< title > iSvnTemplate example </ title >
< style type ="text/css" >
#header, #footer, #lsb { font-size:24px; color: white; }
#header, #footer {
width:100%;
height:50px;
background-color:#303030;
text-align:center;
line-height:50px;
}
#footer {
clear:both;
}
#lsb {
border:1px solid green;
margin:2px;
min-height:200px;
width:90px;
float:left;
}
</ style >
</ head >
< body >
<!-- Template container. -->
< cont name ="header" >
< div id ="header" >
Header
</ div >
</ cont >

< cont name ="lsb" >
< div id ="lsb" >
Lsb
</ div >
</ cont >

< cont name ="blockA" >
< div id ="blockA" >
<!-- Support of conditional expressions. -->
< if cond ="$ a > $b" >
< h2 > A is greater than B </ h2 >
</ if >
< else >
< h2 > B is greater than A </ h2 >
</ else >

<!-- Support of loops. -->
< for cond ="$i = 0; $i < 8; $i++" >
<!-- Including another template. -->
< include file ="" />
</ for >

< php >
print " < b > Hello, World! </ b > "
</ php >
</ div >
</ cont >

< cont name ="footer" >
< div id ="footer" >
Footer
</ div >
</ cont >

</ body >
</ html >


* This source code was highlighted with Source Code Highlighter .

Unknown tags if , for , and include browsers will ignore, and the resulting output will fully correspond to what will be after the document is processed by the template engine (not counting those parts that will not be included in the output).

In addition, we have a powerful tool for parsing templates: DOM parser . Since all the template constructions are in accordance with the HTML syntax, all tags like <if cond = ”$ a> $ b”> </ if> are very easily converted into the corresponding PHP code.

Consider the file structure of the template engine, using the example above, and what happens when the coder is working with an in vivo HTML document:
 / view
   / static
     index.html
 / template
   header.tpl
   lsb.tpl
   footer.tpl
   blockA.tpl
 / logic
   header.tpl.php
   lsb.tpl.php
   footer.tpl.php
   blockA.tpl.php


The index.html pages contain several templates: header, footer, lsb, and blockA. After the coder makes changes to the document (changes one of the templates, or adds a new one), he makes changes to the repository ( svn ci –m “index.html update” ). Immediately the field of this fulfills a post-commit hook, which performs the following:
  1. Reads the entire contents of the / view / static directory
  2. Parsing documents found in step 1.
  3. Extracts all templates and updates the contents of the / view / template directory.
  4. Adds (or deletes) class frameworks to the / view / logic directory in case a new template has been detected.


Each template corresponds to a PHP class inherited from the base class Template . The class automatically loads the template it needs (markup + PHP code after processing with the template engine), accepts the necessary data and returns based on their template body (MVC architecture). Templates can also be nested.

Thus, using simple techniques, we get a solution that is much more flexible than any others (at least from those known to the author).

All this is at the level of an idea, but if someone wishes to implement and use this technique, the author will be happy to provide all possible assistance.

Source: https://habr.com/ru/post/96738/


All Articles