Table of contents

  Previous article Next article  

Table of contents

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 1.11.4
  Last updated: 13-01-2015
  Comments: 2

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>

A2 Webhosting CMS Made Simple

  Comments

  Click here to leave a comment






This is a captcha-picture. It is used to prevent mass-access by robots. (see: www.captcha.net)
  « Enter the CAPTCHA code (lower case)

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!


Can I have your attention for a moment?

Did this tutorial help you solving a problem at your (clients) website and it saved you many hours of work? Great!!
Consider buying Rolf a cup of coffee in return! [ Click here ]


  2 Comments

CMS Made Simple 2.2.2 - Tutorials, tips and tricks - CMSMS 2.2.2

Table of contents

  Article optimized for CMSMS 2.x

  Author:
  Last tested in: CMSMS 1.11.4
  Last updated: 13-01-2015
  Comments: 2

A2 Webhosting CMS Made Simple