rbuchberger / jekyll_picture_tag

Easy responsive images for Jekyll.
https://rbuchberger.github.io/jekyll_picture_tag/
BSD 3-Clause "New" or "Revised" License
623 stars 106 forks source link

No context to render liquid when a picture tag is used in a _layout file #11

Closed bryanbergman closed 10 years ago

bryanbergman commented 11 years ago

When I try to run Jekyll, I get Liquid Exception: undefined method[]' for nil:NilClass in post.html`

A traceback shows /Users/Bryan/Desktop/sl/_plugins/picture_tag.rb:42:inrender': undefined method []' for nil:NilClass (NoMethodError)

post.html is in my _layouts, I wonder if that is affecting it.

``html {% include header.html %}

    {% picture articleimg {{ page.title }} alt="yep" %}

    <div class="wrapper">

        <article class="post">

            <header>
                <h1><a href="{{ page.url }}">{{ page.title }}</a></h1>
                <p><time datetime="{{ page.date | date: "%Y-%m-%d" }}">{{ page.date | date: "%B %d, %Y" }}</time></p>
                <p>By <a href="http://twitter.com/bryanbergman/" target="_blank">Bryan Bergman</a></p>
                <p>Reading time: <strong>{{ page.reading-time }}</strong></p>
            </header>

            {{ content }}

        </article>

        <div class="all"><a href="{{ site.url }}/archives">See all posts &rarr;</a></div>

    </div>

{% include footer.html %}

robwierzbowski commented 11 years ago

Can you post your picture settings from _config.yml?

Line 42 deals with getting the preset settings from config.yml. Last time I saw this error someone was missing the required default preset, so that's a possibility.

bryanbergman commented 11 years ago
picture:
  source: post_img
  output: generated
  markup: "picturefill"
  presets:
    default:
      attr:
        class: "post-img"
      source_default:
        width: "360"
        height: "771"
      source_720:
        width: "720"
        media: "(min-device-pixel-ratio: 1.5)"
      source_430:
        width: "430"
        media: "(min-width: 360px)"
      source_860:
        width: "860"
        media: "(min-width: 360px) and (min-device-pixel-ratio: 1.5)"
      source_500:
        width: "500"
        media: "(min-width: 430px)"
      source_1000:
        width: "1000"
        media: "(min-width: 430px) and (min-device-pixel-ratio: 1.5)"
      source_640:
        width: "640"
        media: "(min-width: 500px)"
      source_1280:
        width: "1280"
        media: "(min-width: 500px) and (min-device-pixel-ratio: 1.5)"
    articleimg:
      attr:
        class: "post-img"
      source_1200:
        width: "1200"
        media: "(min-width: 940px)"
      source_940:
        width: "940"
        media: "(min-width: 780px)"
      source_780:
        width: "780"
        media: "(min-width: 600px)"
      source_600:
        width: "600"
        media: "(min-width: 470px)"
      source_470:
        width: "470"
        media: "(min-width: 360px)"
      source_default:
        width: "360"
robwierzbowski commented 11 years ago

It looks like you're right. There's an issue with the context variable used to render liquid inside the tag when the tag is used in a layout — it's either not getting picked up properly or doesn't exist. I'll take a look into this, see what I can find.

patrickcate commented 10 years ago

I'm getting this same error. Have a solution been found?

robwierzbowski commented 10 years ago

This would be something to ask about in the Jekyll queue. Last time I checked, context just wasn't available in layouts the same way as in posts.

patrickcate commented 10 years ago

So if I put the {% picture %} tag in the post rather than the layout, it should work?

robwierzbowski commented 10 years ago

It sure should.

Rob Wierzbowski @robwierzbowski http://twitter.com/#!/robwierzbowski http://github.com/robwierzbowski http://robwierzbowski.com

On Fri, Feb 28, 2014 at 4:22 PM, PatrickC8t notifications@github.comwrote:

So if I put the {% picture %} tag in the post rather than the layout, it should work?

Reply to this email directly or view it on GitHubhttps://github.com/robwierzbowski/jekyll-picture-tag/issues/11#issuecomment-36395724 .

patrickcate commented 10 years ago

I think I've gotten around the issue by using https://github.com/rustygeldmacher/jekyll-contentblocks

maziarzamani commented 10 years ago

I have the same issue, cant really use any of the presets, it keeps falling back to the default.

robwierzbowski commented 10 years ago

For now, contentblocks is the preferred fix.

anroots commented 9 years ago

After installing the gem, I got undefined method '[]' for nil:NilClass error when compiling. Defining the default preset in _config.yml solved this.

Adding a more informative exception message ("Default preset configuration not defined") would be beneficial to new users.

ChrisChinchilla commented 9 years ago

I can confirm that @anroots suggestion worked for me to. Though I have another issue know, not sure if it's related…