2017 © Pedro Peláez
 

silverstripe-module silverstripe-templatehooks

TemplateHooks enables developers to hook into user (=developer) defined hook points within SilverStripe template files.

image

memdev/silverstripe-templatehooks

TemplateHooks enables developers to hook into user (=developer) defined hook points within SilverStripe template files.

  • Thursday, April 21, 2016
  • by memdev
  • Repository
  • 2 Watchers
  • 0 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Template Hooks for SilverStripe

A simple template hook system for SilverStripe., (*1)

Latest Stable Version Total Downloads Latest Unstable Version License Build Status, (*2)

Sometimes extending / overriding a template is not enough or would produce a lot of duplicate markup. Maybe you just want to inject some markup at a specific point in your template file. This is where template hooks come into play., (*3)

With template hooks, you can add named "injection points" everywhere in your SilverStripe template files and hook into them from within your Controllers or DataObjects., (*4)

Requirements

  • silverstripe/framework 3.1+

Installation

$ composer require memdev/silverstripe-templatehooks

You'll need to do a flush by appending ?flush=1 to your site's URL., (*5)

Usage

To add a hook point to your template, simply call $TemplateHook(), providing a name for this hook as the first parameter:, (*6)

<div>
    <nav class="primary">
        <span class="nav-open-button">²</span>
        <ul>
            <% loop $Menu(1) %>
                <li class="$LinkingMode"><a href="$Link" title="$Title.XML">$MenuTitle.XML</a></li>
            <% end_loop %>
            $TemplateHook('MainNavigation')
        </ul>
    </nav>
    $TemplateHook('AfterMainNavigation')
</div>

You can subscribe to this hook by calling hookInto() in your Controller or DataObject:, (*7)

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}";
    }
}

You can also pass parameters with the template hook:, (*8)

<% loop $Menu(1) %>
    <li class="$LinkingMode">
        <a href="$Link" title="$Title.XML">
            $TemplateHook('MainNavItem', $ID)
            $MenuTitle.XML
        </a>
    </li>
<% end_loop %>

It will be available in your subscriber function:, (*9)

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

Documentation

TODO, (*10)

Reporting Issues

Please create an issue for any bugs you've found, or features you're missing., (*11)

The Versions

21/04 2016

dev-master

9999999-dev https://github.com/memdev/silverstripe-templatehooks

TemplateHooks enables developers to hook into user (=developer) defined hook points within SilverStripe template files.

  Sources   Download

BSD-3-Clause

The Requires

 

silverstripe hooks templatehooks template hooks

21/04 2016

1.0.1

1.0.1.0 https://github.com/memdev/silverstripe-templatehooks

TemplateHooks enables developers to hook into user (=developer) defined hook points within SilverStripe template files.

  Sources   Download

BSD-3-Clause

The Requires

 

silverstripe hooks templatehooks template hooks

17/04 2016

1.0.0

1.0.0.0 https://github.com/memdev/silverstripe-templatehooks

TemplateHooks enables developers to hook into user (=developer) defined hook points within SilverStripe template files.

  Sources   Download

BSD-3-Clause

The Requires

 

silverstripe hooks templatehooks template hooks