Add the possibility to attach template hooks with local variables

This commit is contained in:
Frederic Guillot
2016-08-13 18:08:46 -04:00
parent ffe61abc69
commit 2ebe8b3272
4 changed files with 57 additions and 17 deletions

View File

@@ -3,6 +3,7 @@ Version 1.0.33 (unreleased)
Improvements: Improvements:
* Add the possibility to attach template hooks with local variables
* Add "reference" hooks * Add "reference" hooks
* Show project name in task forms * Show project name in task forms
* Convert vanilla CSS to SASS * Convert vanilla CSS to SASS

View File

@@ -24,8 +24,8 @@ class HookHelper extends Base
{ {
$buffer = ''; $buffer = '';
foreach ($this->hook->getListeners($hook) as $file) { foreach ($this->hook->getListeners($hook) as $params) {
$buffer .= $this->helper->asset->$type($file); $buffer .= $this->helper->asset->$type($params['template']);
} }
return $buffer; return $buffer;
@@ -43,8 +43,12 @@ class HookHelper extends Base
{ {
$buffer = ''; $buffer = '';
foreach ($this->hook->getListeners($hook) as $template) { foreach ($this->hook->getListeners($hook) as $params) {
$buffer .= $this->template->render($template, $variables); if (! empty($params['variables'])) {
$variables = array_merge($variables, $params['variables']);
}
$buffer .= $this->template->render($params['template'], $variables);
} }
return $buffer; return $buffer;
@@ -54,13 +58,18 @@ class HookHelper extends Base
* Attach a template to a hook * Attach a template to a hook
* *
* @access public * @access public
* @param string $hook * @param string $hook
* @param string $template * @param string $template
* @param array $variables
* @return $this * @return $this
*/ */
public function attach($hook, $template) public function attach($hook, $template, array $variables = array())
{ {
$this->hook->on($hook, $template); $this->hook->on($hook, array(
'template' => $template,
'variables' => $variables,
));
return $this; return $this;
} }
} }

View File

@@ -153,6 +153,14 @@ Example to add new content in the dashboard sidebar:
$this->template->hook->attach('template:dashboard:sidebar', 'myplugin:dashboard/sidebar'); $this->template->hook->attach('template:dashboard:sidebar', 'myplugin:dashboard/sidebar');
``` ```
Example to attach a template with local variables:
```php
$this->template->hook->attach('template:dashboard:sidebar', 'myplugin:dashboard/sidebar', array(
'variable' => 'foobar',
));
```
This call is usually defined in the `initialize()` method. This call is usually defined in the `initialize()` method.
The first argument is name of the hook and the second argument is the template name. The first argument is name of the hook and the second argument is the template name.

View File

@@ -6,6 +6,28 @@ use Kanboard\Helper\HookHelper;
class HookHelperTest extends Base class HookHelperTest extends Base
{ {
public function testAttachLocalVariables()
{
$this->container['template'] = $this
->getMockBuilder('\Kanboard\Core\Template')
->setConstructorArgs(array($this->container['helper']))
->setMethods(array('render'))
->getMock();
$this->container['template']
->expects($this->once())
->method('render')
->with(
$this->equalTo('tpl1'),
$this->equalTo(array('k0' => 'v0', 'k1' => 'v1'))
)
->will($this->returnValue('tpl1_content'));
$hookHelper = new HookHelper($this->container);
$hookHelper->attach('test', 'tpl1', array('k1' => 'v1'));
$this->assertEquals('tpl1_content', $hookHelper->render('test', array('k0' => 'v0')));
}
public function testMultipleHooks() public function testMultipleHooks()
{ {
$this->container['template'] = $this $this->container['template'] = $this
@@ -32,10 +54,10 @@ class HookHelperTest extends Base
) )
->will($this->returnValue('tpl2_content')); ->will($this->returnValue('tpl2_content'));
$h = new HookHelper($this->container); $hookHelper = new HookHelper($this->container);
$h->attach('test', 'tpl1'); $hookHelper->attach('test', 'tpl1');
$h->attach('test', 'tpl2'); $hookHelper->attach('test', 'tpl2');
$this->assertEquals('tpl1_contenttpl2_content', $h->render('test')); $this->assertEquals('tpl1_contenttpl2_content', $hookHelper->render('test'));
} }
public function testAssetHooks() public function testAssetHooks()
@@ -64,11 +86,11 @@ class HookHelperTest extends Base
) )
->will($this->returnValue('<script src="skin.js"></script>')); ->will($this->returnValue('<script src="skin.js"></script>'));
$h = new HookHelper($this->container); $hookHelper = new HookHelper($this->container);
$h->attach('test1', 'skin.css'); $hookHelper->attach('test1', 'skin.css');
$h->attach('test2', 'skin.js'); $hookHelper->attach('test2', 'skin.js');
$this->assertContains('<link rel="stylesheet" href="skin.css"></link>', $h->asset('css', 'test1')); $this->assertContains('<link rel="stylesheet" href="skin.css"></link>', $hookHelper->asset('css', 'test1'));
$this->assertContains('<script src="skin.js"></script>', $h->asset('js', 'test2')); $this->assertContains('<script src="skin.js"></script>', $hookHelper->asset('js', 'test2'));
} }
} }