Skip to content

Commit 62cdb40

Browse files
committed
Get shared props from all twig/html in _shared directory
1 parent 7e718ca commit 62cdb40

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

src/controllers/BaseController.php

+30-11
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private function getInertiaProps($params = [], $view): array
194194
$session->remove('recentElementSave');
195195
}
196196

197-
$sharedProps = $this->getSharedPropsFromTemplate();
197+
$sharedProps = $this->getSharedPropsFromTemplates();
198198
$mergedParams = array_merge($sharedProps, $params);
199199
return $this->resolvePartialProps($mergedParams, $view);
200200
}
@@ -304,26 +304,45 @@ private function handleElementRequest($element, $uri)
304304
return [$matchesTwigTemplate, $specifiedTemplate, $templateVariables];
305305
}
306306

307-
private function getSharedPropsFromTemplate()
307+
private function getSharedPropsFromTemplates()
308308
{
309309
$inertiaConfiguredDirectory = Inertia::getInstance()->settings->inertiaDirectory ?? null;
310-
$inertiaTemplatePath = $inertiaConfiguredDirectory ? $inertiaConfiguredDirectory . '/shared' : 'shared';
310+
$sharedDir = $inertiaConfiguredDirectory ? $inertiaConfiguredDirectory . '/_shared' : '_shared';
311311

312-
$matchesTwigTemplate = Craft::$app->getView()->doesTemplateExist($inertiaTemplatePath);
312+
// Get the path to templates directory
313+
$templatesPath = Craft::$app->getPath()->getSiteTemplatesPath();
314+
$sharedPath = $templatesPath . DIRECTORY_SEPARATOR . $sharedDir;
313315

314-
if (!$matchesTwigTemplate) {
316+
if (!is_dir($sharedPath)) {
315317
return [];
316318
}
317319

318-
$stringResponse = Craft::$app->getView()->renderTemplate($inertiaTemplatePath);
320+
$allSharedProps = [];
319321

320-
// Decode JSON object from $stringResponse
321-
$jsonData = json_decode($stringResponse, true);
322-
if (json_last_error() !== JSON_ERROR_NONE) {
323-
throw new \Exception('Failed to decode JSON response: ' . json_last_error_msg());
322+
// Read all .twig and .html files in the _shared directory
323+
$files = array_merge(
324+
glob($sharedPath . DIRECTORY_SEPARATOR . '*.twig'),
325+
glob($sharedPath . DIRECTORY_SEPARATOR . '*.html')
326+
);
327+
328+
foreach ($files as $file) {
329+
$templatePath = $sharedDir . DIRECTORY_SEPARATOR . basename($file);
330+
331+
if (Craft::$app->getView()->doesTemplateExist($templatePath)) {
332+
$stringResponse = Craft::$app->getView()->renderTemplate($templatePath);
333+
334+
// Decode JSON object from each template
335+
$jsonData = json_decode($stringResponse, true);
336+
if (json_last_error() !== JSON_ERROR_NONE) {
337+
Craft::warning('Failed to decode JSON response from ' . basename($file) . ': ' . json_last_error_msg(), __METHOD__);
338+
continue;
339+
}
340+
341+
$allSharedProps = array_merge($allSharedProps, $jsonData);
342+
}
324343
}
325344

326-
return $jsonData;
345+
return $allSharedProps;
327346
}
328347

329348
}

0 commit comments

Comments
 (0)