Hi all, Building a portfolio with Nesta and I'm trying to work out how to get a list or articles / summaries of a specific category? I have a category of portfolio and it has sub categories of portfolio/web and portfolio/visuals which I'd like listed in two separate lists on the portfolio page which has it's own view. How would I best approach this? Rgds, Stefan
On 14 Jun 2011, at 18:46, Stefan Goodchild wrote:
> I have a category of portfolio and it has sub categories of
portfolio/web and portfolio/visuals which I'd like listed in two separate
lists on the portfolio page which has it's own view. How would I best
approach this?
You could try something like this:
Page.find_by_path('/portfolio/visuals').pages.each do |page|
...
Basically, if you can get an object representing a category page, you can
iterate over its pages fairly easily.
Cheers,
Graham
On 14 Jun 2011, at 21:08, Graham Ashton wrote: > On 14 Jun 2011, at 18:46, Stefan Goodchild wrote: > >> I have a category of portfolio and it has sub categories of portfolio/web and portfolio/visuals which I'd like listed in two separate lists on the portfolio page which has it's own view. How would I best approach this? > > You could try something like this: > > Page.find_by_path('/portfolio/visuals').pages.each do |page| > ... > > Basically, if you can get an object representing a category page, you can iterate over its pages fairly easily. Thanks Graham. I'm a massive newb with Nesta / Sinatra though fairly experienced with Rails so don't really know where you're suggesting I put this? I'm guessing not in the view HAML file as A) that's icky and B) it doesn't work! :-) Stefan
On 15 Jun 2011, at 15:28, Stefan Goodchild wrote:
> I'm a massive newb with Nesta / Sinatra though fairly experienced with
Rails so don't really know where you're suggesting I put this? I'm
guessing not in the view HAML file as A) that's icky and B) it doesn't
work! :-)
It should work in a Haml or Erb view, but you can put it in a new route in
an app.rb file if you like. Looking at the documentation menu on
nestacms.com, I can see why you've not discovered how to create an app.rb
file (in which you can define your own Sinatra routes/actions). I clearly
haven't explained how to extend Nesta that well yet (though I've just
added it to the todo list).
In the meantime you can see what an app.rb file should look like in the
template engines docs. If you read up on Sinatra you'll be able to fill in
the rest from there.
http://nestacms.com/docs/design/templating-engines
You said (in an earlier message) that your portfolio page has its own
view. Do you mean it's got its own template in ./views, or that you've
written it as a Haml page (i.e. content/pages/portfolio.haml)?
Unless the layout is significantly different to the other pages on your
site I'd probably go with the content/pages/portfolio.haml approach, and
then put a couple of Ruby loops inline in that template. You can add
helper methods to the app.rb file (inside the helpers block) to make the
code in the Haml file cleaner.
If none of this is making sense, give me access to your Git repo and I'll
send you a patch illustrating what I mean.
Cheers,
Graham
On 15 Jun 2011, at 15:54, Graham Ashton wrote: > On 15 Jun 2011, at 15:28, Stefan Goodchild wrote: > >> I'm a massive newb with Nesta / Sinatra though fairly experienced with Rails so don't really know where you're suggesting I put this? I'm guessing not in the view HAML file as A) that's icky and B) it doesn't work! :-) > Looking at the documentation menu on nestacms.com, I can see why you've not discovered how to create an app.rb file (in which you can define your own Sinatra routes/actions). I clearly haven't explained how to extend Nesta that well yet (though I've just added it to the todo list). Took me a little while but I think I'm getting it now. Used to overriding things in Rails anyway it's just sometimes it's too similar! But some docs would be handy of course :-) > You said (in an earlier message) that your portfolio page has its own view. Do you mean it's got its own template in ./views, or that you've written it as a Haml page (i.e. content/pages/portfolio.haml)? I've tried both ways, both in a ./views/portfolio.haml template and in ./pages/portfolio.haml. Same error in either. NameError at /portfolio uninitialized constant Haml::Engine::Page > Unless the layout is significantly different to the other pages on your site I'd probably go with the content/pages/portfolio.haml approach, and then put a couple of Ruby loops inline in that template. You can add helper methods to the app.rb file (inside the helpers block) to make the code in the Haml file cleaner. I think I'll eventually need it to have it's own Template in ./views due to how it will be laid out but at the moment I'm simply working with some dummy data and the standard theme and templates to get the information appearing and arranged how I want it. > If none of this is making sense, give me access to your Git repo and I'll send you a patch illustrating what I mean. Will do so if this round of emails doesn't get me there :-) Much obliged, Stefan
On 15 Jun 2011, at 16:17, Stefan Goodchild wrote: > NameError at /portfolio > uninitialized constant Haml::Engine::Page Try referring to it as Nesta::Page.... - that should sort it.
On 15 Jun 2011, at 16:31, Graham Ashton wrote: > On 15 Jun 2011, at 16:17, Stefan Goodchild wrote: > >> NameError at /portfolio >> uninitialized constant Haml::Engine::Page > > Try referring to it as Nesta::Page.... - that should sort it. No more error but not finding pages. I tried: =Nesta::Page.find_by_path('portfolio').pages.length and =Nesta::Page.find_by_path('portfolio/web').pages.length =Nesta::Page.find_by_path('portfolio/visuals').pages.length in /pages/portfolio.haml and it returns 0 All the articles under portfolio are listed as expected as per default NestaCMS behaviour. I've added you to my repo on Github so feel free to take a look but there's nothing much in there :-) Would be interested in how you would move the logic into app.rb as you mentioned earlier. Rgds, Stefan
On 15 Jun 2011, at 16:47, Stefan Goodchild wrote: > > No more error but not finding pages. I tried: > > =Nesta::Page.find_by_path('portfolio').pages.length The wasn't really a problem with the code, more with expectations. If you have pages stored on disk like this: content/pages/portfolio.mdown content/pages/portfolio/web.mdown content/pages/portfolio/visuals.mdown then there's nothing in that file structure to indicate that portfolio.mdown is a category page. They're just three totally independent web pages. There isn't much magic in Nesta; you need to tell it what you want it to do. To convert /portfolio into a category page you need to put a page in the portfolio category. You do that by adding meta data to the page that you want to put into the category. I've just pushed a couple of commits to your repo. The first one puts web and visuals into the portfolio category, adding this line to the top of portfolio/web.mdown and portfolio/visuals.mdown: Categories: portfolio http://nestacms.com/docs/creating-content/metadata-reference#categories Scanning through Nesta's docs, I'm not seeing anything that walks you through how categories work, which is a bit of a cock up on my part. > Would be interested in how you would move the logic into app.rb as you mentioned earlier. My second commit to your repo added an app.rb file, with a view helper in. Here's the patch in case anybody else was following along: +++ b/app.rb @@ -0,0 +1,9 @@ +module Nesta + class App + helpers do + def pages_in(path) + Nesta::Page.find_by_path(path).pages + end + end + end +end And this how you use it in the Haml file: --- a/content/pages/portfolio.haml +++ b/content/pages/portfolio.haml @@ -2,10 +2,10 @@ %p This is my work, all I've done that's fit to print -=Nesta::Page.find_by_path('portfolio').pages.length += pages_in('portfolio').length %ul - -Nesta::Page.find_by_path('portfolio/web').pages.each do |page| - =page.heading + - pages_in('portfolio/web').each do |page| + = page.heading Hope that helps... Cheers, Graham
> My second commit to your repo added an app.rb file, with a view helper in.
Thanks for that, makes sense how that abstracts it out and away from the
view which feels nicer however it's still not behaving as I'd expect.
I've pushed some more content into my repo including four pages into the
'portfolio/web' folder with the category 'portfolio/web' in the header of
the file like so.
Categories: portfolio/web
When I use your helper in portfolio.haml
- pages_in('portfolio/web').each do |page|
= page.heading
I get no results at all but if I visit the "portfolio/web" page it lists
them correctly so the pages themselves are categorised correctly. Am I
missing something obvious?
Thanks for you help so far, it's appreciated :-)
Rgds, Stefan
On 19 Jun 2011, at 20:30, Stefan Goodchild wrote: > When I use your helper in portfolio.haml > > - pages_in('portfolio/web').each do |page| > = page.heading > > I get no results at all but if I visit the "portfolio/web" page it lists them correctly so the pages themselves are categorised correctly. Am I missing something obvious? Not obvious, no. I had to scan the code in the nesta gem to spot it. The Page#pages method (which I used in the helper I wrote for you) filters out articles (i.e. pages with a date, or blog posts). I've added a new helper for you which iterates over the articles instead: def articles_in(path) Nesta::Page.find_by_path(path).articles end Cheers, Graham
On 19 Jun 2011, at 21:36, Graham Ashton wrote: > On 19 Jun 2011, at 20:30, Stefan Goodchild wrote: > >> When I use your helper in portfolio.haml >> >> - pages_in('portfolio/web').each do |page| >> = page.heading >> >> I get no results at all but if I visit the "portfolio/web" page it lists them correctly so the pages themselves are categorised correctly. Am I missing something obvious? > > Not obvious, no. I had to scan the code in the nesta gem to spot it. > > The Page#pages method (which I used in the helper I wrote for you) filters out articles (i.e. pages with a date, or blog posts). I've added a new helper for you which iterates over the articles instead: > > def articles_in(path) > Nesta::Page.find_by_path(path).articles > end Brilliant. I did wonder about the difference between pages and articles actually, thank you for your help. I can get on with my website now :-) Regards, Stefan
On 15 Jun 2011, at 16:47, Stefan Goodchild wrote: > I tried: > > = Nesta::Page.find_by_path('portfolio').pages.length I can't see any reason why that would give you 0. > I've added you to my repo on Github so feel free to take a look but there's nothing much in there :-) Would be interested in how you would move the logic into app.rb as you mentioned earlier. Okay, I'll take a look and get back to you. It won't be today though I'm afraid; I'm up to my ears in client work. Graham