Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Create page anchor menu automatically


Go to End


2478 Views

Avatar
erwanpia

Community Member, 63 Posts

24 February 2009 at 9:20am

Edited: 21/03/2009 11:31pm

Hi, I have a nice hack here below I put it in this forum because I don't know where else to post it. I suggest you silverstripe guys create a hack forum

this one is an extension of Page that creates a function that automatically creates a menu from H1 titles within a page, and modify the content variable to create anchors and links. It's based on some plugin from modx (another nice cms getting old) and please note it has a global function and global varialbes so it's not very object but it works so enjoy

usage : in your template, place $AnchorMenu where you want the menu to be didplayed

<?php
	function findAnchors($matches)
{
	global $anchorTitles, $navbar,$premier;
	$start_tag=$matches[1];  //<h2>
	$h=trim($matches[2]);    //titre dans les balises <h2> </h2>
	$end_tag=$matches[3];    //</h2>
	 
	 $search= array ('/(id="(.*)")/sim');
	 $start_tag=preg_replace( $search, "", $start_tag);
	 
	$nom_ancre = html_entity_decode($h);
	 $search=(explode('::',wordwrap(" �����������������������������������������������������",1,'::',1))); 
	 $replac=(explode('::',wordwrap("-AAAAAAaaaaaaOOOOOOooooooEEEEeeeeIIIIiiiiUUUUuuuuyNnCc",1,'::',1))); 
	$nom_ancre=str_replace($search, $replac , $nom_ancre);
	
	$search=array('/<[\/\!]*?[^<>]*?>/si','/([\r\n])[\s]+/','/[^_A-Za-z0-9-]/','/-$/');
	$replace=array('');
	$anchor=preg_replace ($search, $replace,$nom_ancre);// vire les accents et divers caract�res
	
	$anchor= substr(strtolower($anchor),0,20); //$nom_ancre=strtolower(rawurlencode($nom_ancre));
	$anchorTitles[$h]=$anchor;        // sauvegarde du titre et de l'ancre pour le sommaire
	// don't write a navbar for the first title
	if ($premier) 
	{$str="\n$start_tag id='" .$anchor. "'> $h $end_tag\n";} 	
	 else  
	 { $str="\n$start_tag id='" .$anchor . "'> $h $end_tag\n" .   $navbar ;}
	 
	$premier=false;
	return $str;
}

class Page extends SiteTree {
	
	public static $db = array(
	);
	
	public static $has_one = array(
	);
	
 
	function Content()
	{
	global $anchorTitles, $navbar,$premier;
	$all_lines = $this->Content;
	$heading_level='1';
	$anchorTitles =array();
	$search= array ('/(<h'.$heading_level.'[^>]*)>(.+?)(<\/h'.$heading_level.'>)/sim');
	$all_lines_replaced=preg_replace_callback($search, 'findAnchors', $all_lines);  //replace les titre <h2> </h2> par <h2 id=""></h2>
		return $all_lines_replaced;
	}
	function AnchorMenu()
	{   
 	global $anchorTitles, $navbar,$premier;
	$all_lines = $this->Content;
	$heading_level='1';
	$anchorTitles =array();
	$search= array ('/(<h'.$heading_level.'[^>]*)>(.+?)(<\/h'.$heading_level.'>)/sim');
	$all_lines_replaced=preg_replace_callback($search, 'findAnchors', $all_lines);  //replace les titre <h2> </h2> par <h2 id=""></h2>
	foreach ($anchorTitles as  $nom_titre_h2 =>$ancre_h2 )
		$sommaire.= " <li><a href=\"".$_SERVER['REQUEST_URI']."#". $ancre_h2."\" onclick=\"new Effect.ScrollTo('$ancre_h2',{duration:1});return false;\">$nom_titre_h2</a></li>"; 
	
	return  $sommaire; 
	}
	

	
}