[Drupal] Notes on some common theme Functions

Source: Internet
Author: User
Tags php template drupal

Drupal is an excellent open-source CMS. Its topic template customization function is also very powerful. Yesterday, I wrote functions commonly used in joomla templates. Of course, Drupal is indispensable. When developing modules, you don't have to look down. Most of the following functions and methods are mostly used for templates and debugging topics. The description is only applicable to Drupal front-end development because I am a front-end developer, I won't be ugly about the background program. By convention, the following code should be placed in the PHP tag. The functions collected below can be found in the official Drupal API documentation, specific address http://api.drupal.org/personal here lists some of the foreground template theme more practical important as follows:

 

1,Print arrays and objects: Print_r ();

1
 echo print_r($user);

Explanation: when developing a Drupal topic template, you will find print_r (); functions will always be available. When you need to call a value, you can consult the background engineers who work with you, you can also use the above Code to print it and find out what you need. In this example, the values of all user objects are printed. user IDs can be used to spell out the user's page path, user roles, and user portraits, of course, it contains the MD5 value of the user's password. To expand it, you can go to node. TPL. in the PHP template, print the Node object to completely split the information output by the UDF and the node body and other fields.

2,Determine whether a user logs in: Global $ user;

1
Global $ user; if ($ user-> UID) {/* the user has logged on */} else {/* the user has not logged on */}

Note: In this example, you can determine whether a user is logged on by determining the UID. This method can be extended to distinguish a specific user based on the specific value of the UID, for example, if ($ user-> uid = 1) is a super administrator.

3,Judge roles to differentiate: Global $ user;

1
Global $ user; If (in_array ('guest ', $ user-> roles) {/* User guest role */} else {/* not this role */}

Explanation: here, the role information is still obtained by reading the user object. Other applications can expand on their own, such as outputting user portraits and basic user information.

4,Determine whether a user has the editing permission:

1
If (node_access ('update', $ node) {/* has the edit permission */} else {/* Cannot edit this node */}

The example shows whether the current user has the edit permission on the node. if the user has the edit permission, the edit button or the link address is usually output.

5,Use different page templates by Node Type:

1
if( $node->type == 'blog' ){ include 'page-blog.tpl.php'; return; }

An explanation: we usually use a node template such as the node-blog.tpl.php format to render it separately based on the node content type, but the code in the example is required if you want to make the entire page different.

6,Select different page templates by URL:

1
if( arg(0) == 'admin' ){ include 'admin.tpl.php'; return; }

Explanation: the URL here must be the internal path of Drupal, rather than the path after being rewritten through the URL of path, by default, the template engine first looks for the template file by path, such as the page-admin.tpl.php, if not found, the page will be read. TPL. PHP then makes the sample code take effect.

7,REFERENCE The Drupal path in an external Javascript file:

Explanation: if you introduce an external Javascript script file that is useful to the website path, you need to use the Drupal JS object provided by Drupal by default, drupal. settings ['basepath'] to directly call the root path of the current Drupal is equivalent to the $ base_path variable or the base_path () function used in PHP.

8,Define more block locations in the template:

123
Function yourtheme_regions () {return array ('day' => 'sidebar ', 'head' => 'page head', 'foot' => 'foot ', 'content' => 'content ');}

The code is stored in the template. php file in the template folder? PHP starts with a file.) on the "Management-block" page, you can define more regions. This may be one of the least-used concepts from joomla to Drupal.

9,Determine whether the current page is a homepage:

1
If ($ is_front) {/* homepage Code */}

Simple Explanation: by judging the Drupal variable is_front, you can directly determine whether the current page is a home page. This will be very useful when the structure style of the home page and the inner page is not much different and can easily reduce a large amount of repeated code.

10,Generate a page class id based on the URL to make it easier to use CSS to customize the appearance:

12
<body id="<?php echo 'path-',arg(0),'-',arg(1); ?>">  </body><div id="divBody" class="<?php echo 'path-',arg(0),'-',arg(1); ?>"><?php echo $content; ?></div>

Simple Explanation: although the topic customization of Drupal is very flexible, theme functions provided by most modules are very limited. If you only use page-* To adjust the style of a small part -*. TPL. PHP is obviously not worth the candle when developing modules separately, so you can use the above work ing method, in this way, a class or id named in the current path will be added to the page during loading, so that you can easily use the CSS level selector to modify the style (such as Div. path-node-Add a {color: # f00;} You can easily customize the link style of the "node/Add" page without the need to create a TPL or override function separately) this is exactly the solution adopted in Yahoo UI.

11,File Header information of the language Po:

12345678910
msgid ""msgstr """Project-Id-Version: drupal (6.10)/n""POT-Creation-Date: 2009-05-06 23:18 +0800/n""PO-Revision-Date: 2009-04-16 23:06 +0800/n""Language-Team: Chinese, Simplified/n""MIME-Version: 1.0/n""Content-Type: text/plain; charset=utf-8/n""Content-Transfer-Encoding: 8bit/n""Plural-Forms: nplurals=2; plural=(n!=1);/n"

This is the header information of Simplified Chinese (zh-Hans) in the Drupal localized Po file. Note that the final plural-forms attribute must not be written incorrectly.

12,Custom Search page with no returned results:

We know that the customized Drupal search result page can be found in phptemplate. in PHP, it is implemented by overwriting the theme_search_item ($ item, $ type) function. However, if the search does not return any results, Drupal is written to death, here, we implement the "check if your spelling is correct. check your spelling." .

Simply put, you can determine $ Title = T ('your search yielded no results') in the template file box. TPL. php to customize the search result-free page.

The topic functions that can be overwritten by phptemplate are not provided in the search module when no search results are returned, in most cases, in order to prevent the user from jumping out, some prompts or links are provided; so here we can go to box. TPL. in the PHP template, you can customize the content of the page by judging the title as "no result is found in the search.

This method can be used to customize the output content of all theme ('box', $ title, $ output) function rendering in other modules. TPL. PHP judges that the value of $ title is the same as the value of $ title output by this module (for example, theme ('box', T ('comment viewing options'), $ output) in the comment module) T ('comment viewing options') in it); however, it is not applicable to the section where $ title is not fixed, fortunately, most of the box-based rendering parts are fixed (for example, the "navigation" section of the menu management page submits the "submit new comment" section of the comment ).

13,Obtain the absolute path of the website: $ Base_path and base_path ();

Because Drupal uses relative paths for the entire site, when the website installation directory is not under the root directory (/) and we need to obtain an absolute path, we can use $ base_path (in page. TPL. in PHP) base_path () (in node. TPL. PHP.

14,Use imagecache to manually output formatted image output results:

12345
Print theme ('imagecache', $ preset, $ image ['filepath'], $ ALT, $ title, $ attributes); // $ preset: pre-defined imagecache actions in admin, such as scaling and cropping; // $ image ['filepath']: indicates the file path. You can use Upload or the content of the UDF; // $ alt $ title: indicates the alt and title attributes of an image. No substitute text and prompts are displayed when the image is browsed or disabled. // $ attributes: defines an array of other attributes, the class width and height of the slice;

We can use imagecache and kipre to conveniently set and dynamically generate various thumbnail caches, in the TPL template file, you can directly use the above theme function to call the preset actions in imagecache to generate the image cache in real time.

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.