From f65ba75f415b7bc38de7c6ca0e2da5b742f54f3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Cmit=20Tun=C3=A7?= Date: Thu, 6 Nov 2025 16:52:25 -0300 Subject: [PATCH] Enhance TemplateService Placeholder Replacement: Updated the replacePlaceholders method to improve handling of model attributes when values are not found in the provided data. Added logic to retrieve image/file URLs and model attributes dynamically, enhancing flexibility and robustness in content rendering. --- app/Services/TemplateService.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/Services/TemplateService.php b/app/Services/TemplateService.php index 28e7b8d..fb9145c 100644 --- a/app/Services/TemplateService.php +++ b/app/Services/TemplateService.php @@ -524,6 +524,32 @@ class TemplateService } } } + + // 4. If value not found in data and model is provided, try to get from model attributes + if ($value === null && $model && count($parts) === 2) { + [$type, $field] = $parts; + + // First, try to get accessor with _url suffix for image/file fields (e.g., featured_image_url) + if (in_array($type, ['image', 'images', 'file', 'files'])) { + $accessorField = $field . '_url'; + if ($model->offsetExists($accessorField) || isset($model->{$accessorField})) { + try { + $value = $model->{$accessorField}; + } catch (\Exception $e) { + // Accessor doesn't exist, continue to try attribute + } + } + } + + // Try to get from model attribute (e.g., featured_image, title, content) + if ($value === null && ($model->offsetExists($field) || isset($model->{$field}))) { + try { + $value = $model->getAttribute($field); + } catch (\Exception $e) { + // Attribute doesn't exist + } + } + } } // Handle different value types