PHP code example of memdev / silverstripe-templatehooks

1. Go to this page and download the library: Download memdev/silverstripe-templatehooks library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

memdev / silverstripe-templatehooks example snippets


class Page_Controller extends ContentController implements TemplateHooks {

	/**
	 * Use this method to globally subscribe to template hooks.
	 * If you wish to subscribe to hooks in the current controller / object scope,
	 * call "hookInto()" from within any other method, e.g. the controllers init() method.
	 */
	public function initHooks()
	{
		$this->hookInto('MainNavigation', function($hook) {
			return SSViewer::execute_template('MyNavigationAppendix', array());
		});
	}

	public function init() {
		parent::init();

		$this->hookInto('AfterMainNavigation', array($this, 'AfterMainNavigationHook'));

		// OR

		$self = $this;
		$this->hookInto('AfterMainNavigation', function($hook) use ($self) {
		    return "You are currently reading page {$self->Title}";
		});
	}

	public function AfterMainNavigationHook($hook) {
	    return "You are currently reading page {$this->Title}";
	}
}

$this->hookInto('MainNavItem', function($hook, $id) {
    $page = Page::get()->byID($id);
    // your code here
}