views:

207

answers:

2

Hi!

Is there a way to obtain the url of a page on Jekyll? By pages I mean non-post textile files, like about.html and download.html on the following hierarchy:

root
 |
 +- _includes
 |
 +- _layouts
 |
 +- _posts
 |
 +- _config.yml
 |
 +- index.textile
 |
 +- about.textile
 |
 `- download.textile

I'd like to do something like this:

<h1><a href="{{ page.url }}">{{ page.title }}</a></h1>

This works with posts because they have their url included on the post's to_liquid method. Pages on the other hand don't seem to include anything like it - they do have the properties I need (or so it seems), but they don't export them on to_liquid; I'd like to redefine it so it does. Is there a way to do this?

I've found jekyll_extensions, but I don't know if it works on my target environment (github pages).

I'd like to avoid having to include a url parameter on all the yaml heads, if possible.

Thanks a lot!

+1  A: 

I think the answer is no. Here's the Jekyll wiki on what's in the global page item:

For Posts, this is the union of the data in the YAML Front Matter and the computed data (such as URL and date). For regular pages, this is just the YAML Front Matter.

Even if you could reconfigure Jekyll's engine to do it for you locally, I'm not sure how you could make that work on Github's own Pages (since they will be using the vanilla Jekyll rather than yours).

Telemachus
damn. I wish it wasn't so... limiting. Thanks a lot, I guess I will just use the YAML (or create posts with fake dates, eg 0000-01-01)
egarcia
+1  A: 

It is now possible to monkeypatch jekyll classes via its new plugins system!

Just add this file (page.rb) on your _plugins directory:

module Jekyll
  class Page
    def to_liquid
      self.data.deep_merge({
        "url" => File.join(@dir, self.url),
        "content" => self.content,
        "dir" => self.dir,
        "name" => self.name,
        "ext" => self.ext,
        "basename" => self.basename
      })
    end
  end
end

I've just modified to_liquid here, but I could have also added more methods, etc.

egarcia