Details about * .tpl files DLE template their purpose. Using TPL files TPL PHP templates how to use

Immediately I will say that I have already written about this topic here :. However, not everyone understood this material, and I decided to return to him and write everything a little differently. It's no secret that any serious engine will never be mixed in your code. HTML. and Php.. But, HTML. and Php. codes are very closely interrelated, so in order not to violate the rules of "good tone", were invented tPL files. These files are stored HTML code with template elementswho are substituted in PHP code. Let's look more in more detail using TPL files With an example.

We take with you the most elementary example is a user panel where there is his avatar and a greeting by name. First of all, create tPL file.and let him call userpanel.tpl. I remind you that this is just a piece HTML code with template elements:

Hello,% Name%!




Nothing complicated, it's just an ordinary HTML code. Only instead of certain values \u200b\u200bare worth % Elements_Sablon%.

Now we'll figure it out PHP processing. Everything will be more than abstract, but, unfortunately, in any way. The main thing is to understand the principle of work. So PHP code For processing created tPL file.:

/ * This feature, although uses buffers, but the essence of the elementary: return the contents of the file * /
FUNCTION GETTEMPLATE ($ NAME) (
OB_START (); // start saving the output to the buffer
Include ($ Name. "TPL"); // Send the contents of the file to the buffer
$ Text \u003d OB_GET_CLEAN (); // Clean the buffer and return the contents
RETURN $ TEXT; // Text Return from File
}
$ Name \u003d "Nikolai"; // removed from the base
$ avatar \u003d "avatars / user_15.jpg"; // removed from the base
/ * Begins to replace the elements of the template for real data * /
$ UserPanel \u003d STR_REPLACE (
Array (
"% Name%",
"% avatar%"
),
Array (
$ Name
$ avatar
),
GetTemplate ("Userpanel")
);
Echo $ Userpanel; // Display the final result
?>

This is the simplest option. Everything commented, so there should be no questions for this example. And in fact, any page consists of such blocks. Your task take the right blocks (function getTemplate ()), Replace the necessary data (the STR_REPLACE () function and the data obtained, for example, from the base), and then simply connect all the blocks as regular lines, and output everything to the page.

Certainly without OOP It will be very problematic here. You boost such a number of conditions (a lot of pages), which quickly confuse. But the principle of use tPL filesI hope you realized. Further think about how to wrap in OOPSo that everything is as simple as possible in terms of understanding the code and its maintenance in the future.

After creating info.-File, in principle, the topic is already defined. This means that you can go to the field management section. http://mysite.ru/admin/build/themes. And include your topic there. Naturally, after turning on it no design you will not see - the page will acquire the style of "black on white" - on a white background black text.

However, I want to note that despite the fact that there are no files in our topic myTheme.info. does not lie, the site will work as as before - to withdraw all content, add blocks to the regions ( http://mysite.ru/admin/build/block) etc. This is explained by the fact that the kernel of the drupal included mandatory moduleswhich even with the complete absence of any files in your topic (with the exception of the INFO file) allow you to continue working with Drupal.

In principle, all creation of a template comes down to overlapping template files (they have an extension .tpl.php.) standard modules of our CMS..

The most important TPL file (TPL is a reduction from template., template) page.tpl.php.. He is responsible for building each site page. Let's deal with what the template file is:

  • hTML code
  • php code
  • javaScript code (not necessary)

In each template file, Drupal transmits site data in the form of standard variables. For each template file there are 2 types of variables:

  • variables that are transmitted only to this file.
  • variables that are transmitted to all files

Here is a list of all variables for page.tpl.php.:

General variables (for all files):

  • $ Base_Path - Basic path where Drupal was installed
  • $ CSS. - array of CSS files connected to the current template file
  • $ Directory. - path to the folder in which the topic is installed
  • $ is_front - Returns the value of truth if you are on the main page
  • $ Logged_in. - Returns the value of truth if you are logged in
  • $ is_admin. - Returns the value of truth if you have access to the admin

Metadata Pages

  • $ Language. - (an object) Current languagewhich is displayed on the site
  • $ Language-\u003e Language - Contains its text representation
  • $ Language-\u003e Dir - Contains the direction of the language. This or will be "LTR" (Left-to-Right) or "RTL" (Right-to-Left)
  • $ Head_title. - Modified page title version for use between tags
  • $ Head. - inserted between tags . Contains meta tags, keywords etc.
  • $ styles. - Used to download everyone cSS.-Filets to the current page
  • $ scripts. - Used to download everyone javascript "OV to the current page
  • $ body_classes - Set of classes cSS. For Taga . Contains information about the current arrangement of columns on the site, their number, the current URL "E, etc.

Information about the site

  • $ Front_page. - Address of the main page of the site. It is better to use this variable for reference to main pagebecause It includes a domain language and prefix
  • $ logo. - path to the site logo, if included on the site
  • $ Site_Name. - Site name. It can be empty if you turn it off in the features in the info file. Configured in mysite.ru/admin/settings/site-information
  • $ site_slogan. - Slogan site. It can be empty if you turn it off in the features in the info file. Configured in mysite.ru/admin/settings/site-information
  • $ Mission - Mission site. It can be empty if disable it in the features in the info file. Configured in mysite.ru/admin/settings/site-information

Navigation

  • $ search_box - contains an HTML code that displays the search string. Can be empty if you turn it off in the info file
  • $ primary_links.
  • $ Secondary_Links. - an array containing navigation links for the site if they are allowed in the features of the info file

Default Page Content

  • $ Left. - region. Contains HTML code for the left column. If in the info file to set any regions, then it disappears
  • $ BreadCrumb. - "bread crumbs"For current page
  • $ title - Page title
  • $ Help. - Dynamic tips, mainly shown in admin
  • $ Messages. - Displays errors and warnings on the site
  • $ Tabs. - References (tabs) connecting the current page with its subdraps (for example, for the article - with its editing page)
  • $ Content - Content current page
  • $ right - region. Contains HTML code for the right column. If in the info file to set any regions, then it disappears

Lower area / closing data

  • $ Feed_icon. - string with all icons feedback For current page
  • $ Footer_Message. - Message at the bottom of the page. Configured in mysite.ru/admin/settings/site-information
  • $ Footer. - region. Contains HTML code for the bottom of the page. If in the info file to set any regions, then it disappears
  • $ closure. - Closing label for all modules that changed the page. This variable must be displayed after all dynamic content. Best before closing tag body

All are listed here standard variables. But you can add your variables here either as regions through info.-File or in any other role through the file template.php. (about him a little later).

Now I will show what code should be in page.tpl.php. And in what code it is then interpreted by browsers. Before you a piece of code from page.tpl.php.:

In the first line there is a check, whether there is a headline at the current page. If it is not - then the debugger will simply miss this code, and will not come to the end of him. If the header exists, the page code is added to the HTML

, after it will be printed the title of the page, and all this will be closed by tag

. If you look at the code of this page in the browser, then it would look like this:

Lesson 4. Required files to create a template. Page.tpl.php.

Thus, almost all site variables are turned around. This is done so that we can prescribe styles for content, not knowing what it happens.

This way looks like standard file. page.tpl.php.who goes along with Drupal. Change the name of the classes, rearrange the variables in some places - and see what it turns out. It is necessary in order to "swell" how it works and what is derived as a result.

"- // W3C // DTD XHTML 1.0 STRICT // EN" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd"> "http://www.w3.org/1999/XHTML" XML: lang \u003d "language?\u003e " lang \u003d. "language?\u003e " dir \u003d. "dir?\u003e "> <?php print $head_title ; ?> "" >

Dear friends,

we continue the publication of a series of useful tips that facilitate understanding of some actions and features of the script. Recently, questions often come to us with a request to make a modification of the script so that you can use various templates for different sections of the site. For example, the main page with the news should have one layout structure, and for example, the feedback page is completely different. At the same time, motivating to us that it is possible to change patterns in the admin panel only for site news categories. But in fact, all this can be implemented by standard means, which this small article will go.

So the first thing we need to turn to the script documentation, which reads that the template main.tpl Supports the following tags:

Text that displays the text enclosed in tags if the specified section of the site is searched.


also this tag has the opposite

Text that displays the text enclosed in tags if you are viewed any partition in addition to the specified


Take as a basis the example of the task: Make sure that the site uses one design of the template, and the feedback on the site used the other. Based on this, we need to open a template. main.tpl and specify the following:

Here the entire text of the template that will be shown when viewing feedback
Here the entire text of the template, which will be shown everywhere except feedback


But it has one big minus, your main template file main.tpl It will be too large, because In essence, there will be two different designs, and here we again appeal to the documentation and the script and learn about the existence of a beautiful tag: (Include file \u003d "My_block.tpl")which connects the specified My_block.tpl file into the template.

Based on all above, the final implementation is as follows:


In the template file feedback_main.tpl We make a feedback design, and in the All_main.tpl file we make the design of the rest of the site. That's actually, everything is easy and easy to implement, no need to produce any modifications of the script. You can also place the design of any partition, you can combine several sections, etc. Read more and more carefully, read the documentation for the script, there are quite a lot of useful to emphasize for yourself.

Yours faithfully,


The separation of data logic from the logic of their display is a very important component of a web development.
Any programmer who rose a little higher than the level of "Hello World", begins to feel the need for such a division. But not everyone comes to the right conclusions and solutions.
Therefore, I will give here the most important rules:
1. Receiving code and data display code must be divided.
2. Any conclusion should start only after all the data is ready for it.
3. As a result, any script should only deal with data processing. After that, he can either send some NTTR heading, or call a template, having transferred to it the prepared data, or both together.
4. What exactly to use the tenth is the case. The easiest and most affordable - RNR itself, so the examples will be brought on it.

Misconception
No, probably, in the web programming the theme is as obvious, as incomprehensible as templates. Each, sooner or later, comes to the conclusion of the need to use templates. But it comes, for some reason, through some of the wildest misconceptions and fantasies.

The simplest and obvious misconception is that the newcomers are called the template made in a separate "design" file - shared HTML for all pages of the site. And soothe on it. Dynamic information, no harsh, withdrawing old good echo :-)
In fact, the templateizer is mainly engaged in the output of the changing content of the page pages. And the conclusion of "design" is a minor task.

The fantasies are the main two:
1. Templates are needed to "designer" so that he can edit them without understanding PHP.
2. Consequently, templates serve to separate PHP from HTML.

Let's try to think about the first statement. Who is a designer? This is a person who works in Photoshop. HTML he most often does not know at all. And above the template there is either a special layer or - most often ... programmer himself! Funny, is not it?
Now a consequence, about the PHP separation from HTML. Excellent. Before us is the holy goal to separate. Therefore, we come up with Smart and write:
(Foreach Key \u003d CID Item \u003d Con From \u003d $ Contacts)
($ con. Name) - ($ con .Nick)

(/ foreach)

More funnier.
"Designer", for which everything was stood, fainted from happiness.

Theory
It turns out that our reasons for which we decided to use templates are not worth a penny. And what now - is not needed, it turns out, shabons at all? Need. But first you need to answer the question - "Why?" For what We need patterns. And check the response practice. I asked many times this question. But almost no one can answer it. Why he needs templates. It turns out, people do something, not knowing why.
This is the funny thing.

During their activities, as a web programmer, I formulated three reasons for myself, for which templates need personally. In essence, there are two of them. And go down, ultimately, to one:

One code - several views.

It often happens that instead of one information you need to show another. For example, the work code with the database receives an error message instead of the text. In this case, instead of the news page, you need to show a completely different one - with apologies and request to come later. Using templates, this is done elementary.

Often one and the same information should be shown in several species. For example, a regular page and a print page. The information is the same, the code for its preparation is the same, and the output code is different. Faced with this situation, very quickly divide your code into two parts, one of which is responsible for the output, and the second is not responding. Another example: let's say we wanted to output information not directly to HTML, but via ajax request, in JSON format. If we have been used a templateizer, then we change in our code exactly one line - call the templateizer to call JSON_ENCODE (). And if our conclusion was mixed with data acquisition code, then the code would have to rewrite the whole!

In a similar situation: let's say, our script is on two sites. Plus a copy of us at home. And at home we found a large bug. Extraked it. Now you need to update the code on sites. And so he is the moment of truth: if the templates were used correctly, then we simply pour code on both sites, and everything continues to work, as if nothing has happened! Such a situation as I think is the perfect check of the selected approach to template.

Another important point that many are missing (in their theoretical reasoning, while constantly facing him in practice!) - the procedure for executing the script does not always coincide with the order of output in the template. The textbook example is the output of the header of the article in the tag . If we derive information as we arrive, we simply can't do this - the hat <i>already</i> Released, by the time we started receiving the text news.</p><p>It should also be remembered that in addition to the text of the RNR, the scripts also output HTTP headlines. Which are required to display before any text, or even instead of text in general (if we, for example, we want to redirect the user to another page). If we first implement the logic of the application, without displaying anything, then you will not give the necessary NTTR heading to us for us.</p><p>You may have their own reasons to use templates. But only with one condition - these reasons must be caused by a real, vital necessity, and not by the "highest considerations" and care for some unknown people.</p><p><b><a name="example">Practice</a> </b><br> Now from the theory, we turn to practice. <br> In the simplest case, in the show of any page, we will always participate two templates: a common website template and a concrete page content template. <br> Suppose we want to make a page with links to sites of friends. <br> In this case, the code simplified code will look like this:</p><p>Links.php file itself. Nothing displays. Just prepares the data and then causes a template. <br><span><?<br><span>// Inclusive settings. <br></span>include "settings.php";</p><p>// Receive data from the base, determine variables <br></span>$ PageTitle \u003d "(! Lang: Links" ;!} <br>$ Data \u003d $ DB -\u003e GetAll ("Select * from Links");</p><p>// Set the page template and call a general website template. <br></span>$ TPL \u003d "TPL_LINKS.PHP"; <br>include "tpl_main.php"; <br> </p><p>General template (TPL_MAIN.php):</p><p><html xmlns="http://www.w3.org/1999/xhtml"><br> <head><br> <title>MY SITE.<?=$pagetitle?>






In the right place it turns on to the pan_links.php (TPL_LINKS.php):





  • "target \u003d" _ blank "\u003e


    • Hello everyone. I want to introduce another bike written in PHP using Document Object Model. What is his difference from the other three-wheeling representatives of the same species? In fact, the differences are not so much, it combines the best of many. For example:

      1. Full partition HTML and PHP.
      2. No additional labels in type patterns


      3. Ability to embed to lay down the contents of other template files, both from PHP and using a special tag in layout.
      4. Ability to create any HTML-tag on the fly.
      5. Ability to save to HTML file all that has been generated and collected.
      6. Verify the existence of an HTML file of the requested page before generating a template.

      So that everyone immediately became clear how comfortable it is comfortable and easy to appeal, I will tell and show how I used it to create one of my projects (I suspect that I will reprick all my projects under it).

      The first thing I usually do is I get all the information from the database about the page (keywords, page description, template name and CSS and JS files). I keep all this in an array of $ Head. Further I get content from the database and keep an array of $ Page. And proceed to work with the class.

      So, first, I call the class constructor and transmit all the necessary parameters to it:

      $ TPL \u003d NEW TEMPLATE; $ TPL -\u003e EXT \u003d TPL_EXTENSION; # Expansion of files in the $ TPL template catalog -\u003e htm \u003d cache_extension; # Expansion for already generated pages $ TPL -\u003e Skin_Dir \u003d Dir_Templates; # Directory in which all site templates are lying (for example, Templates) $ TPL -\u003e JS_DIR \u003d Dir_js; # Directory where you need to search for JS files $ TPL -\u003e CSS_DIR \u003d DIR_CSS; # Directory Where are CSS $ TPL -\u003e img_dir \u003d dir_img; # Directory where $ TPL pictures -\u003e Skin \u003d $ _session ["Skin"]; # The name of the template that I want to use $ TPL -\u003e cache \u003d dir_cache; # Where to save ready-made HTML $ TPL -\u003e LOG \u003d FILE_T_LOGS; # Where to write logs $ tpl -\u003e tag_start \u003d symbol_start_tag; # The character from which the variables begin in the $ TPL template -\u003e tag_end \u003d symbol_end_tag; # Symbol that end the variables in the $ TPL template -\u003e dir_delimeter \u003d directory_separator; $ TPL -\u003e SPACE \u003d SYMBOL_SPACE; # Symbol replacing space.
      Fuf, it seems all the variables passed, go further.
      In order not to force the class to make an extra work, first we check, suddenly we already have a ready-made HTML file of the requested page.
      if ($ TPL -\u003e TestPageStatus () \u003d\u003d\u003d True) (Require $ TPL -\u003e CacheFileName;) ELSE ($ TPL -\u003e Page ("index"); # We pass the name of the template file, by the way you can pass a few, through a comma $ TPL -\u003e Assign ("Head", $ Head); $ TPL -\u003e Assign ("Content", $ Page); $ TPL -\u003e Build (); # We give a team to collect template $ TPL -\u003e showpage (); # Take down.)
      Here is actually all the methods that need to be used to remove the page.

      Now we will analyze a couple of useful methods of this class. Suppose that we have already passed into the class everything you need, but they did not give him a team to conclusion, because they suddenly remembered that we need to create several HTML tags in the template. This is also done very simple. First you need to find the block in which we want to add something. You can find 2 ways:

      $ TPL -\u003e FindByid ("FindMe"); $ TPL -\u003e FindbyTagname ("Div");
      The FindByid method logically implies that in the template all the tags are unique in the template. And the FindbyTagName method will return the first suitable.
      The result we received search We must pass to the $ TPL method -\u003e createchild () to be able to create in the found element, child tags. The CreateChild method by the way, after creating a new item, returns it to us, so that we can only use the element created somewhere else.

      Picking and experimenting I found 3 ways to create tags in a template, so I will show 3 examples at once. Example1:

      We need to create

      inside

      $ parent \u003d $ tpl -\u003e findbyid ("Parent"); $ TPL -\u003e CREATECHILD ($ PARENT, "DIV", "ID \u003d CHILD, CLASS \u003d TEST");
      We get:


      Example 2:

      We need to create

      Some text
      inside

      $ parent \u003d $ tpl -\u003e findbyid ("Parent"); $ TPL -\u003e CREATECHILD ($ PARENT, "DIV", "ID \u003d CHILD, CLASS \u003d TEST", "Some text");
      We get:

      Some text

      Example3:
      We need to create

      New element
      In the first span element

      $ parent \u003d $ TPL -\u003e FindbyTagname ("Span"); # (1) $ TPL -\u003e CREATECHILD ($ PARENT, "DIV", "New Element"); # (2)
      (1) Finding a parent is not by ID, and in the tag will find the first suitable
      (2) If we do not need attributes, but only the value of the new item, you can not transmit them

      We get:

      New element

      And after these manipulations, I already call Showpage. And here we smoothly approached another 2 interesting moments.
      Imagine the situation that we have a template, suppose that this is a List.tpl template with a list, for example, mobile phones:

      (Content.Brand)

      (Content.Model)

      (Content.info)

      If we have passed only 1 phone information, it will simply be replaced by variables on their values, and if we have passed the information immediately by several phones, then the class will copy this site as many times as the options of values \u200b\u200bcame. And it will make it himself, in contrast, for example, from class Xtemplate, which accounted for each value calls Assign and Parse
      True there is one not a very convenient moment, if after this block is some more, for example:

      (Content.Brand)

      (Content.Model)

      (Content.info)
      Still some block

      Then in such a situation we will have to apply a small trick, packing our mobile phone

      (Content.Brand)

      (Content.Model)

      (Content.info)
      Still some block

      In this case, all mobile phones will appear after each other, inside
      , and "some other unit" will remain below.

      And if I did not forget anything, then the last moment is adding the contents of other templates to the current template.
      I call again to your fantasy.

      Imagine that the gauge wants the contents of the Page.HTML file to be added to the List.html file block, for this it is in the desired location of the List.html file adds page And when a class sees this tag, he will replace it with the contents of the Page.HTML file

      The number of such inserts is not limited and their location is absolutely not critical, so you can insert them anything and in any quantity.

      That's probably everything, if I remember something, I will report additionally. Thank you for reading to the end.

      Tags: PHP, Class, Template, Tenderizer, Parser