Skin function functions. php in Typecho

Source: Internet
Author: User
The skin function functions. php in Typecho is very sorry. it has been very busy recently and has no time to update the content.
The previous article Typecho topic creation file structure describes the template files required to create a Typecho topic. there is a special file named "skin function" and the file name is functions. php ,. This document describes in detail the functions of this file.

When files are loaded

As described in "Widget_Archive parsing in Typecho", most of the request routes of Typecho are forwarded to Widget_Archive. in the execute () function, that is, about 1357 rows, there is the following code:

/** Initialize the skin function */$ functionsFile = $ this-> _ themeDir. 'functions. php'; if (! $ This-> _ invokeFromOutside & file_exists ($ functionsFile) {require_once $ functionsFile; if (function_exists ('themeinit ') {themeInit ($ this );}}

It can be seen that before Widget_Archive loads the target template file, it loads the functions. php file under the template directory and tries to execute the themeInit function, $ this as the parameter.

Special functions that can be defined in the file

The above mentioned themeInit function will be executed before all template files are executed, and can be considered as the first function to be executed in the template code. Similar special functions include:
* ThemeInit
* ThemeConfig
* ThemeConfigHandle
* ThemeFields
* ThreadedComments
* SinglePing
* TreeViewCategories

ThemeInit function
The themeInit function is loaded before all template files. it is generally used to configure Typecho behaviors, such as modifying the default pageSize to 20.

Function themeInit ($ archive) {if ($ archive-> is ('index') {$ archive-> parameter-> pageSize = 20; // number of custom entries }}

The function is to change the number of articles displayed on each page of the homepage from the default value to 20.

ThemeConfig function
This function is used to generate template configuration options in the background. For example, the following function will generate the configuration page.

Function themeConfig ($ form) {$ logoUrl = new Typecho_Widget_Helper_Form_Element_Text ('logurl', NULL, NULL, _ t ('site LOGO address '), _ t ('fill in an image URL here to add a LOGO before the website title '); $ form-> addInput ($ logoUrl ); $ sidebarBlock = new Typecho_Widget_Helper_Form_Element_Checkbox ('sidebarblock', array ('showrecentposts' => _ t ('Show latest Files '), 'showrecentcomments' => _ t ('Show recent response'), 'showcategory '=> _ t ('Show category '), 'showarchive' => _ t ('Show Archive'), 'showother '=> _ t ('show other miscellaneous'), array ('showrecentposts', 'showrecentcomments ', 'showcategory ', 'showarchive', 'showother'), _ t ('sidebar display'); $ form-> addInput ($ sidebarBlock-> multiMode ());}

The effects of the above code:

ThemeConfigHandle function
I have not figured out how to use it. thank you!

ThemeFields function
The role is similar to themeConfig, which has not been carefully studied.

ThreadedComments function
This function is used to configure the comment output. For example:

Function threadedComments ($ comments, $ options) {echo 'custom content 1'; $ comments-> content (); echo 'custom content 2 ';}

So ListComments ();?> Output:

 
 
  1. Custom Content 1 Comments content custom content 2
  2. Custom Content 1 Comments content custom content 2
  3. ...

SinglePing function
// I didn't know what it was about.

TreeViewCategories function
This function is used to modify the output style of a category. How to use it? I will try again later.

File can also define user-defined functions

In addition, since functions. php is loaded at the beginning of the template, you can define your own functions and use them in the template.
For example, in the most commonly used menu highlight function, we can define a function in functions. php to determine whether the current menu should be highlighted:

function active_current_menu($archive,$expected,$active_class='active'){    if($expected == 'index' && $archive.is('index')){        echo $active_class;    }else if($archive.is('archive') && $archive.getArchiveSlug() == $expected){        echo $active_class;    }else{        echo '';    }}

In the template file, you can use the following:

 
 
  • '> Homepage
  • '> Template
  • '> Plug-ins
  • ......

However, note that functions. php entered require in the execute function of Widget_Archive, so functions. you cannot directly use $ this for all functions defined in php, that is, you cannot directly access the internal variables of Widget_Archive. If you need to use the content of Widget_Archive, you can pass it in the form of function parameters, for example, the above example.

  • This article is reproduced from: www.typechodev.com

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.