tagsalchemy-cms

How can I display elements with the same tag in Alchemy CMS?


How can I display elements with the same tag in Alchemy CMS?

I tagged some elements with the tag "logic". Now, I want to display the latest elements with this tag on another page. Is there a codeline for this? For example something like this:

 <%= render_elements :from_tag => 'logic' %> 

Solution

  • You can't use the render_elements helper here.

    But you can use plain ol' Rails and the render_element helper:

    <% @page.elements.available.tagged_with('logic').each do |element| %>
      <%= render_element element %>
    <% end %>
    

    Alternatively without the render_element helper you could just render the view partial with:

    <% @page.elements.available.tagged_with('logic').each do |element| %>
      <%= render element %>
    <% end %>
    

    But beware, that then you need to use the element's partial name as local object in your element view, because Rails uses the partial name as default local object. So, for instance, an element named article with a view partial named _article_view has a local object named article_view.

    If you want to keep the Alchemy element generator's default local element object, you can still pass it into your view:

    <% @page.elements.available.tagged_with('logic').each do |element| %>
      <%= render element, element: element %>
    <% end %>
    

    But this reads a little bit weird, I prefer to either use the render_element helper, or rename the local object in my element view.