Use template engine to render the map.

This commit is contained in:
David Molineus
2017-10-18 17:01:03 +02:00
parent 7498aef141
commit ce9de6ded3
4 changed files with 32 additions and 16 deletions

View File

@@ -16,6 +16,7 @@ services:
- '@netzmacht.contao_leaflet.cache' - '@netzmacht.contao_leaflet.cache'
- '@netzmacht.contao_leaflet.frontend.data_controller' - '@netzmacht.contao_leaflet.frontend.data_controller'
- '@netzmacht.contao_toolkit.repository_manager' - '@netzmacht.contao_toolkit.repository_manager'
- '@templating'
netzmacht.contao_leaflet.libraries: netzmacht.contao_leaflet.libraries:
class: Netzmacht\Contao\Leaflet\Frontend\Assets\LibrariesConfiguration class: Netzmacht\Contao\Leaflet\Frontend\Assets\LibrariesConfiguration

View File

@@ -1,6 +1,7 @@
<div id="<?php echo $mapId; ?>" style="<?php echo $style; ?>"></div> <div id="<?= $this->mapId ?>" style="<?= $this->style ?>"></div>
<script> <script>
L.contao.addMap('<?php echo $mapId; ?>', function () { L.contao.addMap('<?= $this->mapId ?>', function () {
<?php echo $javascript; ?> <?= $this->javascript ?>
return {map: map, layers: layers, controls: controls, icons: icons}; return {map: map, layers: layers, controls: controls, icons: icons};
}());</script> }());
</script>

View File

@@ -1,5 +1,5 @@
L.contao.addMap('<?php echo $mapId; ?>', function() { L.contao.addMap('<?= $this->mapId ?>', function() {
<?php echo $javascript; ?> <?= $this->javascript ?>
return { map: map, layers: layers, controls: controls, icons: icons }; return { map: map, layers: layers, controls: controls, icons: icons };
}()); }());

View File

@@ -23,10 +23,12 @@ use Netzmacht\Contao\Leaflet\Mapper\Request;
use Netzmacht\Contao\Leaflet\Model\LayerModel; use Netzmacht\Contao\Leaflet\Model\LayerModel;
use Netzmacht\Contao\Leaflet\Model\MapModel; use Netzmacht\Contao\Leaflet\Model\MapModel;
use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager; use Netzmacht\Contao\Toolkit\Data\Model\RepositoryManager;
use Netzmacht\Contao\Toolkit\View\Template\TemplateReference;
use Netzmacht\LeafletPHP\Definition\Map; use Netzmacht\LeafletPHP\Definition\Map;
use Netzmacht\LeafletPHP\Leaflet; use Netzmacht\LeafletPHP\Leaflet;
use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection; use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection;
use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface as EventDispatcher;
use Symfony\Component\Templating\EngineInterface as TemplateEngine;
/** /**
* Class MapProvider. * Class MapProvider.
@@ -91,6 +93,13 @@ class MapProvider
*/ */
private $repositoryManager; private $repositoryManager;
/**
* Template engine.
*
* @var TemplateEngine
*/
private $templateEngine;
/** /**
* Construct. * Construct.
* *
@@ -102,6 +111,7 @@ class MapProvider
* @param Cache $cache Cache. * @param Cache $cache Cache.
* @param DataController $dataController Data controller. * @param DataController $dataController Data controller.
* @param RepositoryManager $repositoryManager Repository manager. * @param RepositoryManager $repositoryManager Repository manager.
* @param TemplateEngine $templateEngine Template engine.
*/ */
public function __construct( public function __construct(
DefinitionMapper $mapper, DefinitionMapper $mapper,
@@ -111,7 +121,8 @@ class MapProvider
ContaoAssets $assets, ContaoAssets $assets,
Cache $cache, Cache $cache,
DataController $dataController, DataController $dataController,
RepositoryManager $repositoryManager RepositoryManager $repositoryManager,
TemplateEngine $templateEngine
) { ) {
$this->mapper = $mapper; $this->mapper = $mapper;
$this->leaflet = $leaflet; $this->leaflet = $leaflet;
@@ -121,6 +132,7 @@ class MapProvider
$this->cache = $cache; $this->cache = $cache;
$this->dataController = $dataController; $this->dataController = $dataController;
$this->repositoryManager = $repositoryManager; $this->repositoryManager = $repositoryManager;
$this->templateEngine = $templateEngine;
} }
/** /**
@@ -356,19 +368,21 @@ class MapProvider
protected function doGenerate($model, $filter, $elementId, $template, $style) protected function doGenerate($model, $filter, $elementId, $template, $style)
{ {
$definition = $this->getDefinition($model, $filter, $elementId); $definition = $this->getDefinition($model, $filter, $elementId);
$template = \Controller::getTemplate($template);
// @codingStandardsIgnoreStart - Set for the template.
$javascript = $this->leaflet->build($definition, $this->assets); $javascript = $this->leaflet->build($definition, $this->assets);
$mapId = $definition->getId(); $mapId = $definition->getId();
// @codingStandardsIgnoreEnd
ob_start(); $templateReference = new TemplateReference($template, 'html5', TemplateReference::SCOPE_FRONTEND);
include $template; $parameters = [
$content = ob_get_contents(); 'definition' => $definition,
ob_end_clean(); 'model' => $model,
'elementId' => $elementId,
'style' => $style,
'javascript' => $javascript,
'mapId' => $mapId,
];
$event = new GetJavascriptEvent($definition, $content); $content = $this->templateEngine->render($templateReference, $parameters);
$event = new GetJavascriptEvent($definition, $content);
$this->eventDispatcher->dispatch($event::NAME, $event); $this->eventDispatcher->dispatch($event::NAME, $event);
$buffer = $event->getJavascript(); $buffer = $event->getJavascript();