3

I would like to display dynamic content in the middle of a text block. For example, a list of entries pulled in from the backend, in the middle of content.

In Wordpress we can use Shortcodes. Does Craft have something similar?

Can I just add an include statement to include a template? (I cant get this working using normal twig include code).

I could create a new field for someone to add in in between two other text block fields, but its quote over the top for what should be a simple include statement to include a template.

Thanks!

1
  • Not sure if it's what you're looking for, but check out this Shortcodes plugin.
    – Lindsey D
    Commented Dec 21, 2018 at 21:53

2 Answers 2

3

The typical 'Craft' way of doing this would be via a Matrix field instead of a single text block field, so that editors can sandwich the dynamic content (which would be its own block type) in between whatever other block types they select. That way is very powerful, flexible, extensible, and clean...

However, assuming migrating your content to a Matrix setup would be too onerous or undesirable for other reasons, there's nothing to stop you creating your own basic shortcode replacement functionality via Twig's replace filter. For example, in your body field you might insert arbitrary markers like [list-of-entries]... then in the template that outputs the field, you can go to town with defining what each shortcode represents and then replace them like this:

{% set listOfEntries %}
    <ul>
        {% for item in craft.entries.section('something').all %}
            <li>{{ item.title }}</li>
        {% endfor %}
    </ul>
{% endset %}

{% set parsedBody = entry.body|replace({
    "[dynamic-address-injection]" : craft.entries.section('something').one().addressField,
    "[video]" : entry.videos.one().embedCode,
    "[list-of-entries]" : listOfEntries
}) %}

{{ parsedBody }}
2
  • that sounds good will give it a try thanks. Reason for not using Matrix -> When we build websites one of our goals is to make administering it as easy as possible. Once you add a matrix field to manage what should be one block of content, the admin interface starts to get too complex for simple web administrators. You get an entire textblock dedicated to a small sentence. It really starts to become too complex/cluttered for what really should be a simple task of inserting dynamic content in the middle of some paragraphs. Commented Dec 21, 2018 at 9:38
  • Wordpress has shortcodes which are great for this purpose and easy to setup. It would allow us to program in PHP code without having to develop a plugin which has now become complex in Craft 3. Plugin would also be over top just to list some entry titles and links for example in the middle of content. So a simplistic way to add dynamic content, and for the customer to manage the content, is the best. So thanks a lot, if this works it should ensure the simplicity of it. I will like to try to include a template in that code though to break out the coding into an includes file. Commented Dec 21, 2018 at 9:39
2

There's also the Shortcodes plugin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.