0
votes

The post_url method is really great, but what if I want to also get the title of that post? Would I need to make a separate call like {{ site.posts | where:"permalink","/post/date/whatever" }}?

Seems really clunky - is there a better way?

1

1 Answers

0
votes

You could always add a new Liquid tag. Here's a sketch based on post_url.rb:

class PostTitle < Liquid::Tag
  def initialize(tag_name, post, tokens)
    super
    @orig_post = post.strip
    begin
      @post = Jekyll::Tags::PostComparer.new(@orig_post)
    rescue => e
      raise Jekyll::Errors::PostURLError, <<-eos
Could not parse name of post "#{@orig_post}" in tag 'post_title'.
Make sure the post exists and the name is correct.
#{e.class}: #{e.message}
eos
    end
  end

  def render(context)
    site = context.registers[:site]

    site.posts.docs.each do |p|
      return p.data.title if @post == p
    end

    raise Jekyll::Errors::PostURLError, <<-eos
Could not find post "#{@orig_post}" in tag 'post_title'.
Make sure the post exists and the name is correct.
eos
  end
end

Liquid::Template.register_tag('post_title', PostTitle)