Skip to content

Commit 2b0f02d

Browse files
committed
refactor(container): Simplify content handling in DotCMSExtension
- Updated generateHtmlBasedOnProperty method to accept Contentlet type for improved type safety. - Removed unused getContainersData method and streamlined container content handling in Twig templates. - Enhanced README to reflect changes in functionality.
1 parent f70b071 commit 2b0f02d

1 file changed

Lines changed: 21 additions & 58 deletions

File tree

examples/dotcms-symfony/README.md

Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ use Twig\Environment;
298298
use Twig\Extension\AbstractExtension;
299299
use Twig\TwigFunction;
300300
use InvalidArgumentException;
301-
use RuntimeException;
302301
use Dotcms\PhpSdk\Utils\DotCmsHelper;
302+
use Dotcms\PhpSdk\Model\Content\Contentlet;
303303
304304
class DotCMSExtension extends AbstractExtension
305305
{
@@ -312,7 +312,6 @@ class DotCMSExtension extends AbstractExtension
312312
{
313313
return [
314314
new TwigFunction('getGridClass', [$this, 'getGridClass']),
315-
new TwigFunction('getContainersData', [$this, 'getContainersData']),
316315
new TwigFunction('generateHtmlBasedOnProperty', [$this, 'generateHtmlBasedOnProperty'], ['is_safe' => ['html']]),
317316
new TwigFunction('htmlAttr', [$this, 'htmlAttr'], ['is_safe' => ['html']])
318317
];
@@ -332,59 +331,27 @@ class DotCMSExtension extends AbstractExtension
332331
};
333332
}
334333
335-
public function generateHtmlBasedOnProperty(array $content): string
334+
public function generateHtmlBasedOnProperty(Contentlet $content): string
336335
{
337-
if (!isset($content['contentType'])) {
336+
if (empty($content)) {
338337
return '';
339338
}
340339
341-
$twig = $this->twig;
342-
$template = match($content['contentType']) {
343-
'Banner' => 'dotcms/content-types/banner.twig',
344-
'Product' => 'dotcms/content-types/product.twig',
345-
'Activity' => 'dotcms/content-types/activity.twig',
346-
default => ''
347-
};
348-
349-
if (empty($template)) {
350-
return DotCmsHelper::simpleContentHtml($content);
351-
}
352-
353-
try {
354-
return $twig->render($template, ['content' => $content]);
355-
} catch (\Exception $e) {
356-
return '';
340+
$contentType = $content->contentType;
341+
if ($contentType) {
342+
$template = 'dotcms/content-types/' . strtolower($contentType) . '.twig';
343+
if ($this->twig->getLoader()->exists($template)) {
344+
return $this->twig->render($template, ['content' => $content]);
345+
}
357346
}
358-
}
359347
360-
public function getContainersData(array $containers, array $containerRef): array
361-
{
362-
$identifier = $containerRef['identifier'] ?? throw new RuntimeException("Missing container identifier");
363-
$uuid = $containerRef['uuid'] ?? throw new RuntimeException("Missing container UUID");
364-
365-
$containerData = $containers[$identifier] ?? throw new RuntimeException("Container not found: $identifier");
366-
$structures = $containerData['containerStructures'] ?? [];
367-
$container = $containerData['container'] ?? [];
368-
369-
$contentlets = $containerData['contentlets']["uuid-$uuid"]
370-
?? $containerData['contentlets']["uuid-dotParser_$uuid"]
371-
?? [];
372-
373-
if (empty($contentlets)) {
374-
error_log("No contentlets found for container: $identifier, uuid: $uuid");
375-
}
376-
377-
return [
378-
...$container,
379-
'acceptTypes' => implode(',', array_column($structures, 'contentTypeVar')),
380-
'contentlets' => $contentlets,
381-
'variantId' => $container['parentPermissionable']['variantId'] ?? null
382-
];
348+
// Fall back to the SDK simple HTML renderer
349+
return DotCmsHelper::simpleContentHtml($content->jsonSerialize());
383350
}
384351
}
385352
```
386353

387-
This Twig extension provides utility functions for rendering DotCMS content in templates - handling container data extraction, content-type HTML generation, and proper attribute formatting for DotCMS elements.
354+
This Twig extension provides utility functions for rendering DotCMS content in templates - handling content-type HTML generation and proper attribute formatting for DotCMS elements.
388355

389356
### 6. Create Templates
390357

@@ -414,8 +381,7 @@ Create the necessary Twig templates to render DotCMS content:
414381
{% if column.containers is defined and column.containers is not empty %}
415382
{% for container in column.containers %}
416383
{% include 'dotcms/container.twig' with {
417-
'container': container,
418-
'containers': containers
384+
'container': container
419385
} %}
420386
{% endfor %}
421387
{% endif %}
@@ -442,19 +408,16 @@ Create the necessary Twig templates to render DotCMS content:
442408
#### Container Template (`templates/dotcms/container.twig`):
443409

444410
```twig
445-
{% set containerObject = getContainersData(containers, container) %}
446-
{% set containerContent = containers[container.identifier].contentlets['uuid-' ~ container.uuid]|default([]) %}
447-
448411
{% set containerAttrs = {
449412
'data-dot-object': 'container',
450-
'data-dot-identifier': containerObject.path|default(containerObject.identifier),
451-
'data-dot-accept-types': containerObject.acceptTypes,
452-
'data-max-contentlets': containerObject.maxContentlets,
413+
'data-dot-identifier': container.identifier,
414+
'data-dot-accept-types': container.acceptTypes,
415+
'data-max-contentlets': container.maxContentlets,
453416
'data-dot-uuid': container.uuid
454417
} %}
455418
456419
<div {{ htmlAttr(containerAttrs) }}>
457-
{% for content in containerContent %}
420+
{% for content in container.contentlets %}
458421
{% set contentAttrs = {
459422
'data-dot-object': 'contentlet',
460423
'data-dot-identifier': content.identifier,
@@ -463,10 +426,10 @@ Create the necessary Twig templates to render DotCMS content:
463426
'data-dot-inode': content.inode,
464427
'data-dot-type': content.contentType,
465428
'data-dot-container': {
466-
'acceptTypes': containerObject.acceptTypes,
467-
'identifier': containerObject.path|default(containerObject.identifier),
468-
'maxContentlets': containerObject.maxContentlets,
469-
'variantId': containerObject.variantId,
429+
'acceptTypes': container.acceptTypes,
430+
'identifier': container.identifier,
431+
'maxContentlets': container.maxContentlets,
432+
'variantId': container.variantId,
470433
'uuid': container.uuid
471434
}|json_encode
472435
} %}

0 commit comments

Comments
 (0)