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

  • Home
  • SEARCH
  • 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 1051775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:59:19+00:00 2026-05-16T16:59:19+00:00

I’ve been playing around with the Liquid templating engine this weekend, and I wonder

  • 0

I’ve been playing around with the Liquid templating engine this weekend, and I wonder if the following is possible.

Say I have a latest_posts method in a Blog model, which I can pass an integer to to get the latest N posts. Is it possible to use that method in a liquid template?

For example:

class Blog

  has_many :posts

  def latest_posts(n)
    posts.latest(n) # using a named scope
  end

  def to_liquid(*args)
    {
      'all_posts' => posts.all,  # allows me to use {% for posts in blog.all_posts %}
      'last_post' => post.last,  # allows me to use {% assign recent = blog.last_post %}
      'latest_posts' => posts.latest_posts(args[0])  # how do I pass variables to this?
    }
  end

end

In the simplified example above, in my liquid templates I can use blog.all_posts and blog.last_post, but have no idea how I would do anything like blog.latest_posts: 10.

Can anyone point my in the right direction?

One idea I thought of was to create a Liquid filter and pass both the Blog object and an integer to that. Something like:

{% for post in blog | latest_posts(10) %}
  • but haven’t tried that yet as feel like I’m stabbing around in the dark a bit. Would appreciate some help from more experienced Liquid users.
  • 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-05-16T16:59:20+00:00Added an answer on May 16, 2026 at 4:59 pm

    Answering my own question here, I found a solution documented in the Liquid groups pages.

    Essentially, I needed to create a drop for the latest posts – a LatestPostsDrop – and kind of hack passing a variable to it using the before_method method. Here is the complete solution:

    class Blog
    
      has_many :posts
    
      def latest_posts
        LatestPostsDrop.new(posts)
      end
    
      def to_liquid
        {
          'all_posts' => posts.all,
          'last_post' => post.last,
          'latest_posts' => latest_posts
        }
      end
    
    end
    
    class LatestPostsDrop < Liquid::Drop
    
      def initialize(posts)
        @posts = posts
      end
    
      def before_method(num)
        @posts.latest(num)    # Post.latest is a named scope
      end
    
    end
    

    Doing the above, allows you to iterate through any number of latest posts using something like:

    {% for post in blog.latest_posts.10 %}  # the last attribute can be any integer
      <p>{{ post.title }}</p>
    {% endfor %}
    

    It seems a bit hacky, but it works 🙂

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

Sidebar

Related Questions

No related questions found

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.