Skip to content
Browse files

Added support for arbitrary templates for rendering require initializ…

…ation
  • Loading branch information...
1 parent b3710ff commit a38177dc9123a93c67d79832001fd942bc83ee6e @kmontag kmontag committed Sep 29, 2011
View
4 DependencyInjection/Configuration.php
@@ -50,6 +50,10 @@ public function getConfigTreeBuilder()
->cannotBeEmpty()
->defaultValue('@HearsayRequireJSBundle/Resources/scripts')
->end()
+ ->scalarNode('initialize_template')
+ ->cannotBeEmpty()
+ ->defaultValue('HearsayRequireJSBundle::initialize.html.twig')
+ ->end()
->arrayNode('paths')
->defaultValue(array())
->useAttributeAsKey('path')
View
2 DependencyInjection/HearsayRequireJSExtension.php
@@ -51,6 +51,8 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('hearsay_require_js.base_url', $config['base_url']);
$container->setParameter('hearsay_require_js.base_directory', $this->getRealPath($config['base_directory'], $container));
+ $container->setParameter('hearsay_require_js.initialize_template', $config['initialize_template']);
+
// Set optimizer options
$container->setParameter('hearsay_require_js.r.path', $this->getRealPath($config['optimizer']['path'], $container));
foreach ($config['optimizer']['excludes'] as $exclude) {
View
2 Resources/config/services.yml
@@ -22,6 +22,7 @@
parameters:
hearsay_require_js.base_url: js
hearsay_require_js.base_directory: null # Set in the extension code
+ hearsay_require_js.initialize_template: null # Set in the extension code
hearsay_require_js.r.path: null # Set in the extension code
hearsay_require_js.module_formula_loader.class: Hearsay\RequireJSBundle\Factory\Loader\ModuleFormulaLoader
@@ -59,6 +60,7 @@ services:
arguments:
- '@templating'
- '@hearsay_require_js.configuration_builder'
+ - %hearsay_require_js.initialize_template%
tags:
- { name: templating.helper }
View
4 Resources/views/initialize.html.twig
@@ -28,11 +28,11 @@
# A module to require immediately when RequireJS is loaded.
#}
+{% if config is defined and config is not empty %}
{% block configuration %}
- {% if config is defined and config is not empty %}
<script type='text/javascript'>var require={{ config|json_encode|raw }}</script>
- {% endif %}
{% endblock configuration %}
+{% endif %}
{% block requirejs %}
{# Applications may override the requirejs_src block to use a custom URL. #}
View
14 Templating/Helper/RequireJSHelper.php
@@ -43,16 +43,24 @@ class RequireJSHelper extends Helper
* @var ConfigurationBuilder
*/
protected $configurationBuilder = null;
+ /**
+ * @var string
+ */
+ protected $initializeTemplate = null;
/**
* Standard constructor.
* @param EngineInterface $engine Templating engine.
- * @param ConfigurationBuilder $configurationBuilder Helper to get the live configuration.
+ * @param ConfigurationBuilder $configurationBuilder Helper to get the live
+ * configuration.
+ * @param string $initializeTemplate The template name to use for rendering
+ * initialization.
*/
- public function __construct(EngineInterface $engine, ConfigurationBuilder $configurationBuilder)
+ public function __construct(EngineInterface $engine, ConfigurationBuilder $configurationBuilder, $initializeTemplate)
{
$this->engine = $engine;
$this->configurationBuilder = $configurationBuilder;
+ $this->initializeTemplate = $initializeTemplate;
}
/**
@@ -78,7 +86,7 @@ public function initialize(array $options = array())
'configure' => true,
);
$options = array_merge($defaults, $options);
- return $this->engine->render('HearsayRequireJSBundle::initialize.html.twig', array(
+ return $this->engine->render($this->initializeTemplate, array(
'config' => $options['configure'] ? $this->configurationBuilder->getConfiguration() : null,
'main' => $options['main'],
));
View
14 Tests/Templating/Helper/RequireJSHelperTest.php
@@ -45,13 +45,13 @@ public function testDefaultInitialization()
$engine->expects($this->once())
->method('render')
- ->with('HearsayRequireJSBundle::initialize.html.twig', array(
+ ->with('template', array(
'config' => array('option' => 'value'),
'main' => null,
))
->will($this->returnValue('initialized'));
- $helper = new RequireJSHelper($engine, $config);
+ $helper = new RequireJSHelper($engine, $config, 'template');
$this->assertEquals('initialized', $helper->initialize(), 'Incorrect initialization rendered');
}
@@ -66,13 +66,13 @@ public function testConfigurationSuppressed()
$engine->expects($this->once())
->method('render')
- ->with('HearsayRequireJSBundle::initialize.html.twig', array(
+ ->with('template', array(
'config' => null,
'main' => null,
))
->will($this->returnValue('initialized'));
- $helper = new RequireJSHelper($engine, $config);
+ $helper = new RequireJSHelper($engine, $config, 'template');
$this->assertEquals('initialized', $helper->initialize(array('configure' => false)), 'Incorrect initialization rendered');
}
@@ -88,14 +88,14 @@ public function testMainScriptIncluded()
$engine->expects($this->once())
->method('render')
- ->with('HearsayRequireJSBundle::initialize.html.twig', array(
+ ->with('template', array(
'config' => array('option' => 'value'),
'main' => 'module',
))
->will($this->returnValue('initialized'));
- $helper = new RequireJSHelper($engine, $config);
- $this->assertEquals('initialized', $helper->initialize(array('main' => 'module')), 'Incorrect initialization rendered');
+ $helper = new RequireJSHelper($engine, $config, 'template');
+ $this->assertEquals('initialized', $helper->initialize(array('main' => 'module')), 'Incorrect initialization rendered');
}
}

0 comments on commit a38177d

Please sign in to comment.
Something went wrong with that request. Please try again.