Template parsing variables

Source: Internet
Author: User
Discuz! The template uses the syntax similar to the PHP expression. The supported elements are as follows: <! -- {...} --> Logical element surround character, which is used to determine conditions for inclusion and loop elements <! -- {If expr1} --> statement1 <! -- {Elseif expr2} --> statement2 <! -- {Else} --> statement3 <! -- {/If} --> This is a typical condition template. When the condition expr1 is true, the template statement1 content is displayed. Otherwise, the template statement2 content is displayed when expr2 is true, otherwise, the template statement3 is displayed. Like condition control in other languages, where <! -- {Elseif expr} --> and <! -- {Else} --> is optional. Array loop without subscript variables <! -- {Loop $ array $ value} --> statement <! -- {/Loop} --> equivalent to the PHP array loop statement: foreach ($ array as $ value) {statement} array loop with subscript variable <! -- {Loop $ array $ key $ value} --> statement <! -- {/Loop} --> equivalent to the PHP array loop statement: foreach ($ array as $ key => $ value) the {statement} logical element delimiter {} contains the html annotation symbol <! --> In addition to distinguishing information elements, it also facilitates users who use tools such as Dreamweaver and Frontpage to edit templates. Because logical elements are recognized as html comments and are not displayed, most users do not need to modify the content of logical elements, an error message or the entire system may fail to run if the character is modified incorrectly. Therefore, when you modify a template, do not modify the logical information of the template. Do not modify the internal elements of the logical information (such as <! -- {Else} -->. If your template is accidentally modified or damaged, immediately use the original standard default template to overwrite it. For experts, actually external <! --> Can be omitted and can be used to locate certain elements in the template. However, it is strongly recommended that you do not try it easily. {...} The information element envelope character {CONSTANT} can reference the interface to replace variables, which must be uppercase letters. The actual purpose of this symbol is to reference the php constant, therefore, you can use it to reference other constants. {Lang variable} can reference the content of the language variable in the template. The language variable is defined by the $ language array in templates. php. lang. For example, the content of {lang post_edit} is replaced with the content of $ language ['post _ edit'] during Template Compilation. The simplified Chinese version indicates "edit post ". The {template name} template embedding operator. To avoid lengthy templates, you can use this symbol to embed the content of the name template into this template (which is actually referenced ). {Eval statement} run the PHP statement body statement. We recommend that you use this structure only when necessary, because it will damage the structure and readability of the template. For example, {eval echo 'Template! ';} Implements echo 'template'; {LF} line feed, because Discuz! The template engine ignores unnecessary line breaks (\ n). This symbol is used to generate the required \ n. Recommended Template modification method: you can use the general editor to manually write html or use Dreamweaver or Frontpage to modify the template. However, you must remember not to modify the positions and content of elements in the same logical element or nested element. That is, you cannot <! -- {} --> The contents of the bucket cannot be deleted. However, the entire external logical structure can be moved or deleted. If the following error message Parse error: parse error, unexpected '}' in/home/username/forums/forumdata/templates/1_login.tpl.php on line 6 is output on the modified template page, it indicates that the template contains logical elements that cannot be matched or are incorrectly formatted. Please carefully check whether there are elements that do not match, are not ended or nested, and the format is incorrect, if necessary, the original template can be restored to solve the problem. Discuz! A convenient interface packaging program is provided. The format has been described earlier. Paste the exported information to the import interface solution in the style solution in system settings and run it. The program will automatically parse the data content, restore the relevant interface settings, customize the replacement variables and the required template settings. If a non-default template is used on the exported page, you must set the./templates directory attribute to 777 so that the import program can automatically create template records and folders. Variable definition description <? Exit?> <! -- {Eval $ I = 2} --> <! -- {If $ I = 1} --> <! -- {/If} --> <! -- {Block name = "spaceblog" parameter = "notype/1/order/I. dateline, I. lastpost/limit/0, 14/cachetime/900/cachename/newti/tpl/data "} --> <! -- Latest log --> <! -- {Eval $ I = 0 ;}--> <! -- {Loop $ _ SBLOCK ['newti'] $ value} --> <! -- {If $ I % 2 = 0} --> // if the remainder of I divided by 2 is equal to 0 <div class = "listbg"> <! -- {Else} --> <div class = "listbg2"> <! -- {/If} --> · [<span class = "listA"> $ value [typename] </span>] <aclass = "listA" href = "$ value [url] "title =" $ value [subjectall] "target =" _ blank "> $ value [subject] </a> </div> <! -- {Eval $ I ++;} --> <! -- {/Loop} --> <! -- {Eval for ($ I = 1; $ I <= 5; $ I ++) {}--> <! -- {Eval echo "Hello World! <Br/> ";}--> <! -- {Eval }}--> to create a template, you must have a basic HTML (learning the html language), a basic div + css style, and a simple template syntax. These skills can be learned by referring to the default template file. This section mainly analyzes the parsing syntax of the Discuz template. I. DiscuzX2.5 new php format Template File loading support from Discuz! Starting from X2.5, the template file supports the PHP extension format. The main function is to prevent the template from being saved as stolen by other forums! For example, template/default/common/header.htm can be created as template/default/common/header. php: after the template file is php, the code writing method remains unchanged and HTML is used as before. In the PHP template file, you only need to add a line of code at the beginning of the original HTM template file, for example: <? Php exit;?> Copy the code or <? Php echo 'You cannot view the content of this template '; exit;?> The template data content of the template file that copies the code PHP will be parsed from the second line of the file. When both the PHP and HTM template files exist, the PHP template files are parsed first. 2. The Template variables DiscuzX have two types of variables: 1.G variable: $ _ G [xxx] G variable is the global variable of the program. To make the program more efficient, reduce unnecessary data acquisition, so the program will usually need to use the variable unified under the G variable, such as user logon information, background settings, server environment information, client CooKies, and data cache are all stored in G variables, when creating a template, the author only needs to print out the G variable to obtain whether the required information is in the G variable. 2. Custom variable: $ xxx custom variable is a user-defined code that starts with $ and starts with a letter or underscore, for example: $ data, $ thread, $ post, $ forumlist, $ threadlist, and so on! Custom variables can be customized by the author in the program, or the program itself has been defined. III. Output mode of variable data in the template 1. Output the value of a variable {$ my_var}, equivalent to the value of php <? Php echo $ my_var;?>, Curly braces can be omitted but are not recommended to be removed. 2. The format of cyclic code writing for custom variable data: <! -- {Loop $ data $ key $ value} --> <li> $ key $ value </li> <! -- {/Loop} --> Copy the code. This code is a typical cyclic code, which means to loop the custom variable $ data and pass the data in each loop to $ value, $ key is the array key value (serial number), <! -- {Loop $ data $ key $ value} --> <! -- {/Loop} --> to write html code. We only need to remember this simple but important meaning! Open template/default/forum/viewthread.htm and search for this code as the circular code on the post page, we should be able to get a general idea of this circular code through the above [Circular code writing format for custom variable data. The $ postlist variable contains the topic information and floor information of the current post. Data of each floor is passed to the $ post variable through a loop. The cyclic HTML code is stored in template/default/forumviewthread_node.htm. After opening this file, you can see that many variable names in it are $ post. For example: $ post ['authorid'] = author UID, $ post ['username'] = author username, etc. The corresponding data is output using different field information of $ post [xxx, these fields are stored in the database-data table pre_forum_post. It is often found that many children's shoes put the $ post [xxx] variable into other template files for use, and the results are refreshed with no results! The reason is that each template file has a corresponding php program file, so custom variables cannot be used in different pages, but only in template files that define variables. For example, the Post data on the Post list page cannot be output on the forum homepage or other pages. IV. Condition determination 1. if the process branch is written in an HTML form element through if, you can skip this step to make the code clearer and easier to read, for example, {if $ my_var} xxx {/if} <! -- {If $ my_var} --> any html statement <! -- {/If} --> Copy code 2. Write an if statement with multiple conditions. You can use the bitwise operator in the regular PHP judgment. <! -- {If $ my_var & ($ my_var2 & 1 | $ my_var3 = 3)} --> any html statement <! -- {/If} --> Copy code 3. Write if statements with branch conditions <! -- {If $ my_var = 1} --> the variable is 1 <! -- {Elseif $ my_var = 2} --> the variable is 2 <! -- {Else} --> Other cases <! -- {/If} --> Copy code 5. The nested template content is parsed into PHP statements and merged into the template. Example: in template/default/forum/viewthread.htm, you can find: <! -- {Subtemplate forum/viewthread_node} --> This code loads another template file: template/default/forum/viewthread_node.htm, when we open this file, we find that there is still a piece of loading code: <! -- {Subtemplate forum/viewthread_node_body} --> This code is loaded into another template file: template/default/forum/viewthread_node_body.htm, which is nested N times in the template file of DiscuzX! First, we need to clearly understand the usage of each template file, and then understand the meaning of these nesting: template/default/forum/viewthread.htm post content page master template, this template file is the template/default/forum/viewthread_node.htm post content page template file loaded by the Post content page handler, this template file loops through one floor template/default/forum/viewthread_node_body.htm templates without editing the other two files. This is already described in the template creation method. 6. Plug-In hooks <! -- {Hook/index_top} --> Copy the code hook as a keyword to define index_top as a hook. The function of the plug-in hook is to allow the plug-in to output the relevant code in a specified position! When creating a template, we must make reasonable arrangements by referring to the plug-in Hook location in the default template, unless you think a plug-in Hook is not required in your own template, otherwise, keep the plug-in Hook code.

 

Template parsing variables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.