Template detection of content shortcode

I looked and was unable to find anything that answered this.

Is there a way for a template to know if a content piece uses a specific shortcode, and to act conditionally on that?

My use case is AMP pages using an iframe. AMP requires the amp-iframe script be in the head of the html. I would like a way for a template to know if a named shortcode is used (one that would insert iframes in my case) and then insert the amp-iframe code only for that rendered page.

I only have one or two content pages that use iframes and I would like to have the amp-iframe script only on those pages. No need to load it on every single AMP page if most pages do not need it.

Not aware of this either.

But you could assign a content type for pages with iframes and then do the check in your head template lile so:

{{ if eq .Type "iframe" }
<--- amp-iframe script --->
{{ end }}

That would certainly work; though it is not as elegant. Also, for my site, I use .Type already, I would have to create another value I can access with .Params

@bep would something like this be useful? Maybe having shortcode information as part of the new .Page.Resources

But, drum roll, I do.


This worked for me.

    {{ if .Page.HasShortcode "myIframeShortcode" }}
    <script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script>
    {{ end }}

Thank you.