Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Issue #64: Register the helper() function through the helperable beha…
Browse files Browse the repository at this point in the history
…vior
  • Loading branch information
johanjanssens committed Apr 29, 2016
1 parent 0e71bea commit 696ee5d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
11 changes: 11 additions & 0 deletions code/template/behavior/helperable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@
*/
class TemplateBehaviorHelperable extends TemplateBehaviorAbstract
{
/**
* Register a helper() function in the template
*
* @param ViewContextInterface $context A view context object
* @return void
*/
protected function _beforeRender(TemplateContextInterface $context)
{
$context->subject->registerFunction('helper', array($this, 'invokeHelper'));
}

/**
* Invoke a template helper
*
Expand Down
1 change: 0 additions & 1 deletion code/template/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ protected function _initialize(ObjectConfig $config)
'filters' => array(),
'functions' => array(
'escape' => array(__NAMESPACE__.'\StringEscaper', 'escape'),
'helper' => array($this, 'invokeHelper'),
'parameter' => array($this, 'getParameter'),
'parameters' => array($this, 'getParameters')
),
Expand Down
98 changes: 98 additions & 0 deletions code/view/behavior/routable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Kodekit Component - https://www.timble.net/kodekit
*
* @copyright Copyright (C) 2007 - 2014 Johan Janssens and Timble CVBA. (https://www.timble.net)
* @license MPL v2.0 <https://www.mozilla.org/en-US/MPL/2.0>
* @link https://github.com/timble/kodekit-pages for the canonical source repository
*/

namespace Kodekit\Library;

/**
* Routable View Behavior
*
* @author Johan Janssens <https://github.com/johanjanssens>
* @package Kodekit\Library\View\Behavior
*/
class ViewBehaviorRoutable extends ViewBehaviorAbstract
{
/**
* Get a route based on a full or partial query string
*
* 'option', 'view' and 'layout' can be omitted. The following variations will all result in the same route :
*
* - foo=bar
* - component=[package]&view=[name]&foo=bar
*
* In templates, use route()
*
* @param string|array $route The query string or array used to create the route
* @param boolean $fqr If TRUE create a fully qualified route. Defaults to TRUE.
* @param boolean $escape If TRUE escapes the route for xml compliance. Defaults to TRUE.
* @return DispatcherRouterRoute The route
*/
public function getRoute($route = '', $fqr = true)
{
$parts = array();
$identifier = $this->getMixer()->getIdentifier();

if(is_string($route)) {
parse_str(trim($route), $parts);
} else {
$parts = $route;
}

//Check to see if there is component information in the route if not add it
if (!isset($parts['component'])) {
$parts['component'] = $identifier->package;
}

//Add the view information to the route if it's not set
if (!isset($parts['view'])) {
$parts['view'] = $this->getName();
}

//Add the format information to the route only if it's not 'html'
if (!isset($parts['format']) && $identifier->name !== 'html') {
$parts['format'] = $identifier->name;
}

//Add the model state and layout only for routes to the same view
if ($parts['component'] == $identifier->package && $parts['view'] == $this->getName())
{
$states = array();
foreach($this->getModel()->getState() as $name => $state)
{
if($state->default != $state->value && !$state->internal) {
$states[$name] = $state->value;
}
}

$parts = array_merge($states, $parts);

//Add the layout information
if(!isset($parts['layout']) && $this->getMixer() instanceof ViewTemplatable)
{
$layout = $this->getLayout();

if ($layout !== 'default') {
$parts['layout'] = $layout;
}
}
}

//Create the route
$escape = $this->getUrl()->isEscaped();
$route = $this->getObject('lib:dispatcher.router.route', array('escape' => $escape))->setQuery($parts);

//Add the host and the schema
if ($fqr === true)
{
$route->scheme = $this->getUrl()->scheme;
$route->host = $this->getUrl()->host;
}

return $route;
}
}

0 comments on commit 696ee5d

Please sign in to comment.