[Drupal] How to theme a node in Drupal 7 website?

| | 2 min read

If you are a beginner in Drupal your might be wondering what a node is. The concept of a node is very simple. All you have to understand is that each and every content in Drupal is treated as a node. A page is a node, an article is also a node. If you want to add custom styles and formatting to the nodes of a specific content type then you have to theme a node. Read on to know how to theme a node in a Drupal 7 website.

Steps to theme a node in Drupal 7

  1. First you have to create a node--content_type_name.tpl.php file inside your sites/all/themes/your_theme_name folder. For example, If you have to theme a node of type - Article, the name of template should be node--article.tpl.php.
  2. Next we need to add the fields inside this node template file. For example, if your article has the fields title, subtitle and body, you must have access to these fields inside your template file to theme these fields
  3. The fields appear inside the template file as variables.

    Some of the commonly available variables are:
    • $content - An array of node items.
    • $title - Title of the node.
    • $node - Full node object.
    • $type - Type of node.
  4. The field variables are printed as shown below
    print render($title); //prints title
    print render('$content['body']); //prints body
    print render($content['field_subtitle'])//prints body
  5. There are other variables like the node status variables available inside the node template file.
    Some of them are
    • $teaser – Returns a boolean value, if the node view is a teaser
    • $page – Returns a boolean value if the node view is a page.

You can also install the Drupal devel module and use the dpm() function to print the variables. dpm($node) prints all the relevant details about a node.