Variables, loops, judgments, and file nesting that must be known during discuzx template Creation

Source: Internet
Author: User
Tags php template
Basic concepts of templates

Templates are various improvements to the original interface and framework of the program. The author collects the templates based on his own creations and functions, so that the program front-end can be displayed in a new form! The second enhancement and user experience of template creation hitting program functions
When creating a template, you must have basic HTML, css style, and simple template syntax. You can learn this by modifying the default template.
After learning how to create a style, you can try to create a template you need.
Features: it has an independent template directory and involves custom modifications to a large number of template files. Some templates have complicated installation processes.
A set of excellent templates requires the site management personnel to perform special data push and Data Display Methods Based on the template features they use.
No matter how good the template is, if the data pushed by the management personnel to the template is not enough to attract attention, it is also a failure! This article explains important links in template creation. The following content is also applicable to other open-source programs such as PHPwind, php168, and Ecshop, more trials, more hands-on, and more backups! Support for loading php template files added in DiscuzX2.5

From Discuz! Starting from X2.5, the template file supports the PHP extension format. The main function is to prevent the template from being stolen by other forums!
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 the HTML

You must add a line of code at the beginning of the php extension template file:

For example:

  1. <? Php exit;?>
Or:
  1. <? Php echo 'you cannot view the content of this template '; exit;?>

The template data content of the PHP template file will be parsed from the second line of the file. When both the php and htm template files exist, the PHP template files will be parsed first (this mode can only be used with X2.5 and later versions !)

Template variable name: DiscuzX contains two variables
A variable named G: $ _ G [xxx]
G variable is the global variable of the program. In order to make the program more efficient and reduce unnecessary data acquisition, the program will unify frequently used variables under G variables, 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.

Another custom variable: $ xxx
Custom variables start with $ and start with a letter or underline, such as $ data, $ thread, $ post, $ forumlist, and $ threadlist!
Custom variables can be customized by the author in the program, or the program itself has been defined.

PS: when creating a template, we present the existing data of the program in another way, so we do not need to customize variables.

How to output variable data in the template:
The program reads data from a database or cache file, converts the data to an array, and loads the data into a template for output! Therefore, when calling the list data in the template and outputting the list data, we must write loop code to display the array data one by one!

The format of the cyclic code writing for custom variable data:
  1. <! -- {Loop $ data $ key $ value} -->
  2. <Li> $ key $ value </li>
  3. <! -- {/Loop} -->

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

Find the following code:
  1. <! -- {Eval $ postcount = 0;} -->
  2. <! -- {Loop $ postlist $ post} -->
  3. <! -- {If $ post ['invisable']! =-5} -->
  4. <! -- {If $ rushreply & $ _ G ['gp _ checkrush'] & $ post ['rewardfloor ']! = 1} -->
  5. <! -- {Eval continue;} -->
  6. <! -- {/If} -->
  7. <Div id = "post _ $ post [pid]">
  8. <! -- {Subtemplate forum/viewthread_node} -->
  9. </Div>
  10. <! -- {Eval $ postcount ++;} -->
  11. <! -- {/If} -->
  12. <! -- {/Loop} -->

This code is the circular code on the post page. Through the above [writing format of the circular code for custom variable data] We should be able to get a general idea of this circular code! 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 are $ post, for example, $ post ['authorid'] = author UID, $ post ['username'] = author username, etc, you can use $ post [xxx] to output corresponding data based on different field information, which is stored in 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 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!

The most important condition judgment in template creation (if else ):

In loop statements, we often need to present different data in other ways. In this case, we need to use condition judgment! Conditional judgment syntax has many methods, such as greater than, equal to, less than, not equal to, and whether a variable exists! If else shorthand method: if = if else = then, conditional judgment involves logic problems, so it requires a strong logical thinking. The following example may sound funny when you first read it. Please take a closer look!

From simple to complex examples: $ xxx variable exists:

  1. <! -- {If $ xxx} -->... <! -- {/If} -->
  2. PS: if the $ xxx variable exists, execute the intermediate code
$ Xxx variable greater than 1:
  1. <! -- {If $ xxx> 1} --> ...... <! -- {/If} -->
  2. PS: if the $ xxx variable is greater than 1, the intermediate code is executed.
$ Xxx variable is less than 1:
  1. <! -- {If $ xxx <1} --> ...... <! -- {/If} -->
  2. PS: if the $ xxx variable is less than 1, the intermediate code is executed.

$ Xxx variable is not equal to 1:

  1. <! -- {If $ xxx! = 1} -->... <! -- {/If} -->
  2. PS: if the $ xxx variable is not equal to 1, the intermediate code is executed.
A little more complicated (if so ):
  1. <! -- {If $ xxx = 1} --> a <! -- {Else} --> B <! -- {/If} -->
  2. PS: if the $ xxx variable is equal to 1, code a is executed. If $ xxx is not equal to 1, code B is executed.

More complicated (if so)

  1. <! -- {If $ xxx = 1} --> a <! -- {Elseif $ xxx = 2} --> B <! -- {Elseif $ xxx = 3} --> c <! -- {/If} -->
  2. PS: if the $ xxx variable is equal to 1, execute code,
  3. Then, if $ xxx is not equal to 1 or 2, code B is executed,
  4. If $ xxx is not equal to 1 or 2 is equal to 3, execute c code. A little dizzy?

A little more complicated (ifelse two condition judgment)

  1. <! -- {If $ xxx = 1 | $ xxx = 3} --> a <! -- {Elseif $ xxx = 2} --> B <! -- {/If} -->
  2. PS: if the $ xxx variable is equal to 1 or $ xxx is equal to 3, run code,
  3. If $ xxx is not equal to 1 or 3, and is equal to 2, code B is executed,

Two Variables

  1. <! -- {If $ xxx = 1 | $ ooo = 1} --> a <! -- {Elseif $ xxx &&! $ Ooo} --> B <! -- {/If} -->
  2. PS: if the $ xxx variable is equal to 1 or $ ooo is equal to 1, run code,
  3. If $ xxx is not equal to 1, $ ooo is not equal to 1, $ xxx variable exists, and $ ooo variable does not exist, execute B code.

After reading the ifelseexample, click <! -- {If $ post ['invisable']! =-5} -->... <! -- {/If} --> according to the ifelse annotation, we can now understand the meaning: when judging the loop output, if $ post ['invisable'] is not equal to-5, the loaded HTML code is executed.

Plug-in hook in the template file (plug-in embedding point): plug-in hook code example:
  1. <! -- {Hook/xxx_xxx} -->

The function of the plug-in Hook is to allow the plug-in to output relevant code at specified locations!
In DZ7.x-DX1.5, plug-in hooks are not very important. However, with the continuous popularization of plug-in applications and the continuous addition of plug-in creators, plug-in hooks play a particularly important role in templates, if the required plug-in Hook is missing, the built-in system plug-in function may be affected!

Therefore, when creating a template, make sure to 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, keep the plug-in hook code!

The template file of DiscuzX is nested N times:

In the template file template/default/forum/viewthread.htm, We 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 is the template file of DiscuzX nested N times!

First of all, we need to clearly understand what template files are used to understand the meaning of these nesting:
Template/default/forum/viewthread.htm main template of the post content page. This template file is loaded by the post content page handler.
Template/default/forum/viewthread_node.htm post content page floor template file. This template file is a floor once in a loop
Template/default/forum/viewthread_node_body.htm post content page, post content template, this template file is specially used to process post content

Other, instead of editing the other two files.

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.