Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use includeDynamic from a plugin #753

Open
samuelreichor opened this issue Jan 16, 2025 · 2 comments
Open

Use includeDynamic from a plugin #753

samuelreichor opened this issue Jan 16, 2025 · 2 comments
Labels
question Further information is requested

Comments

@samuelreichor
Copy link

Support Request

Hi Ben,

I hope you're doing well.

I’m currently working on a simple plugin to add an "Edit Page Entry" link to the frontend, and I was wondering if there’s a way to inject a dynamically rendered template into the frontend via a plugin, so that even cached entries would receive this link. I understand your time is very valuable, and I don't want to take too much of it, but I’d really appreciate any guidance you could provide.

Here’s what I’ve tried so far:

$blitzVariable = Craft::createObject(BlitzVariable::class);
$html = $blitzVariable->includeDynamic('craft-quick-edit/_settings.twig');
Craft::$app->getView()->registerHtml($html, View::POS_END);

This is the content of my _settings.twig:

<div>
  <p>Hi</p>
</div>

But when I test it, this is the output I get:

<span id="blitz-inject-2" class="blitz-inject" data-blitz-id="2" data-blitz-uri="/_dynamic" data-blitz-params="action=blitz/include/dynamic&amp;index=1519155553" data-blitz-property=""></span>

Any insights or suggestions you might have would be greatly appreciated!

Thanks so much!

Plugin Version

5.9.9

@samuelreichor samuelreichor added the question Further information is requested label Jan 16, 2025
@samuelreichor
Copy link
Author

samuelreichor commented Jan 16, 2025

After writing this, I tried to put the code into the Craft::$app->onInit() function and now I get the correct html back. Yeyy, but my variables are not working properly :(

My code is now:

            $cpEditUrl = $entry->getCpEditUrl();
            $canEdit = Craft::$app->getElements()->canSave($entry); // does update here
            $blitzVariable = Craft::createObject(BlitzVariable::class);
            $html = $blitzVariable->includeDynamic('craft-quick-edit/_settings.twig', [
                'cpEditUrl' => $cpEditUrl, // works as expected
                'canRender' => $canEdit, // does not update in the _settings component
            ]);
            Craft::$app->getView()->registerHtml($html, View::POS_END);

in _settings.twig i tried something like that:

{{ dump(canRender, cpEditUrl) }}
{% if canRender %}
  <div>
    <p>Hi</p>
  </div>
{% endif %}

@bencroker
Copy link
Collaborator

Can you provide a simple example so I can test locally? Be sure to clear the cache any time you make changes during testing, otherwise you’ll be viewing stale versions of the page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants