Coder Social home page Coder Social logo

helpfulrobot / memdev-silverstripe-templatehooks Goto Github PK

View Code? Open in Web Editor NEW

This project forked from manuelteuber/silverstripe-templatehooks

0.0 2.0 0.0 6 KB

A simple template hook system for SilverStripe.

License: BSD 3-Clause "New" or "Revised" License

PHP 100.00%

memdev-silverstripe-templatehooks's Introduction

Template Hooks for SilverStripe

A simple template hook system for SilverStripe.

Latest Stable Version Total Downloads Latest Unstable Version License

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.

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.

Requirements

  • silverstripe/framework 3.2+

Installation

$ composer require memdev/silverstripe-templatehooks

You'll need to do a flush by appending ?flush=1 to your site's URL.

Usage

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

<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:

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:

<% 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:

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

Documentation

TODO

Reporting Issues

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

memdev-silverstripe-templatehooks's People

Contributors

manuelteuber avatar

Watchers

James Cloos avatar helpfulrobot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.