Skip to content

Commit fe23184

Browse files
committed
feat(view): Render the dotcms page with all it components for content types and grid system
- Introduced DotCmsHelpers class with methods for handling container data and generating HTML attributes. - Created DotCmsHelpersServiceProvider to register Blade directives for helper methods. - Updated AppServiceProvider to include DotCmsHelpersServiceProvider for enhanced view functionality. - Refactored AppController to utilize new helper methods for improved page rendering. - Enhanced app view with new layout and components for better structure and maintainability. - Create blade templates for each content types - Add CSS for the grid system.
1 parent e2dd881 commit fe23184

20 files changed

Lines changed: 407 additions & 296 deletions
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use App\Providers\DotCMSHelpersServiceProvider;
2+
3+
$this->app->register(DotCMSHelpersServiceProvider::class);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Illuminate\Support\Facades\View;
7+
use App\Helpers\DotCmsHelpers;
8+
9+
class DotCMSHelpersServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* Register services.
13+
*/
14+
public function register(): void
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Bootstrap services.
21+
*/
22+
public function boot(): void
23+
{
24+
// Share the DotCmsHelpers with all views
25+
View::share('dotCmsHelpers', new DotCmsHelpers());
26+
}
27+
}

bootstrap/providers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
App\Providers\DotCMSHelpersServiceProvider::class,
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace App\Helpers;
4+
5+
class DotCmsHelpers
6+
{
7+
/**
8+
* Get container data from the containers array
9+
*
10+
* @param array $containers
11+
* @param array $container
12+
* @return array|null
13+
*/
14+
public function getContainersData($containers, $container)
15+
{
16+
if (empty($containers) || empty($container)) {
17+
return null;
18+
}
19+
20+
$identifier = $container['identifier'] ?? null;
21+
22+
if (!$identifier || !isset($containers[$identifier])) {
23+
return null;
24+
}
25+
26+
return $containers[$identifier];
27+
}
28+
29+
/**
30+
* Generate HTML attributes from an associative array
31+
*
32+
* @param array $attributes
33+
* @return string
34+
*/
35+
public function htmlAttr($attributes)
36+
{
37+
if (empty($attributes)) {
38+
return '';
39+
}
40+
41+
$html = '';
42+
43+
foreach ($attributes as $key => $value) {
44+
if (is_bool($value)) {
45+
if ($value) {
46+
$html .= ' ' . $key;
47+
}
48+
} else {
49+
$html .= ' ' . $key . '="' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '"';
50+
}
51+
}
52+
53+
return $html;
54+
}
55+
56+
/**
57+
* Generate HTML based on contentlet properties
58+
*
59+
* @param array $content
60+
* @return string
61+
*/
62+
public function generateHtmlBasedOnProperty($content)
63+
{
64+
if (empty($content)) {
65+
return '';
66+
}
67+
68+
// Check if we have a template to render
69+
$contentType = $content['contentType'] ?? '';
70+
if ($contentType) {
71+
$viewPath = 'dotcms.content-types.' . strtolower($contentType);
72+
if (view()->exists($viewPath)) {
73+
return view($viewPath, ['content' => $content])->render();
74+
}
75+
}
76+
77+
// Default rendering with title
78+
$title = $content['title'] ?? $content['name'] ?? 'No Title';
79+
return '<div class="content-wrapper"><h3>' . htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . '</h3></div>';
80+
}
81+
}

examples/dotcms-laravel/app/Http/Controllers/AppController.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,33 @@ public function index(Request $request)
3838
$pageRequest = $this->dotCMSClient->createPageRequest($path, 'json');
3939

4040
// Get the page data
41-
$page = $this->dotCMSClient->getPage($pageRequest);
41+
$pageAsset = $this->dotCMSClient->getPage($pageRequest);
4242

4343
// Create a navigation request with depth=2
4444
$navRequest = $this->dotCMSClient->createNavigationRequest('/', 2);
4545

4646
// Get the navigation
4747
$nav = $this->dotCMSClient->getNavigation($navRequest);
4848

49+
// Check for entity wrapper in the response
50+
if (isset($pageAsset->entity)) {
51+
// Some dotCMS versions return data in an 'entity' wrapper
52+
$page = $pageAsset;
53+
} else {
54+
// Standard structure already expected by our templates
55+
$page = $pageAsset;
56+
}
57+
58+
// Log the structure for debugging
59+
Log::debug('DotCMS Page Structure', [
60+
'hasEntity' => isset($pageAsset->entity) ? 'yes' : 'no',
61+
'hasLayout' => isset($page->layout) ? 'yes' : 'no',
62+
'hasContainers' => isset($page->containers) ? 'yes' : 'no'
63+
]);
64+
4965
// Pass the data to the view
50-
return view('app', [
51-
'page' => $page,
66+
return view('page', [
67+
'pageAsset' => $page,
5268
'navigation' => $nav
5369
]);
5470
} catch (\Exception $e) {

examples/dotcms-laravel/app/Providers/AppServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\ServiceProvider;
66
use App\Providers\DotCMSServiceProvider;
7+
use App\Providers\DotCmsHelpersServiceProvider;
78

89
class AppServiceProvider extends ServiceProvider
910
{
@@ -13,6 +14,7 @@ class AppServiceProvider extends ServiceProvider
1314
public function register(): void
1415
{
1516
$this->app->register(DotCMSServiceProvider::class);
17+
$this->app->register(DotCmsHelpersServiceProvider::class);
1618
}
1719

1820
/**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Illuminate\Support\Facades\View;
7+
use App\Helpers\DotCmsHelpers;
8+
9+
class DotCmsHelpersServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* Register services.
13+
*/
14+
public function register(): void
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Bootstrap services.
21+
*/
22+
public function boot(): void
23+
{
24+
// Share the DotCmsHelpers with all views
25+
View::share('dotCmsHelpers', new DotCmsHelpers());
26+
}
27+
}

examples/dotcms-laravel/bootstrap/providers.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
return [
44
App\Providers\AppServiceProvider::class,
55
App\Providers\DotCMSServiceProvider::class,
6+
App\Providers\DotCmsHelpersServiceProvider::class,
67
];

examples/dotcms-laravel/resources/css/app.css

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,37 @@
1010
--font-sans: 'Instrument Sans', ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji',
1111
'Segoe UI Symbol', 'Noto Color Emoji';
1212
}
13+
14+
/* Grid System */
15+
.row {
16+
display: grid;
17+
grid-template-columns: repeat(12, 1fr);
18+
gap: 1rem;
19+
}
20+
21+
.col-start-1 { grid-column-start: 1; }
22+
.col-start-2 { grid-column-start: 2; }
23+
.col-start-3 { grid-column-start: 3; }
24+
.col-start-4 { grid-column-start: 4; }
25+
.col-start-5 { grid-column-start: 5; }
26+
.col-start-6 { grid-column-start: 6; }
27+
.col-start-7 { grid-column-start: 7; }
28+
.col-start-8 { grid-column-start: 8; }
29+
.col-start-9 { grid-column-start: 9; }
30+
.col-start-10 { grid-column-start: 10; }
31+
.col-start-11 { grid-column-start: 11; }
32+
.col-start-12 { grid-column-start: 12; }
33+
34+
.col-end-1 { grid-column-end: 1; }
35+
.col-end-2 { grid-column-end: 2; }
36+
.col-end-3 { grid-column-end: 3; }
37+
.col-end-4 { grid-column-end: 4; }
38+
.col-end-5 { grid-column-end: 5; }
39+
.col-end-6 { grid-column-end: 6; }
40+
.col-end-7 { grid-column-end: 7; }
41+
.col-end-8 { grid-column-end: 8; }
42+
.col-end-9 { grid-column-end: 9; }
43+
.col-end-10 { grid-column-end: 10; }
44+
.col-end-11 { grid-column-end: 11; }
45+
.col-end-12 { grid-column-end: 12; }
46+
.col-end-13 { grid-column-end: 13; }

examples/dotcms-laravel/resources/views/app.blade.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)