Skip to content

Commit

Permalink
Merge 1a6ee82 into 3a48e10
Browse files Browse the repository at this point in the history
  • Loading branch information
idmontie committed Sep 16, 2015
2 parents 3a48e10 + 1a6ee82 commit 8b65e0a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/view/extensions/handlebars-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ private function add_engine_helpers() {
self::add_html_special_chars_handler();
self::add_conditional_operators();
self::add_strip_tags();
self::add_config();
}

private function add_config() {
$this->engine->addHelper(
'config',
function ( $template, $context, $args ) {
$handlebars_arguments = new \Handlebars\Arguments( $args );
$arguments = $handlebars_arguments->getPositionalArguments();

$key = $arguments[0];

if ( count( $arguments ) > 1 ) {
$default = $arguments[1];
$value = Configuration::get_instance()->get( $key, $default );
} else {
$value = Configuration::get_instance()->get( $key );
}

return $value;
}
);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/simple-handlebars-view-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Nectary\Tests;

use Nectary\Views\Simple_Handlebars_View;
use Nectary\Configuration;

/**
* @group handlebars
Expand Down Expand Up @@ -186,4 +187,45 @@ function( $engine ) use ( $template ) {
$this->assertEquals( '', $output );
}

function test_config_will_return_configuration_value() {
$template = "{{#config myKey}}";

Configuration::get_instance()->set( 'myKey', 'myValue' );

$view = new Simple_Handlebars_View(
false,
'template_name',
function( $engine ) use ( $template ) {
return $engine->render(
$template,
[
]
);
}
);

$output = $view->output();

$this->assertEquals( 'myValue', $output );
}

function test_config_will_return_default() {
$template = "{{#config myKeyInvalid myDefault}}";

$view = new Simple_Handlebars_View(
false,
'template_name',
function( $engine ) use ( $template ) {
return $engine->render(
$template,
[
]
);
}
);

$output = $view->output();

$this->assertEquals( 'myDefault', $output );
}
}

0 comments on commit 8b65e0a

Please sign in to comment.