Skip to content

Commit

Permalink
Merge pull request #79 from thelia-modules/smarty_plugin
Browse files Browse the repository at this point in the history
Add a smarty plugin to get render from json
  • Loading branch information
lopes-vincent committed Jun 3, 2022
2 parents 361c792 + 2d9b4ec commit 4a8a730
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 14 deletions.
5 changes: 5 additions & 0 deletions Config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd">

<services>
<service id="theliablocks.json.block" alias="TheliaBlocks\Service\JsonBlockService" public="true">
</service>
</services>

<hooks>
<hook id="theliablocks.hook.menu" class="TheliaBlocks\Hook\TheliaBlocksMenuHook" scope="request">
<tag name="hook.event_listener" event="main.top-menu-tools" type="back" method="onMainTopMenuTools"/>
Expand Down
22 changes: 8 additions & 14 deletions Loop/BlockGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
use TheliaBlocks\Model\BlockGroupQuery;

/**
* Class BlockGroup
* @package TheliaBlocks\Loop
* Class BlockGroup.
*
* @method int[] getId()
* @method string[] getSlug()
* @method string getItemType()
* @method integer getItemId()
* @method bool|string getVisible()
* @method int[] getId()
* @method string[] getSlug()
* @method string getItemType()
* @method int getItemId()
* @method bool|string getVisible()
*/
class BlockGroup extends BaseI18nLoop implements PropelSearchLoopInterface
{
Expand Down Expand Up @@ -82,14 +81,9 @@ public function buildModelCriteria()

public function parseResults(LoopResult $loopResult)
{
/** @var \TheliaBlocks\Model\BlockGroup $entry */
/** @var \TheliaBlocks\Model\BlockGroup $entry */
foreach ($loopResult->getResultDataCollection() as $entry) {
$blockRenders = [];

foreach (json_decode($entry->getVirtualColumn('i18n_JSON_CONTENT'), true) as $block) {
$blockRenders[] = $this->container->get("thelia.parser")->render("blocks".DS.$block['type']['id'].".html", $block);
}
$htmlRender = implode(' ', $blockRenders);
$htmlRender = $this->container->get('theliablocks.json.block')->renderJsonBlocks($entry->getVirtualColumn('i18n_JSON_CONTENT'));

$content = json_decode($entry->getVirtualColumn('i18n_JSON_CONTENT'), true);

Expand Down
35 changes: 35 additions & 0 deletions Service/JsonBlockService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace TheliaBlocks\Service;

use TheliaSmarty\Template\SmartyParser;

class JsonBlockService
{
/** @var SmartyParser */
private $parser;

public function __construct(SmartyParser $parser)
{
$this->parser = $parser;
}

public function renderJsonBlocks($json): string
{
$blockRenders = array_map(function ($block) {
return $this->parser->render('blocks'.DS.$block['type']['id'].'.html', $block);
}, json_decode($json, true));

return implode(' ', $blockRenders);
}
}
44 changes: 44 additions & 0 deletions SmartyPlugin/JsonBlockRender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Thelia package.
* http://www.thelia.net
*
* (c) OpenStudio <info@thelia.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace TheliaBlocks\SmartyPlugin;

use TheliaBlocks\Service\JsonBlockService;
use TheliaSmarty\Template\AbstractSmartyPlugin;
use TheliaSmarty\Template\SmartyPluginDescriptor;

class JsonBlockRender extends AbstractSmartyPlugin
{
/** @var JsonBlockService */
private $jsonBlockService;

public function __construct(JsonBlockService $jsonBlockService)
{
$this->jsonBlockService = $jsonBlockService;
}

public function getPluginDescriptors()
{
return [
new SmartyPluginDescriptor('function', 'json_block_render', $this, 'renderJsonBlock'),
];
}

public function renderJsonBlock($params, $smarty): string
{
if (!isset($params['json'])) {
return '';
}

return $this->jsonBlockService->renderJsonBlocks($params['json']);
}
}

0 comments on commit 4a8a730

Please sign in to comment.