Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:39:15+00:00 2026-06-04T12:39:15+00:00

With ERB you can fragment cache individual records in a list view like so:

  • 0

With ERB you can fragment cache individual records in a list view like so:

<% @items.each do |item| %>
  <% cache item do %>
    <%= item.name %>
  <% end %>
<% end %>

Thus, the second time the list is viewed, each rendered item will be loaded from cache. Is it possible to use this same approach when using Liquid templates? The template might look something like:

{% for item in items %}
  {{ item.name }}
{% endfor %}

and rendered with:

template = Liquid::Template.parse(template)
template.render('items' => @items)

When it loops over the items, I’d like to be able to fragment cache each one. Any pointers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:39:17+00:00Added an answer on June 4, 2026 at 12:39 pm

    You can define custom tags in liquid, for example if you put this in an initializer

    class Cacher < Liquid::Block
      def initialize(tag_name, markup, tokens)
         super
        @key= markup.to_s
      end
    
      def render(context)
        Rails.cache.fetch(@key) do
          super
        end
      end
    end
    
    Liquid::Template.register_tag('cache', Cacher)
    

    Then you can do

    {% cache "some_key" %}
      ...
    {% endcache %}
    

    Be very careful with how you construct the cache key. You of course want to avoid clashes, but you also probably don’t want people to be able to read arbitrary keys from your memcache store (which this code does). How match this matters depends on who has access to this in your app. One strategy would be to namespace cache keys.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an .erb file where I'd like to return the following text: Name
I have the following .erb view in a Sinatra app: <% sessions.each do |session|
how can i apply vim settings to erb file?? like /* tabstop=4 shiftwidth=4 foldmethod=indent
How can I render a string like erb files render. for example I want
In my html.erb views I can normally use code like: if current_user.role?(:label) do something
Can I have conditional javascript in my show.erb.html files? Something like this: <%- if
inside show.js.erb $('#viewport').html(<%= escape_javascript(render('show')) %>); I am trying to render the show view into
I am using Puppet for automating configuration management across hosts. It can use erb
I can do request.path_parameters['controller'] and request.path_parameters['action'], but is there anything like request.path_parameters['template'] so I
in my view I usually do something like this <%= render :partial => shared/p

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.