Dedecms tag overview

Source: Internet
Author: User
Tags php basics
Topic homepage: get the id of the current topic {dede: fieldnameid/} current topic name: {dede: fieldnametypename/} topic list page: get the id of the current topic {dede: fieldnameid/} current topic name: {dede: fieldnametypename/} article content page: get the id of the current article: {dede: fieldna Topic homepage:
Obtain the id of the current topic {dede: field name = 'id '/}
Current topic name: {dede: field name = 'typename '/}

Topic list page:
Obtain the id of the current topic {dede: field name = 'id '/}
Current topic name: {dede: field name = 'typename '/}

Article content page:
Get the id of the current article: {dede: field name = 'id '/}
Topic name of the current article: {dede: field name = 'typename '/}

Other labels:
Counter used for loop and traversal (each time + 1): [field: global name = autoindex/]
Truncate the specified number of bytes in Chinese: [field: title function = 'CN _ substr (@ me, 16) '/]

{Dede: SQL} label:
The SQL tag can be called a universal tag, and the query database outputs it:

  1. used to output statisticsThis is a good example. for example, we will calculate the total number of articles published. The idea is to output the total number of additional table content in dede_addonarticle.
{Dede: SQL = "SELECT COUNT (*) AS nums FROMdede_addonarticle "}
[Field: name = "nums"/]
{/Dede: SQL}

  2. use ~ Field ~ For special queriesFor example, in the previous forum, many members needed to output the relevant content of the current published article member. Previously, the SQL or arclist tag cannot be used, because the content of each article is different, here we will use 5.5 SQL statements for demonstration.
First, add the following tag to the corresponding position in the article template.
{Dede: SQL = "SELECT * FROM dede_archives WHEREwriter =~ Writer ~"}
[Field: id/],
{/Dede: SQL}
This article will be displayed on the article_article.htm page, and relevant articles published by the current member will be retrieved ~ Writer ~ The system replaces the environment variables of the current content before performing the query.
Here, the conditional query in the SQL statement is ~ Writer ~, That is, related content in $ refObj-> Fields [$ value]

{Dede: php} tag:
Many people hope that the template tag of Zhimeng can be more flexible and can run PHP. here, a special tag {dede: php} {/dede: is added to DedeCMSV5.5: php}. this label can execute PHP statements.

Here are some common examples:

  1. the simplest output content:
{Dede: php}
$ NumA = 1;
$ NumB = 2;
Echo $ numA + $ numB;
{/Dede: php}
The output content is the calculation result: 3

  2. query and output a single piece of content in conjunction with SQL
{Dede: php}
$ Row = $ dsql-> GetOne ('select id, typename from dede_arctypewhere id = 2 ');
Print_r ($ row );
{/Dede: php}
The output content is
Array
(
[Id] => 2
[Typename] => Q &
)

  3. get the variables on the current page
For example, we enter [template]-[global tag test] in the system background, and fill in the following content in the code:
{Dede: php}
Print_r ($ refObj-> Fields );
{/Dede: php}
If the environment variable remains the default value, that is, "Do not use the environment ID", we will see the following results:
Array
(
[Typeid] => 0
[Phpurl] =>/plus
[Indexurl] =>/
[Templeturl] =>/templets
[Memberurl] =>/member
[Specurl] =>/special
[Indexname] => Home Page
[Templetdef] =>/templets/default
)
Let's test the environment variables again. here we use my local column for testing:
Array
(
[Id] => 3
[Reid] => 0
[Topid] => 0
[Sortrank] => 1
[Typename] => Product
[Typedir] =>{ cmspath}/product
... ...
[Indexname] => Home Page
[Templetdef] =>/templets/default
[Position] => Home Page> Product>
[Title] => Product
)
In this way, the local variables on the current page are retrieved. Next, we can use the preceding SQL statement to call different content columns based on different environment IDs.
For example:
{Dede: php}
$ Thisid = $ refObj-> Fields ['id'];
$ Row = $ dsql-> GetOne ('select id, typename from dede_arctypewhere id = '. $ thisid );
Print_r ($ row );
{/Dede: php}
This is to call the title of the current topic. this label function is similar to {dede: field. typename /}

Custom tag:
DedeCMS has supported tag extension since V5.3, but many new users seldom use it. today I will briefly introduce how to compile their own call tags.
We need to know the storage directory and file name structure of the extension label. First, the extension labels are stored in the/include/taglib directory, and the names are all "tag names. lib. php "format. for example, the {dede: channel/} tag corresponds to a channel. lib. php file.
We can see an example tag: demotag. lib. php
  
If (! Defined ('deinc '))
{
Exit ("Request Error! ");
}
Function lib_demotag (& $ ctag, & $ refObj)
{
Global $ dsql, $ envs;
// Attribute processing
$ Attlist = "row | 12, titlelen | 24 ";
FillAttsDefault ($ ctag-> cattriems-> Items, $ attlist );
Extract ($ ctag-> cattriact-> Items, EXTR_SKIP );
$ Revalue = '';
// The code you need to write. you cannot use the echo or other syntax to pass the final return value to $ revalue.
//------------------------------------------------------
$ Revalue = 'Hello Word! ';
//------------------------------------------------------
Return $ revalue;
}
?>
Log on to the system background and run {dede: demotag/} in [template]-[global tag test]. the following result is displayed:
 
Here we know that the content generated by the tag is actually a return value of this function. The Content returned here is a string, that is, the return $ revalue function; $ revalue must be a string generated after processing.
$ Attlist = "row | 12, titlelen | 24"; this is the attribute list. after the function is processed, the variable is generated and copied. we can test and make the following changes:
$ Revalue = 'Hello Word! ';
$ Revalue. = "Row:". $ row. "; TitleLen:". $ titlelen;
 
In this way, we can see that this property has been created with variables and assigned values.
Next, we can further modify the label.
For example, we need to write a tag to query the relevant article on the article content page. the function is similar to the SQL statement in the preceding SQL tag, but here we split it into a tag.
We can create a new tag, for example, writerarc. then we need to create a writerarc. lib. php, and then imitate demotag to write a function. Note that we need to modify it
Function lib_writerarc (& $ ctag, & $ refObj)
Next, we can write query statements and related functions for processing the underlying template.
$ Revalue = '';
$ Innertext = $ ctag-> GetInnerText ();
$ Ctp = new DedeTagParse ();
$ Ctp-> SetNameSpace ('field', '[', ']');
$ SQL = "SELECT * FROM dede_archives WHEREwriter = '{$ refObj-> Fields ['write']}' limit 0, $ row ";
$ Innertext: this is the underlying template used to obtain tags. $ ctp is created to process the variables in the underlying template and process and replace them. We compile our SQL statement based on the obtained attributes. here we use limit0 and $ row to determine the number of queries based on $ row.
Of course, we can get more attributes so that our tag is more powerful. for example, we can add related attributes similar to arclist and process them in functions, however, this requires some PHP Basics.
Next we will process the SQL and output variables by executing the query:
$ Dsql-> Execute ('me', $ SQL );
While ($ rs = $ dsql-> GetArray ('me '))
{
// Process query variables based on attributes
$ Rs ['title'] = cn_substr ($ rs ['title'], $ titlelen );
// Obtain the underlying Template
$ Ctp-> LoadSource ($ innertext );
Foreach ($ ctp-> CTags as $ tagid => $ ctag ){
If (! Empty ($ rs [strtolower ($ ctag-> GetName ()]) {
$ Ctp-> Assign ($ tagid, $ rs [$ ctag-> GetName ()]);
}
}
// Obtain the processing result based on the underlying template and query variable.
$ Revalue. = $ ctp-> GetResult ();
}
In this way, we replace the query results with the variables in the underlying template, generate the output string, and store all the string information to $ revalue.
Return $ revalue;
The content of the entire file is as follows:
  
If (! Defined ('deinc '))
{
Exit ("Request Error! ");
}
Function lib_writerarc (& $ ctag, & $ refObj)
{
Global $ dsql, $ envs;
// Attribute processing
$ Attlist = "row | 12, titlelen | 24 ";
FillAttsDefault ($ ctag-> cattriems-> Items, $ attlist );
Extract ($ ctag-> cattriact-> Items, EXTR_SKIP );
$ Revalue = '';
$ Innertext = $ ctag-> GetInnerText ();
$ Ctp = new DedeTagParse ();
$ Ctp-> SetNameSpace ('field', '[', ']');
$ SQL = "SELECT * FROM dede_archives WHEREwriter = '{$ refObj-> Fields ['write']}' limit 0, $ row ";
$ Dsql-> Execute ('me', $ SQL );
While ($ rs = $ dsql-> GetArray ('me '))
{
// Process query variables based on attributes
$ Rs ['title'] = cn_substr ($ rs ['title'], $ titlelen );
// Obtain the underlying Template
$ Ctp-> LoadSource ($ innertext );
Foreach ($ ctp-> CTags as $ tagid => $ ctag ){
If (! Empty ($ rs [strtolower ($ ctag-> GetName ()]) {
$ Ctp-> Assign ($ tagid, $ rs [$ ctag-> GetName ()]);
}
}
// Obtain the processing result based on the underlying template and query variable.
$ Revalue. = $ ctp-> GetResult ();
}
Return $ revalue;
}
?>
Next, let's test our tag. modify the article_article.htm template and add the following tag code to it:
{Dede: writerarc row = '10' titlelen = '6 '}
[Field: title/]
{/Dede: writerarc}
View debugging http://www.dedecms.com/plus/view.php through dynamic browsing pages? Aid = 3. we will find that the tag is working and our content is output.

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.