OllieMackJames Posted April 12, 2018 Share Posted April 12, 2018 SOLVED - Thanks to @wbmnfktr for pointing out that this was already made possible with Hanna code, big thanks therefore to @ryan who even gave this as a working example of what could be done with Hanna code. Man this forum is awesome, this CMS is awesome, and now my site gets one step closer again to awesome for my visitors as well, thanks all! Leaving the original question below in case someone searches the forum for this exact same thing. ===========================original question below============================ I need a way to automate getting a table of contents on a really long page that I write. The sections on the page that need to end up in the TOC all have h2 headers. So I need the TOC module to loop through the page and take every instance of a h2 header and use that to buld the toc with clickable links to that particular h2 header Anybody have an idea how to do this? SOLVED 1 Link to comment Share on other sites More sharing options...
wbmnfktr Posted April 12, 2018 Share Posted April 12, 2018 @ryan provides exactly this kind of TOC/jumplinks as an example for the Hanna Code module. Take a look at the end of the page: http://modules.processwire.com/modules/process-hanna-code/ So you could use Hanna Code inside your pages or create something new from that example. Here is the code: <?php // $for defines the headlines you want to take care of. // i.e.: h2, h3, h4 $for = str_replace(',', ' ', $for); $for = explode(' ', $for); foreach($for as $k => $v) $for[$k] = trim($v); $for = implode('|', $for); $anchors = array(); $value = $hanna->value; if(preg_match_all('{<(' . $for . ')[^>]*>(.+?)</\1>}i', $value, $matches)) { foreach($matches[1] as $key => $tag) { $text = $matches[2][$key]; $anchor = $sanitizer->pageName($text, true); $anchors[$anchor] = $text; $full = $matches[0][$key]; $value = str_replace($full, "<a name='$anchor' href='#'></a>$full", $value); } $hanna->value = $value; } if(count($anchors)) { echo "<ul class='jumplinks'>"; foreach($anchors as $anchor => $text) { echo "<li><a href='$page->url#$anchor'>$text</a></li>"; } echo "</ul>"; } else { echo ''; } For better understanding you might want to install Hanna Code and or want to customize this for your needs. 3 Link to comment Share on other sites More sharing options...
bernhard Posted April 12, 2018 Share Posted April 12, 2018 Another option could be to use repeaters in your template. This might or might not be better and depends a lot on your situation... The TOC would be really simple then. Find operations might be harder... 1 Link to comment Share on other sites More sharing options...
OllieMackJames Posted April 12, 2018 Author Share Posted April 12, 2018 @wbmnfktr thanks lots, if I could get that to work, that would make me happy! This works beautifully [[jumplinks for=h2]] with your code did it all in one go! I tried the following code, but it would not work, can you see where I went wrong? <?php // $for defines the headlines you want to take care of. // i.e.: h2, h3, h4 $for = str_replace('h2', 'h3', $for); $for = explode(' ', $for); foreach($for as $k => $v) $for[$k] = trim($v); $for = implode('|', $for); $anchors = array(); $value = $hanna->value; if(preg_match_all('{<(' . $for . ')[^>]*>(.+?)</\1>}i', $value, $matches)) { foreach($matches[1] as $key => $tag) { $text = $matches[2][$key]; $anchor = $sanitizer->pageName($text, true); $anchors[$anchor] = $text; $full = $matches[0][$key]; $value = str_replace($full, "<a name='$anchor' href='#'></a>$full", $value); } $hanna->value = $value; } if(count($anchors)) { echo "<ul class='jumplinks'>"; foreach($anchors as $anchor => $text) { echo "<li><a href='$page->url#$anchor'>$text</a></li>"; } echo "</ul>"; } else { echo ''; } @bernhard thanks, I prefer not to go the route of repeaters here, as I just want to write the article and use h2 and h3 headers and then let Hanna deal with it. But thanks for chiming in, much appreciated. 3 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now