In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "PHP template engine Prototype configuration", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "PHP template engine Prototype how to configure" this article.
1. The first step is to configure the root directory of the template engine. If you do not set it, the absolute path to the root directory will be automatically generated.
Template:: $rootPath = dirname (_ _ FILE__); / / both relative and absolute paths are available, but we use absolute paths here! Such as "C:/wwwroot/prototype"
two。 Configure the template file directory, which is used to store template files. If it is not set, it defaults to: templates directory.
/ / the default directory is used here, which is set up according to your own needs. It is recommended that you do not make mistakes in writing Chinese. It does not matter whether you add a forward and backward slash before and after the directory name. Eventually, the template engine will automatically correct it. Template:: $templateName ='/ templates/'
3. Configure the compilation file directory, which is used to store the compiled files generated after the template file is parsed. If it is not set, the default is: compiles directory.
Template:: $compileName ='/ compiles/'; / / uses the default directory as well as the template directory.
4. Configure the cache file directory, which is used to store the cache files generated by the compiled files after the caching function is enabled by the template engine. If it is not set, the default is: caches directory.
Template:: $cacheName ='/ caches/'; / / use default as well
5. Configure the template constant directory, you may not quite understand what the template constant is used for, and what is the difference between the template constant and the ordinary PHP constant? As for the explanation of template constant, we will discuss it in detail in the following application. Just configure it with me first. If you don't set it, it defaults to constants directory.
Template:: $constantName ='/ constants/'; / / use default
6. By step 6, the configuration of the directory is complete. There is no need to worry about the problem that the directory does not exist, and there is no need to create it manually. The template engine will automatically complete it for us. Then the next step is to set the file name of the template constant. If it is not set, the default is: default.xml file. Like the directory, there is no template engine to create it automatically.
/ / We also use the default, but it is important to note that .xml must be used as the extension here, because constant files are described with XML tags, and if they do not end with .xml, it may cause an exception when the template engine handles constants! Template:: $constantFile = 'default.xml'
7. Set the cache switch, which is turned off by default and will only be turned on if we set it.
/ / attention, what I write here is a Boolean value, in fact, any value can be filled in here, and eventually it will be implicitly converted to a Boolean value, either 0 or 1. I write a Boolean value directly to facilitate your understanding! Template:: $cacheSwitch = true
8. At this point, the configuration of the template engine is basically complete, which is still very simple. Now we just need to instantiate the template engine object and we can actually run the template engine.
$tpl = new Template (); / / instantiate the template engine. All configurations before this step take effect, and no parameters need to be passed when the template engine is instantiated.
9. After we instantiate the template engine object, we can start to operate on it, so who is it going to work on? Template file, of course. First, we need to create a template file. Create it in the template file directory. The template file is actually a pure HTML code file, and the extension can be customized, but we all have the .tpl extension. Suppose we have created a template file named: index.tpl, because it has the same name as our php business logic file index.php, which is also in accordance with the convention, because the index.php file calls the index.tpl template.
10. After creating the template file, we can load the template file and inject template variables in the business file (the previous configuration is also performed in index.php). Template variables and other template identifiers (collectively referred to as template tags) will be explained one by one in the next steps.
There are two formats for injecting variables. Take a look at the instructions in the API manual. The array format and the traditional key-value pair format are fine. Let's use both. $tpl-> assign ('title',' title'); / / first of all, the traditional key-value pair format $tpl-> assign (array ('title' = >' title', 'name' = > name')); / / the array format is obviously easier to use, because when you inject multiple variables, you don't have to write multiple injection statements and do it in one sentence. / / if two identical variable names appear, the previous one will be replaced later. In the following code, the final value of the language variable is: English. $tpl-> assign (array ('language' = >' Chinese', 'language' = >' English')); / / the next step is to load the template file, write the template name directly, and the template engine will automatically lock to the template file directory. $tpl-> display ('index.tpl')
11. At this point, the operation of the template engine is over, and then we will familiarize ourselves with the use of the various template tags in the template file and what they are used for. There are 9 kinds of template tags in the prototype, which are: 1. Template variable, 2. Template constant, 3. Single-line template comments, 4. Multiline template comments, 5. Include file loading, 6. Template template file loading, 7. Source source template file loading (special), 8. If divergence statement, 9. Foreach loop statement. So let's first explain the template variables.
{$title} {$123}
twelve。 The following is the use of template constants. Template constants and PHP constants, although they are called constants by name, are not essentially the same thing. Template constants can actually be regarded as pseudo-constants, rather than real constants, it is through the storage of XML tags, to achieve a global invariant specific values, these values need to be manually added to the constant file. (it is not very convenient to add manually. I will add the function of automatic addition in the subsequent iteration of the version.)
First, we need to manually add template constants to the template constant file, as shown in the following code:
WEBNAME site title 123abc
After the constant is configured, the next step is to call it in the template file, as follows:
{WEBANME}
{NAME}
{abc123}
13. The following are the annotation characters for the template, there are two kinds: one is single-line, the other is multi-line. It is often used to annotate the code of the template file, so that the artist can understand the actual meaning of the code when designing the interface.
{@ ordinary single-line comment} {@ single-line comment} {#} this is a multi-line comment, pay attention to the first echo! {/ #} this is a multi-line comment. I've changed the line! {/ #} {#} did not write the ending symbol
14. Template loading identifiers, loading methods can be divided into three categories: direct loading of ordinary files by include; loading after compilation of template files by template; and direct output of the path of compiled files by source after compiling template files (this method is special and imperfect and needs to be used in specific situations, such as the call of frame pages)
The first is the load call to the ordinary file, which is as follows:
{include path = "test.php"} {include path = 'abc.php "} {include path =" 123.php "} {include path =" 123.php "}
The following is the compilation and loading of the template file, with the following code:
{template path = 'test.tpl'}
The last is the template file compilation address output, this function is more special, even if do not understand it does not matter, this method has serious BUG has not been processed, so it is not perfect, and the probability of use is also very low, here is only a simple introduction. Whether it will be retained and improved in subsequent version iterations is still being decided, and the code is as follows:
15. Then there is the frequently used if divergence statement, which is similar to php's if statement, but is functionally simple and does not support multiple and nested judgments, but I will make it more powerful in subsequent iterations.
{if $action}
Interface 1
{/ if} {if! $action}
Interface 2
{/ if} {if $action}
Interface 1
{else}
Interface 2
{/ if} {if $action}
16. Finally, it's the last and most complex invocation of the foreach loop statement, which is the same as PHP, with a slight change in format.
{foreach $array (key, value)}
{% key}... {% value}
{/ foreach} {foreach $userList (id, username)}
{% id}... {% username}
{% password}
{/ foreach} above is all the content of the article "how to configure PHP template engine Prototype". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.