Skip to content

Commit a8448c4

Browse files
committed
bugfix: template render error
1 parent 1800b82 commit a8448c4

File tree

2 files changed

+55
-23
lines changed

2 files changed

+55
-23
lines changed

src/Helpers/TemplateHelper.php

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,28 +141,36 @@ public static function ensureViewFileNameForTemplate(string $name)
141141
*/
142142
public static function splitBladeExpressionForProperty(string $bladeExpression)
143143
{
144-
$explodedValues = array_map('trim', explode(',', $bladeExpression));
145-
146-
if (count($explodedValues) > 3) {
147-
[$group, $property, $propertyVarName, $dtoVar] = $explodedValues;
148-
} elseif (count($explodedValues) > 2) {
149-
[$group, $property, $propertyVarName] = $explodedValues;
150-
} else {
151-
[$group, $property] = $explodedValues;
144+
try {
145+
$explodedValues = array_map('trim', explode(',', $bladeExpression));
146+
147+
if (count($explodedValues) > 3) {
148+
[$group, $property, $propertyVarName, $dtoVar] = $explodedValues;
149+
} elseif (count($explodedValues) > 2) {
150+
[$group, $property, $propertyVarName] = $explodedValues;
151+
} elseif (count($explodedValues) > 1) {
152+
[$group, $property] = $explodedValues;
153+
} else {
154+
return [];
155+
}
156+
157+
$group = static::normalizeVarNameFromBladeExpression($group);
158+
$property = static::normalizeVarNameFromBladeExpression($property);
159+
160+
$propertyVarName ??= static::generatePropertyVarName($group, $property);
161+
// Ensure the variable name is not empty or null
162+
if (empty($propertyVarName) || is_null($propertyVarName) || $propertyVarName === 'null') {
163+
$propertyVarName = static::generatePropertyVarName($group, $property);
164+
}
165+
166+
$dtoVar ??= '$content';
167+
168+
return [$group, $property, $dtoVar, static::normalizeVarNameFromBladeExpression($propertyVarName)];
169+
} catch (\Throwable $th) {
170+
//
152171
}
153172

154-
$group = static::normalizeVarNameFromBladeExpression($group);
155-
$property = static::normalizeVarNameFromBladeExpression($property);
156-
157-
$propertyVarName ??= static::generatePropertyVarName($group, $property);
158-
// Ensure the variable name is not empty or null
159-
if (empty($propertyVarName) || is_null($propertyVarName) || $propertyVarName === 'null') {
160-
$propertyVarName = static::generatePropertyVarName($group, $property);
161-
}
162-
163-
$dtoVar ??= '$content';
164-
165-
return [$group, $property, $dtoVar, static::normalizeVarNameFromBladeExpression($propertyVarName)];
173+
return [];
166174
}
167175

168176
/**

src/InspireCmsServiceProvider.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,23 +581,47 @@ protected function registerComponentAndDirectives(): void
581581
Blade::component('cms-template', Template::class);
582582

583583
Blade::directive('property', function ($expression) {
584-
[$group, $property, $dtoVar, $propertyVarName] = TemplateHelper::splitBladeExpressionForProperty($expression);
584+
585+
$list = TemplateHelper::splitBladeExpressionForProperty($expression);
586+
587+
if (count($list) === 4) {
588+
[$group, $property, $dtoVar, $propertyVarName] = $list;
589+
} else {
590+
// Return nothing
591+
return '';
592+
}
585593

586594
return "<?php
587595
\${$propertyVarName} = {$dtoVar}->getPropertyGroup('{$group}')?->getPropertyData('{$property}')?->getValue();
588596
echo is_array(\${$propertyVarName}) ? \\Illuminate\\Support\\Arr::first(\${$propertyVarName}) : \${$propertyVarName};
589597
?>";
590598
});
591599
Blade::directive('propertyArray', function ($expression) {
592-
[$group, $property, $dtoVar, $propertyVarName] = TemplateHelper::splitBladeExpressionForProperty($expression);
600+
601+
$list = TemplateHelper::splitBladeExpressionForProperty($expression);
602+
603+
if (count($list) === 4) {
604+
[$group, $property, $dtoVar, $propertyVarName] = $list;
605+
} else {
606+
// Return nothing
607+
return '';
608+
}
593609

594610
return "<?php
595611
\${$propertyVarName} = {$dtoVar}->getPropertyGroup('{$group}')?->getPropertyData('{$property}')?->getValue();
596612
?>";
597613
});
598614

599615
Blade::directive('propertyNotEmpty', function ($expression) {
600-
[$group, $property, $dtoVar, $propertyVarName] = TemplateHelper::splitBladeExpressionForProperty($expression);
616+
617+
$list = TemplateHelper::splitBladeExpressionForProperty($expression);
618+
619+
if (count($list) === 4) {
620+
[$group, $property, $dtoVar, $propertyVarName] = $list;
621+
} else {
622+
// Return nothing
623+
return '';
624+
}
601625

602626
return "<?php
603627
\${$propertyVarName} = {$dtoVar}->getPropertyGroup('{$group}')?->getPropertyData('{$property}')?->getValue();

0 commit comments

Comments
 (0)