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
How to pass custom class into Dynamic Include #620
Comments
If you inline the JS code then you’ll be able to use a Twig variable. <script>
document.addEventListener('beforeBlitzInject', function (event) {
if (event.detail.element.hasClass('{{ className }}') {
// Cancel event
event.preventDefault();
}
});
</script> Or alternatively create and call a function instead. <script>
cancelBlitzInject('{{ className }}');
</script> |
Hey @bencroker - I'm a colleague of @erikberger - apologies if we've misunderstood, but just want to explain our use case a little more to make sure this would work. We've got some We were thinking maybe the 3rd parameter to {% set options = {
placeholder: '<img src="/images/placeholder.svg">',
custom: 'logged-in-only'
} %}
{{ craft.blitz.includeDynamic('path/to/template', { param1: 'value1' }, options) }} Then in the javascript handler we could do something like document.addEventListener('beforeBlitzInject', function (event) {
if(userIsLoggedIn ===false && event.detail.custom === 'logged-in-only'){
event.preventDefault();
}
}); |
@bencroker @spib - it looks like if we use
that value gets passed on to the injected tag as the Does that look right? |
Yes, that’s exactly what I’d recommend doing. The element output will look something like this: <span id="blitz-inject-1"
class="blitz-inject"
data-blitz-id="1"
data-blitz-uri="/_dynamic" data-blitz-params="action=blitz%2Finclude%2Fdynamic&index=558018959"
data-blitz-property="logged-in-only"
></span> So you can check the value of |
@bencroker @erikberger No, that doesn't work (at least the testing I did previously) because blitz assumes the I think it does need a separate field just for this purpose? |
Oh yes, you’re right @spib, that is used internally by Blitz. Wrapping the include in an element that you can target might be the simplest workaround, without requiring any changes to the Blitz inject script. <div class="logged-in-only">
{{ craft.blitz.includeDynamic(template) }}
</div> <script>
document.addEventListener('beforeBlitzInject', function (event) {
if (event.detail.element.parentElement.hasClass('logged-in-only') {
event.preventDefault();
}
});
</script> Let me know if that works for you. |
Hi @bencroker - that works for us using It does leave us with extraneous empty Would you consider putting in a future release something like a
which results in the injected tag looking like
Or another way we can identify and cancel specific dynamic includes? |
Support Request
In this closed issue @bencroker , you give an example of canceling a dynamic include with a class of "shoot-me-down" #151 (comment) .
I need to be able to conditionally cancel some template injections given various circumstances. How can I pass a class or attribute like "shoot-me-down" into the injected markup to achieve this? All of my injected includes just have the stock
blitz-inject blitz-inject--injected
classes.Plugin Version
4.9.3
The text was updated successfully, but these errors were encountered: