10

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.

Papouche Guinslyzinho
  • 5,277
  • 14
  • 58
  • 101
  • I like to [use Pry to discover the answer](https://andrew.kvalhe.im/use-pry-as-the-middleman-console/) to this sort of thing. – ændrük Oct 02 '14 at 17:08

1 Answers1

11

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

Will Schenk
  • 194
  • 1
  • 7
  • Finally... an answer! Thank you very much. I would like to know what is the link that you find all the information about ['config','data','blog','drafts'] I was looking at [docs](http://www.rubydoc.info/github/middleman/middleman/Middleman/Cli/Console) and I couldn't find information about those objects. I was thinking like in rails and tryied to do `Articles.all` in fact as you point out I should have tryied `blog.articles` – Papouche Guinslyzinho Nov 17 '14 at 02:36
  • My process was like thisL: (I guess I can't use new lines here!) Start up `middleman console` and type in a random method. You get an error: `undefined local variable or method `this_is_not_a_method' for #` which implies that's where you are running. `self.class` returns `Middleman::Application::MiddlemanApplication1` so you are in that context. Basically this means that all helper methods are available to you in the console. `sitemap` will return the middleman sitemap for example. – Will Schenk Nov 18 '14 at 12:49
  • When I try to use middleman functions (defined inside the 'helpers' section) it tells me `NoMethodError: undefined method `tags_to_mi' for #`. Any idea why I can't access the functions? [This thread is what I want, but the answer does not work for me](http://stackoverflow.com/questions/42144577/how-can-helpers-be-accessed-from-middleman-console) – James L. Apr 06 '17 at 14:25