Calling a templated Element through Twig

I’ve saved a block of code as an Element template in my Library. Now I’d like to pull that onto a specific page.

I’ve had success using the Twig Templates functionality (thanks to your help), but the element template above is clearly a different template. How do I pull an element I’ve templated?

{% if url.path == 'my-account' %}
{{ *print the templated element named 'latest-post'* }}
    {% else %}
    {% endif %}

Or barring that, is there a way to call a block of code (vs including the literal code in the Twig Template?)?

Thanks!

Hello Matt,

Thank you for the inquiry.

Have you tried using the include function?

Example:

{% include 'cs-template:id-0' %}

Make sure to replace “id-0” with the actual ID of your template. For more info, please check this documentation: https://theme.co/docs/twig#including-templates

Let us know if you need more help.

Best regards,
Ismael

Sorry, it’s confusing there’s more than one kind of ‘template’ in Cornerstone. There’s the Twig Template workflow, where you pull a template you’ve coded into the Globals pane. But as I mentioned above, this is an Elements Template (Templates > Manage Library):

What I’d like to do is to use Twig to show this element ONLY on the ‘my Account/dashboard’ page. I’ve tried to do this with include both directly into a text block, AND/OR encoded into a Twig Template:

`{% if url.path == 'my-account' %}`
`{% include 'cs-template:Latest-post-element' %}`
    `{% else %}`
   ` {% endif %}`

but neither works:


It simply pulls in the name of the element, not the visuals.

If pulling and Element template isn’t possible with Twig, do you have any suggestions on how I can do this outside of hard coding this element (which pulls in the latest blog post and featured image) into a Twig template?

thx!

Thank you for the clarification.

You may need to convert the element to a component, export it, then use the component shortcode in the Twig template. Please check this documentation: https://theme.co/docs/component-builder-overview#component-shortcode

This video shows how to export a component: https://www.youtube.com/watch?v=Ke8AdlYaY7Y&t=264s

Example of a twig template with component shortcode:

<h1>Test</h1> [cs_component id="M9SHd7vqePH5BJcUCx"]

Let us know if you need more help.

Wow, cool, that worked! Thanks much!

Hey Matt,

You’re most welcome!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.