table of contents + contemplate? [auto-toc]
Architeck - November 3, 2008 - 20:42
| Project: | Table of Contents |
| Version: | 6.x-3.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | AlexisWilke |
| Status: | closed |
Jump to:
Description
Is it possible to have the render through a contemplate or a .tpl.php template file.
I need to have the table of contents show on every page of a specific content type, without requiring the user to add the code as they publish the page.
thanks in advance to any response

#1
Well, right now this is all through the input filter system. It could be done with hook_nodeapi where if the content type matches, it passes $node->body through the input filter manually. Doing it through a contemplate or template file probably won't work well, because the results won't be cached.
Another way (probably even better) would be to add a list of content types to the filter settings page (in the unstable version) where if selected, would add the table at the top regardless of the presence of a tag.
You might also be able to implement this yourself without any module changes. In your node-type.tpl.php file, try something like the following:
if (module_exists('tableofcontents')) {$body = tableofcontents_filter('prepare', 0, -1, $node->body);
$body = tableofcontents_filter('process', 0, -1, $body);
}
Though that's not really a clean solution. It would cause issues on larger sites as the filter wouldn't be cached.
Let me know how this goes; if we don't find an easy solution without modifying the module code, we can see about writing something for the 2.4 release.
--Andrew
#2
Hey Andrew, Thanks for your help,
I tried the snippet of code your provided added to node-type.tpl.php file, made some changes and got it to work.
Here is the modified code.
<?php
if (module_exists('tableofcontents')) {
$content = '<!--tableofcontents-->'.$content;
$content = tableofcontents_filter('prepare', 0, -1, $content);
$content = tableofcontents_filter('process', 0, -1, $content);
}
?>
thanks again, hope this helps anyone else trying to achieve the same functionality.
It would be nice to have an option to automatically tag the tableofcontents onto specific content types.
For now this may be something to add into the readme documentation.
#3
I'd second that, although the above hack works it would be very useful to be able to automatically insert the TOC based on content type.
#4
Having the TOC automatically appear if any h2 exists on the page is a very helpful feature. By using the code in #2, the page execution time doubles, which is detrimental to a large site.
Before code in #2 used:
Page execution time was 299.87 ms.
Page execution time was 327.9 ms.
After code in #2 used:
Page execution time was 636.62 ms.
Page execution time was 603.59 ms.
Will you guide me and help me narrow down which function I should patch so that the TOC table shows up regardless of the presence of a tag? Then I'll be able to write a patch to help display TOC by content type.
Thanks!
#5
subscribing
#6
subscribing
#7
This feature will be helpful. Subscribing.
#8
This feature will be integrated in the TOC filter. And hopefully the speed will be improved, but no guarantee there. 8-)
#9
I added a drop down in the settings of the filter so people can get an automatic TOC in 3.x-dev.
It cannot be made specific to a node type (unfortunately) because it is a filter and filters may be applied to anything even non-node data. (i.e. blocks)
If you want to make it specific to a node type, use this module: http://drupal.org/project/filterbynodetype and assign only filters that make use of the automatic TOC on the corresponding node type.
Thank you.
Alexis Wilke
#10
Automatically closed -- issue fixed for 2 weeks with no activity.
#11
Thanks Alexis.
Just to confirm, with the new release, any H2 and H3 tags on a page are included in the TOC? I have several that are in contempalte but not inside the node body.
#12
giorgio79,
No, if it is only defined in your template, then it won't be found by the TOC module. The TOC module is a filter that works on the BODY of the node. Nothing else.
Thank you.
Alexis Wilke
#13
Thanks Alexis, appreciate you taking your time and responding.
Would you have any tips or ideas that I can explore or write a patch regarding how to make this work with contemplate.
As I understand, instead of the node body variable, the module could be looking at the entire contemplate output as a variable and scan it for H2 and H3 tags. :)
Do you think this would be a big hack, and in turn would you accept such a patch if I make one?
Cheers,
G
#14
Well... it isn't really clear. Is the ConTemplate using a filter to generate the final output? If so, you just have to put the TOC filter after the contemplate filter.
Reading their info, it did not feel like that was going to work. It seems that the way it works is by using a template.php file which means they get data that has already been filtered.
The will eventually be a problem since you could end up filtering multiple times (which does not work properly in many situations.)
I generally accept patches as long as they don't require Drupal Core changes that are not bugs. 8-)
Alexis
#15
Ah! Thinking about it, you may be able in your PHP code handling your contemplate to call a function in tableofcontents_...() to get it filtered for a TOC. That should be enough...
#16
Thanks Alexis, I am starting to see the light.
Now I just need to find out what variable holds the contemplate output.
Gathering more info over here:
#617488: Make Contemplate work with Table of Contents
#17
Ah I think I will just go with #1 and #2 :) That seems the simplest hack for the node template file, and get the TOC working for the entire $node->content :)