middleman console: how to use it?

2019-03-26 00:29发布

问题:

I would like to know how to use middleman console. Is it a simple irb? What can I do with it that differs from the simple irb?

middleman console [options]         # Start an interactive console in the cont...

I have some articles and I try to do Article.all but I had this following error:

NameError: uninitialized constant Middleman::Application::MiddlemanApplication1::Article
    from (irb#1):1

and I also have local-data /data/friends.json but Friend.all output error too.

{
  "friends": [
    "Tom",
    "Dick",
    "Harry"
  ]
}

I saw that Symbol.all_symbols output a lots of middleman variable and functions but I don't really know how to use the middleman console.

回答1:

Oh man, I just spend a few hours trying to solve something that would have gone a lot faster if I had known that there was a middleman console!

I was trying to build the next/back logic for going through articles on my blog. There's a chronological thing, but I wanted to also be able to go back and forth on tags. There are a few that are tagged both "ruby" and (say) "rails", and "the next" for both tags was the same I wanted to list it together under both the tags.

The main problem was that I didn't know what sorts of things were available to me in the templates file. When you startup middleman console it loads up the configuration of your site so then you can start poking around.

$ bundle exec irb
2.0.0-p481 :001 > blog
NameError: undefined local variable or method `blog' for main:Object
from (irb):1
2.0.0-p481 :002 > exit
$ middleman console
== LiveReload is waiting for a browser to connect
2.0.0-p481 :001 > blog
=> #<Middleman::Blog::BlogData: [#<Middleman::Blog::BlogArticle: {"title"=>"Emb

etc.

What middleman console does is load up the middleman environment, and then lets you call methods on the current Middleman::Application object. If you are using a middleman extension and they've defined helpers, you can get to them here and start poking around.

Handy things:

config is the middleman config object.

data is the middleman data object, from the data directory

blog is the blog config, if you are using middleman-blog

drafts are the draft articles, if you are using middleman-blog-drafts



标签: middleman