@@ -194,7 +194,7 @@ private function getInertiaProps($params = [], $view): array
194
194
$ session ->remove ('recentElementSave ' );
195
195
}
196
196
197
- $ sharedProps = $ this ->getSharedPropsFromTemplate ();
197
+ $ sharedProps = $ this ->getSharedPropsFromTemplates ();
198
198
$ mergedParams = array_merge ($ sharedProps , $ params );
199
199
return $ this ->resolvePartialProps ($ mergedParams , $ view );
200
200
}
@@ -304,26 +304,45 @@ private function handleElementRequest($element, $uri)
304
304
return [$ matchesTwigTemplate , $ specifiedTemplate , $ templateVariables ];
305
305
}
306
306
307
- private function getSharedPropsFromTemplate ()
307
+ private function getSharedPropsFromTemplates ()
308
308
{
309
309
$ inertiaConfiguredDirectory = Inertia::getInstance ()->settings ->inertiaDirectory ?? null ;
310
- $ inertiaTemplatePath = $ inertiaConfiguredDirectory ? $ inertiaConfiguredDirectory . '/shared ' : 'shared ' ;
310
+ $ sharedDir = $ inertiaConfiguredDirectory ? $ inertiaConfiguredDirectory . '/_shared ' : '_shared ' ;
311
311
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 ;
313
315
314
- if (!$ matchesTwigTemplate ) {
316
+ if (!is_dir ( $ sharedPath ) ) {
315
317
return [];
316
318
}
317
319
318
- $ stringResponse = Craft:: $ app -> getView ()-> renderTemplate ( $ inertiaTemplatePath ) ;
320
+ $ allSharedProps = [] ;
319
321
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
+ }
324
343
}
325
344
326
- return $ jsonData ;
345
+ return $ allSharedProps ;
327
346
}
328
347
329
348
}
0 commit comments