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.

This commit is contained in:
Ümit Tunç
2025-11-06 16:52:25 -03:00
parent 43ff270cbd
commit f65ba75f41
+26
View File
@@ -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