ruhoh / ruhoh.rb

http://ruhoh.com
616 stars 69 forks source link

[Feature] Allow widgets access to post and site data #164

Closed lynnfaraday closed 11 years ago

lynnfaraday commented 11 years ago

Love the new 2.1 design. It's very clean. Sorry for the spam of pull requests, but I had to re-implement a lot of stuff in the new framework and thought some of it might be useful.

One thing I haven't been able to re-implement is the "comments count" widget, which shows the number of comments on a post on the main index/pagination pages. (Necessary since none of the comment plugins support multi-post pages).

I figured out how to give widgets the context of the page (issue #160) but I need the post URL for the comment count widget.

On a related note, it would be handy to be able to access site.production_url from the widgets, if there's some way to provide it that context as well.

I'm happy to implement it myself, but I can't figure out how to access the post context from within the widget collection view. If you can point me in the right direction, I can take a stab at it.

plusjade commented 11 years ago

I've added full context to the widgets with referenced commit (to use just load the gem from git using gem 'ruhoh' , git: 'git@github.com:Gild/Gildhub.git' in Gemfile and bundle install until I push to rubygems which will be soon.)

To access global config data you can do:

{{ collection.ruhoh.config.production_url }}

This is basically drilling down into the native ruby structure, and although verbose, it will be supported. Note you basically have access to the full ruhoh object so you can call any public method using the above style.

I figured out how to give widgets the context of the page (issue #160) but I need the post URL for the comment count widget.

Within the new global context you can call the collection names as normal e.g. {{# posts.all }} .. do something {{/ posts.all }}

To get the current page you'd use {{ page.url }}

For paginator pages you have access to the current collection (the collection that holds the current page) via collection so you can do stuff with that. To get the current paginated object for the current page use collection.paginator which is the same way the paginator renders itself. Using this you can loop through and collect all the urls from the posts that are on that given page.

I'm not sure I understand your request fully though. Does that help? Can you point to a page in your blog that your'e trying to make work so I can take a look?

Overall, thanks for your help!

lynnfaraday commented 11 years ago

@plusjade - That's exactly what I needed, thanks. (Apart from it possibly causing issue #171.)

More context: I have a comments_count widget (viewable on my fork) to show something like "3 Comments" at the bottom of a post on the paginator index pages. (Since disqus and livefyre can't handle showing the /actual/ comments on a multi-post page.) The trouble was that you need the post URL to feed into the disqus/lf plugin. With your change, I was able to do:

 <a href="{{collection.ruhoh.config.production_url}}{{url}}#disqus_thread">No Comments</a>

(The "No Comments" gets replaced by the actual # of comments through javascript magic courtesy of disqus.)