Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

YFM not working: A question about troubleshooting in general #424

Closed
StevenBlack opened this Issue · 16 comments

5 participants

@StevenBlack

Hi, forgive me if this elementary. I sense I'm close to a general breakthrough but there is something I don't "get".

I have pages with YFM defined in the conventional way. The pages assemble fine because Layouts and Includes are working great. Everything is perfect except for one nagging thing.

All my pages have YFM sections and, for the life of me, I can't get these YAML locals to render.

test.html

---
title: Here is the money
---
<p>Show me the money: {{page.title}}</p>
<p>Show me the money: {{title}}</p>

These don't render anything. Just doesn't work. I get blank strings in lieu of tokens.

Are page files required to have a particular file extension for this to work?

On a more meta level, what's a good loop or checklist for debugging Assemble?

There are a lot of moving parts in Assemble and I sense we need to better surface how the underlying processes interact. My best guess is Handlebars isn't kicking-in. I know the YAML processing is working -- or is being parsed out -- because the final product merged into Layouts with Includes contains no YAML section.

Coming from Jekyll, I can tell you the YFM is fine, no tabs therein or any of the usual YAML gotchas are present here.

In some cases files are 3-folders below below my gruntfile's folder. Would this be a factor?

@hinok

Are page files required to have a particular file extension for this to work?

You should have defined in Gruntfile.js, where assemble looks for pages, layouts and partials like here:

assemble: {
  options: {
    assets: 'assets',
    plugins: ['permalinks'],
    partials: ['includes/**/*.hbs'],
    layout: ['layouts/default.hbs'],
    data: ['data/*.{json,yml}']
  },
  pages: {
    src: ['docs/*.hbs'],
    dest: './'
  }
}

Of course I mean this part

  pages: {
    src: ['docs/*.hbs'],
    dest: './'
  }

Check if paths are correct and your files extensions. I see you wrote test.html, maybe you defined in Gruntfile.js different file format?

On a more meta level, what's a good loop or checklist for debugging Assemble?

I am not sure, if it's the best way to do these things, but I add handlebars helper, which can print any JS object.
So in any layout, page or partial I can fast check context's data, just by use

{{ stringify this }}

it prints object's content.

To add that helper, you've to define property in assemble's options in Gruntfile.js

options: {
  // another options...
  helpers: 'src/helpers/helper-*.js'
}

Paste code below to your helper's folder.

module.exports.register = function(Handlebars, options) {
    Handlebars.registerHelper('stringify', function(context) {
        return JSON.stringify(context);
    });
};

Hope these will work for you.

@StevenBlack

Thanks @hinok. Assemble itself appears to be working fine. The files all merge and the resultants all copy perfectly. File ops are fine.

I'll try the Handlebars helper but I fear this may not help. We already know Handlebars isn't working. The page's YFM isn't being processed properly. Shouldn't it? A page's YFM should be in-scope, right? No voodoo here, unless I completely misunderstand, which is possible.

@doowb
Owner

Are you calling {{page.title}} from inside a loop or another partial that had a different context passed in? I'm on the road and won't be able to take a closer look until later.

@StevenBlack

@doowb Not calling from inside loop or partial or anything else.

This is as vanilla as it gets. Stand alone page with YFM section and a couple of lines referencing that YAML.

@doowb
Owner

I created this repo with yo assemble and took out the other pages and added a test.hbs page with the code you posted above. It seems to be working for me. Do you have a repo you can show us? There might be some simple configuration problem.

@StevenBlack

Thank you very much @doowb for taking the time to put that together. This has put me on the right track.

If I drastically strip-down my assemble hash in gruntfile.js -- as in the demo repo -- then my test.hbs works.

But as soon as I define a layout into which this test.hbs is nested, no test.hbs YFM variable substitution occurs.

To be clear: test.hbs gets nested into the layout, but its YFM may as well be blank strings; they are lost in the process.

Does this sound familiar?

My gut feeling is, there's a sequencing nuance here. How do I ensure test.hbs gets the YFM treatment before it gets merged into the layout?

@akileez

@StevenBlack
can you create a test repo. It is hard to figure out the issue from your description. I am sure it is something simple.

@jonschlinkert

My gut feeling is, there's a sequencing nuance here

@akileez turns out this is a bug. I pushed up changes to the test repo to focus on the reduced test case. I also added templates that demonstrate the problem. https://github.com/doowb/assemble-page-yfm-demo

The good news is that we're very close to releasing v0.5.0 (this month) and we'll have this fixed in that version. The bad news is that we probably won't fix it in a 0.4.x branch.

@jonschlinkert

I should say I did a PR to @doowb's repo, he'll need to merge that in first, or in the meantime here is my fork https://github.com/jonschlinkert/assemble-page-yfm-demo.

And here is the template that shows how different contexts should be processed https://raw.github.com/jonschlinkert/assemble-page-yfm-demo/master/templates/multiple.md

Again, it shows how context should be processed, but some aren't. Most common scenarios work (more than any other site generator I've seen), but we are going to get to the point with 0.5.0 where you should be able to use data from partials, pages, config, layouts, helpers, block expressions, etc. and have the context be processed correctly at any time.

@doowb
Owner

Okay, I see now.

I found where the issue is and it's a small code change, but it might have a larger impact on the rest of the assemble context. I ran it against our tests in the main repo and everything is coming out the same. I also ran it against the test repo that @jonschlinkert updated and it uses the correct context.

Should I push this fix up?

@jonschlinkert
@doowb doowb referenced this issue from a commit in doowb/assemble
@doowb doowb fixing layout/page context issue from #424 d2f5ed9
@doowb
Owner

PR sent.

@jonschlinkert

Nice, I also tested your changes on the yfm repo and got the same result. works like a charm :-) thanks @doowb!

@StevenBlack

@hinok I just want to say your {{ stringify this }} answer above was excellent, very helpful.

I just renamed it to stringifyobj because there's already a handlebars-helper by the same name.

@jonschlinkert

@hinok agreed! great answer!

@jonschlinkert

I think this can be closed, please re-open if necessary

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.