MENU

Table of contents

  Previous article Next article  

Generate a table of contents for your pages based on the heading tags. It requires, depending on how you want to use it, IDs or anchors in your page.

This article originates from the former CMSMS Wiki, the original authors are Elijah Lofgren and Simon Schaufelberger.

User Defined Tag, named "table_of_contents"
/**
 * Generates a table of contents based on parameters you want
 * @example: <h2 id="packages">Packages</h2> use: tag="h2" type="id"
 * @example: <a name="packages">Packages</a> use: tag="a" type="name"
 */

function get_table_of_contents($page_contents, $tag, $type)
{
  preg_match_all("/<$tag $type=\"([a-z-0-9]+)\">(.*)<\/$tag>/i", $page_contents, $match);
  if (false == empty($match[0]))
  {
    $contents = "<ul>\n";
    foreach ($match[1] as $key => $value)
    {
      if ($type == $match[1][$key])
      {
        $contents .= '';
      }
      else
      {
        $contents .= '        ';
      }
      $contents .= '<li><a href="'.$_SERVER['REQUEST_URI'].'#'.$match[1][$key].'">'.$match[2][$key].'</a>';
      // Start a new sub-list if this item has children otherwise just end the list item
      if (false == empty($match[1][$key + 1]) && $match[1][$key + 1] == $match[1][$key] + 1)
      {
        $contents .= "\n    <ul>\n";
        $unclosed_list = TRUE;
      }
      else
      {
        $contents .= "</li>\n";
      }
      // If the next item is higher level then close this list item
      // In example current item is h3 and next is h2
      if (false == empty($match[1][$key + 1]) && $match[1][$key + 1] == $match[1][$key] - 1)
      {
        $contents .= "    </ul>\n</li>\n";
        $unclosed_list = FALSE;
      }
    }
    if (false == empty($unclosed_list))
    {
      $contents .= "    </ul>\n</li>\n";
    }
    $contents .= "</ul>\n";
  }
  else
  {
    $contents = '';
  }
return $contents;
}

echo get_table_of_contents($params['thepagecontent'], $params['tag'], $params['type']);

  How to use

For use with IDs put in your template:
{content assign='pagecontent'}
{table_of_contents thepagecontent=$pagecontent tag='h2' type='id'}
{$pagecontent}
<h2 id="packages">Packages</h2>
For use with Anchors put in your template:
{content assign='pagecontent'}
{table_of_contents thepagecontent=$pagecontent tag='a' type='name'}
{$pagecontent}
<a name="packages">Packages</a>


  Show related articles:


  Comment Form






Note: your IP address will be stored on submit. I reserve the right to change or delete your response without notice! In case of abuse your IP address will be blocked!

GDPR privacy disclaimer
This form will send me your email address and your IP address.
If you don't want that, don't use this form!


  2 Comments

CMS Made Simple - Tutorials, tips and tricks - CMSMS

Table of contents

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 1.11.4
  Last updated: 23-06-2018
  Comments: 2
  http://cms.ms/wRuh

Advertisement



Ads help me to help you! Thanks!

Ads help me to help you! Thanks!

Ads help me to help you! Thanks!