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.frontend.data_controller'
- '@netzmacht.contao_toolkit.repository_manager'
- '@templating'
netzmacht.contao_leaflet.libraries:
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>
L.contao.addMap('<?php echo $mapId; ?>', function () {
<?php echo $javascript; ?>
L.contao.addMap('<?= $this->mapId ?>', function () {
<?= $this->javascript ?>
return {map: map, layers: layers, controls: controls, icons: icons};
}());</script>
}());
</script>

View File

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