3

(Ruby noob) I'm using nanoc to generate a site. Here is my sample page with metadata:

--- 
title: abc
parameters:
    abc: def
    ghi: ijk
---

test

I know that I can have parameters one level up but I want to access them in layout file:

<%= @item[:parameters][:abc] %>

but I got following error:

undefined method `[]' for nil:NilClass

Parameters are passed to layout because when I do:

<%= YAML::dump(@item[:parameters]) %>

I can see them. As a Ruby noob I think there's a simple solution to my problem. Also, if you could post a snippet iterating through :parameters hash I would be grateful.

keepkimi
  • 373
  • 3
  • 12
  • It doesn't make sense that `YAML::dump` returns data while `@item[:parameters][something]` gives undefined method `[]` for `NilClass`. It's not possible. You can't be indexing `nil` with `[]` if `YAML::dump` shows there is something there. So are you sure that error is because of that particular code? Something else must be going on. – Casper Jan 15 '13 at 18:10
  • I agree with @Casper. I modified the tutorial adding your parameters and the layout change, it works just fine. – Diego Basch Jan 15 '13 at 18:14
  • What does `<%= @item.class %>` and `<%= @item[:parameters].class %>` say? – Casper Jan 15 '13 at 18:29
  • When I do: <%= YAML::dump(@item[:parameters]) %> I got: -- :abc: def :ghi: ijk but <%= @item[:parameters][:abc] %> gives exception above. Maybe my Ruby is too old? ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux] – keepkimi Jan 15 '13 at 18:32
  • <%= @item[:parameters].class %> - Hash – keepkimi Jan 15 '13 at 18:33
  • <%= @item.class %> - Nanoc::Item – keepkimi Jan 15 '13 at 18:33
  • Ok..I think I know what's wrong. Do you have more than one page on your site? – Casper Jan 15 '13 at 18:36
  • @Casper, yes, three in a folder where a problem occurs. – keepkimi Jan 15 '13 at 18:41

1 Answers1

3

You probably have more than one page on your site. Therefore parameters does not exist in the layout for every page nanoc processes. For some pages the element will be nil and hence the error you get.

Perhaps you need to rethink how you want to use those parameters, or then you need a conditional in your layout to look for parameters and only use them when they are present:

<% if @item[:parameters] %>
 <%= @item[:parameters][:abc] %>
<% end %>
Casper
  • 33,403
  • 4
  • 84
  • 79
  • Unfortunatelly that's not the case. I got this error on page that has parameters in metadata. – keepkimi Jan 15 '13 at 18:45
  • @keepkimi And if you change the code to the above code, what happens? – Casper Jan 15 '13 at 18:46
  • 1
    @keepkimi I don't know how nanoc processes changes to pages, but the code without the `if` statement will fail the moment nanoc processes a page or layout file that did NOT load your page first that contains the `parameters` data. So you see even if nanoc says it only processes your one page, perhaps it has to do some comparisions internally and loads some pages that does not have `parameters` -> BOOM. – Casper Jan 15 '13 at 18:59