diff --git a/docs/_docs/templates.md b/docs/_docs/templates.md index 4b93b75a..e91de10d 100644 --- a/docs/_docs/templates.md +++ b/docs/_docs/templates.md @@ -588,6 +588,23 @@ One major benefit of using the `link` or `post_url` tag is link validation. If t Note you cannot add filters to `link` tags. For example, you cannot append a string using Liquid filters, such as `{% raw %}{% link mypage.html | append: "#section1" %} {% endraw %}`. To link to sections on a page, you will need to use regular HTML or Markdown linking techniques. +The name of the file you want to link can be specified as a variable instead of an actual file name. For example, suppose you defined a variable in your page's front matter like this: + +```yaml +--- +title: My page +my_variable: footer_company_a.html +--- +``` + +You could then reference that variable in your link: + +```liquid +{% raw %}{% link {{ page.my_variable }} %}{% endraw %} +``` + +In this example, the link would add link to the file `footer_company_a.html`. + ### Linking to posts If you want to include a link to a post on your site, the `post_url` tag will generate the correct permalink URL for the post you specify. diff --git a/lib/jekyll/tags/link.rb b/lib/jekyll/tags/link.rb index b3f5a2ad..9dc035f7 100644 --- a/lib/jekyll/tags/link.rb +++ b/lib/jekyll/tags/link.rb @@ -18,14 +18,17 @@ module Jekyll def render(context) site = context.registers[:site] + liquid = site.liquid_renderer.file("(jekyll:link)") + relative_path = liquid.parse(@relative_path).render(context) + site.each_site_file do |item| - return item.url if item.relative_path == @relative_path + return item.url if item.relative_path == relative_path # This takes care of the case for static files that have a leading / - return item.url if item.relative_path == "/#{@relative_path}" + return item.url if item.relative_path == "/#{relative_path}" end raise ArgumentError, <<-MSG -Could not find document '#{@relative_path}' in tag '#{self.class.tag_name}'. +Could not find document '#{relative_path}' in tag '#{self.class.tag_name}'. Make sure the document exists and the path is correct. MSG diff --git a/test/test_tags.rb b/test/test_tags.rb index ae75c024..e7a0f726 100644 --- a/test/test_tags.rb +++ b/test/test_tags.rb @@ -789,6 +789,45 @@ CONTENT end end + context "simple page with dynamic linking to a page" do + setup do + content = < source_dir, + "destination" => dest_dir, + "read_all" => true, + }) + end + + should "not cause an error" do + refute_match(%r!markdown\-html\-error!, @result) + end + + should "have the URL to the 'contacts' item" do + assert_match(%r!/contacts\.html!, @result) + end + + should "have the URL to the 'info' item" do + assert_match(%r!/info\.html!, @result) + end + + should "have the URL to the 'screen.css' item" do + assert_match(%r!/css/screen\.css!, @result) + end + end + context "simple page with linking" do setup do content = < source_dir, + "destination" => dest_dir, + "collections" => { "methods" => { "output" => true } }, + "read_collections" => true, + }) + end + + should "not cause an error" do + refute_match(%r!markdown\-html\-error!, @result) + end + + should "have the URL to the 'yaml_with_dots' item" do + assert_match(%r!/methods/yaml_with_dots\.html!, @result) + end + end + context "simple page with nested linking" do setup do content = < source_dir, + "destination" => dest_dir, + "collections" => { "methods" => { "output" => true } }, + "read_collections" => true, + }) + end + end + end + context "include tag with parameters" do context "with symlink'd include" do should "not allow symlink includes" do