Skip to content

Commit ad2453a

Browse files
authored
Merge pull request #5 from RareFormLabs/pull
Add `pull` tag functionality
2 parents 6d53dfc + 5b88744 commit ad2453a

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
/bootstrap.php
99
/craft
1010
/composer.lock
11+
/.vscode

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release Notes for Inertia
22

3+
## 1.0.0-beta.1 - 2025-02-22
4+
5+
- Add `pull` tag DX helper for sharing variables across templates.
6+
37
## 1.0.0-alpha.5 - 2025-02-01
48

59
- Fix console error

README.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ You can install this plugin from the Plugin Store or with Composer.
2121
Go to the Plugin Store in your project’s Control Panel and search for “Inertia”. Then press “Install”.
2222

2323
#### With Composer:
24+
2425
Open your terminal and run the following commands:
2526

2627
```sh
@@ -34,7 +35,9 @@ Be sure to follow the installation instructions for the [client-side framework](
3435
> Upon installing, the Inertia adapter will takeover all routing and expect all templates to respond with inertia protocol responses. To prevent this, you may set the `takeoverRouting` [config option](#configuration) to `false`
3536
3637
> [!IMPORTANT]
38+
>
3739
> ## Required Reading
40+
>
3841
> The [Inertia documentation](https://inertiajs.com) is a must-read to understand the protocol, the responsibilities of this adapter, and how to use Inertia on the client-side. The following sections will explain how to use this adapter, but assume you have a basic understanding of Inertia.
3942
4043
## Defining Pages
@@ -70,7 +73,9 @@ Create a `_shared` directory at the root of your `/templates` directory, and use
7073
csrfTokenName: craft.app.config.general.csrfTokenName
7174
}) }}
7275
```
76+
7377
This allows more flexibility for designating responses you may want to cache to reduce unnecessary repetitive queries.
78+
7479
```twig
7580
{# templates/_shared/current-user.twig #}
7681
@@ -88,6 +93,24 @@ This allows more flexibility for designating responses you may want to cache to
8893
{% endif %}
8994
```
9095

96+
## Pull in Variables
97+
98+
Use the `pull` tag to include variables from a specified template and make them available in the current response twig file.
99+
100+
```twig
101+
{# teams/_base.twig #}
102+
{% set teamColor = "#EE4B2B" %}
103+
```
104+
105+
```twig
106+
{# templates/teams/_entry.twig #}
107+
{% pull('teams/_base') %}
108+
109+
{{ inertia('Teams/Entry', { teamColor: teamColor }) }}
110+
```
111+
112+
This is a simple DX alternative to using `extends` and `block` tags to share variables across templates. Note that the `pull` tag is only available in Inertia responses.
113+
91114
## Saving Data
92115

93116
Craft CMS does not use traditional POST, PUT, PATCH, and DELETE requests for saving data, and instead uses the `action` parameter to hit various internal Craft controllers. This means saving data to Craft CMS data is a little different than what is expected in a traditional Inertia application.
@@ -178,4 +201,3 @@ return [
178201
'takeoverRouting' => true,
179202
];
180203
```
181-

src/controllers/BaseController.php

+51-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public function actionIndex(): array|string
5555
}
5656

5757
try {
58-
$stringResponse = Craft::$app->getView()->renderTemplate($template, $templateVariables);
58+
// Process any pulls in the template first
59+
$processedTemplate = $this->processTemplatePulls($template);
60+
61+
// Render the processed template
62+
$stringResponse = Craft::$app->getView()->renderString($processedTemplate, $templateVariables);
5963
} catch (\Twig\Error\RuntimeError $e) {
6064
$sourceContext = $e->getSourceContext();
6165
$templateFile = $sourceContext ? $sourceContext->getName() : 'unknown template';
@@ -360,4 +364,50 @@ private function getSharedPropsFromTemplates()
360364
return $allSharedProps;
361365
}
362366

367+
/**
368+
* Process any template pull tags in the template
369+
*/
370+
private function processTemplatePulls(string $template): string
371+
{
372+
$view = Craft::$app->getView();
373+
374+
// Store original template mode and switch to site mode
375+
$originalMode = $view->getTemplateMode();
376+
$view->setTemplateMode($view::TEMPLATE_MODE_SITE);
377+
378+
try {
379+
// Find the actual template file
380+
$templatePath = $view->resolveTemplate($template);
381+
if (!$templatePath) {
382+
throw new \Exception("Template not found: {$template}");
383+
}
384+
385+
// Read the template contents
386+
$templateContent = file_get_contents($templatePath);
387+
388+
// This pattern will match {% pull('path') %}
389+
$pattern = '/\{%\s*pull\s*\(\s*([^\)]+)\s*\)\s*%\}/';
390+
391+
$processedContent = preg_replace_callback($pattern, function($matches) use ($view) {
392+
$pullPath = trim($matches[1]);
393+
394+
$directPath = trim($pullPath, "'\"");
395+
$referencedPath = $view->resolveTemplate($directPath);
396+
397+
if (!$referencedPath) {
398+
Craft::warning("Template not found: {$pullPath}", __METHOD__);
399+
return ''; // Or handle the error as needed
400+
}
401+
402+
return file_get_contents($referencedPath);
403+
}, $templateContent);
404+
405+
return $processedContent;
406+
407+
} finally {
408+
// Restore original template mode
409+
$view->setTemplateMode($originalMode);
410+
}
411+
}
412+
363413
}

0 commit comments

Comments
 (0)