librelist archives

« back to archive

List articles title and/or summaries by category

List articles title and/or summaries by category

From:
André
Date:
2014-04-23 @ 16:37
Hi again,

I'm rendering some of an article's metadata, namely categories, over each
article. I'm going to render each category as a link to articles with that
category - pretty straightforward.

I've understood that I need an index page in the content/pages/categories
directory. This index.haml file will have this meta-data

Template: list-categories

The list-categories.haml template (located in views) should render a
regular <ul> list of all categories that exists.

How can I render such a thing? Would that be a menu or something like
list_articles() ?

-----

In the content/pages/categories/ directory I understand that I need one
haml file for each category. I have created
content/pages/categories/project (haml file) which contains:

Template: articles

The articles.haml template (located in views) renders a list of all
articles - successfully. I also want to handle the case where I just list
articles by a certain category, such as in this case.

Looking around I see that I may do this as follows:

    - unless @page.articles.empty?

      %h1
        Articles on #todo, add dynamic category

      %section.articles
        %header
          %h2= articles_heading
        = article_summaries(@page.articles)

However, my @page.articles is in fact always empty when visiting
/categories/project even though I have an article with the metadata
"Categories: backbone, javascript, github, project"

Preferably I would like to render my articles by category as I list them
regularly:

- latest_articles(12).each do |article|
etc..

Something like:

- articles_by_cat(category, 12).each do |article|
etc..

I haven't seen a helper like that in the recipes or in any repository I
found.

Any help on this and how I may achieve what I'm after is very appreciated -
thanks!

Br

Re: [nesta] List articles title and/or summaries by category

From:
Graham Ashton
Date:
2014-04-23 @ 18:34
> On 23 Apr 2014, at 17:37, "André" <andre.drougge@gmail.com> wrote:
> 
> I've understood that I need an index page in the 
content/pages/categories directory. This index.haml file will have this 
meta-data
> 
> Template: list-categories

Hi.

Do you definitely need to have a special template, or could you just make 
content/pages/categories/index.haml and let it iterate over the categories
within the page?

> The list-categories.haml template (located in views) should render a 
regular <ul> list of all categories that exists.

I'm not sure how we'll list your categories. There's nothing special about
a category page per se, they're just pages that have associated articles.

If you happen to have put all the pages you care about inside a folder (it
seems like you might have, but I'm not 100% sure) you could filter all the
pages that way. If you confirm the logic of how we can identify the pages 
that matter (on your site) I should be able to write you a helper method 
to list them.

If I was doing this I'd manually select which ones I wanted to link to, so
that my most important content was always more visible, rather than just 
dumping a big list, but if everything is more appropriate here you'll want
a helper method to iterate over them.

> my @page.articles is in fact always empty when visiting 
/categories/project even though I have an article with the metadata 
"Categories: backbone, javascript, github, project"

Can you share a link to the repository containing this site? Feel free to 
add me (gma on GitHub) if it's private, and email me privately.

I'd like to check the metadata on the files inside your content folder...

> Something like:
> 
> - articles_by_cat(category, 12).each do |article|

`category.articles` does that for you (pull off the first 12 if you want 
to limit it). That yours is empty strongly suggests you're missing some 
metadata. Do these articles have dates defined? If not, they're not 
articles, they're just pages. `category.pages` is the method you want if 
memory serves (I'm not at my computer).

Re: [nesta] List articles title and/or summaries by category

From:
André
Date:
2014-04-23 @ 20:41
list-categories :

Yes you are right, all articles that I want to list here is in the same
folder, but not all of the articles in that folder should be listed.

--|-page
  |--articles
  |---- a page with date. category-1
  |---- a page with date. category-2
  |---- a page with date. category-1
  |--documentation
  |--other

In the view we could grab all pages within the articles folder, and filter
all pages without date and without the category "category-1".

About @page.articles being empty, I have looked into your categories (for
instance on

https://github.com/gma/nestacms.com/blob/master/content/pages/docs/recipes/automatically-reload-pages-as-you-write.mdown)
and they seem to be formatted with parent directory:

category-1
category-2

would be..

categories/category-1
categories/category-2

Adding that, I can now see a list of only the relevant articles in
@page.articles - great! But, is this really how categories should be
formatted? I would prefer if I could keep my metadata:

"Categories: backbone, javascript, github, project"

instead of..

"Categories: categories/backbone, categories/javascript, categories/github,
categories/project"

I'm going to look closer at category.pages and category.articles - thank
you!



2014-04-23 21:34 GMT+03:00 Graham Ashton <graham@effectif.com>:

> On 23 Apr 2014, at 17:37, "André" <andre.drougge@gmail.com> wrote:
>
> I've understood that I need an index page in the content/pages/categories
> directory. This index.haml file will have this meta-data
>
> Template: list-categories
>
>
> Hi.
>
> Do you definitely need to have a special template, or could you just make
> content/pages/categories/index.haml and let it iterate over the categories
> within the page?
>
> The list-categories.haml template (located in views) should render a
> regular <ul> list of all categories that exists.
>
>
> I'm not sure how we'll list your categories. There's nothing special about
> a category page per se, they're just pages that have associated articles.
>
> If you happen to have put all the pages you care about inside a folder (it
> seems like you might have, but I'm not 100% sure) you could filter all the
> pages that way. If you confirm the logic of how we can identify the pages
> that matter (on your site) I should be able to write you a helper method to
> list them.
>
> If I was doing this I'd manually select which ones I wanted to link to, so
> that my most important content was always more visible, rather than just
> dumping a big list, but if everything is more appropriate here you'll want
> a helper method to iterate over them.
>
> < div dir="ltr">
> my @page.articles is in fact always empty when visiting
> /categories/project even though I have an article with the metadata
> "Categories: backbone, javascript, github, project"
>
>
> Can you share a link to the repository containing this site? Feel free to
> add me (gma on GitHub) if it's private, and email me privately.
>
> I'd like to check the metadata on the files inside your content folder...
>
> Something like:
>
> - articles_by_cat(category, 12).each do |article|
>
>
> `category.articles` does that for you (pull off the first 12 if you want
> to limit it). That yours is empty strongly suggests you're missing some
> metadata. Do these articles have dates defined? If not, they're not
> articles, they're just pages. `category.pages` is the method you want if
> memory serves (I'm not at my computer).
>



-- 
André Drougge
Web Developer
portfolio.miphe.com

Re: [nesta] List articles title and/or summaries by category

From:
Graham Ashton
Date:
2014-04-23 @ 20:46
On 23 Apr 2014, at 21:41, André <andre.drougge@gmail.com> wrote:

> Adding that, I can now see a list of only the relevant articles in 
@page.articles - great! But, is this really how categories should be 
formatted? I would prefer if I could keep my metadata:
> 
> "Categories: backbone, javascript, github, project”

I can see that’d be more convenient for you, but Nesta obviously wouldn’t 
know where to look for your category pages. I think this may be 
highlighting a way you could improve the structure of your site.

Let’s consider your URLs. How useful is having “/categories” in your URLs?
It’s not a word that really adds any semantic value to the URL, so it’s 
not that much help to people or computers (i.e. search engines).

Have you thought about dropping the “/categories” part and moving all 
these pages up into the site’s root? 

Re: [nesta] List articles title and/or summaries by category

From:
André
Date:
2014-04-24 @ 19:32
That makes sense yes. I wanted to keep the categories flat, without the
parent directory partly because I would render it in meta data and titles.
The way I would handle that is with a with a Link text (updated to latest
version now). Is that correct?

I'll try to be more clear.

- Each category has a category_name.haml file in the category folder.
- Each of those files looks like this:

Template: articles
Link text: Bananas

Then in page_metadata where I render the categories, I render the link text
like this:

%a(href="#{category.abspath}")= category.link_text

Also, in the articles template I have a custom title for when the template
is rendering a list of category articles instead of all articles.

= @page.metadata('Link text') || 'Articles'

This works well - is it how it was meant to be used?

I still have the issue with rendering all articles/summaries with a certain
category. The /categories/ URL prefix could be skipped that's true.. but
for now I would like to keep it. I made an attempt on creating a couple of
helpers that would:

1) Render one article in a certain way
2) Filter all articles received as argument to match a certain category
---- articles_by_category(category, articles)

I went through all articles from latest_articles to match something (like
path) to the category argument, but I didn't manage to find anything in an
article that I could access. I'm starting to rethink this a bit, perhaps it
would be better with a method taking the following arguments: scope,
categories

Scope would be the folder in which the method gathers all articles.
Categories would be an array of categories, one of which would have to be
found in an article. That would enable me to look in a structure like this:

articles
-- article[cat-2, cat-5]
-- article[cat-1, cat-5]
posts
-- article[cat-12]
-- article[cat-12, cat-19]
-- article[cat-1]

I would like to grab all articles from the posts folder with category cat-1.

I have a feeling that I am missing something and that I'm taking the long
way around. You mentioned `category.articles` - where would I be able to
use this?


2014-04-23 23:46 GMT+03:00 Graham Ashton <graham@effectif.com>:

> On 23 Apr 2014, at 21:41, André <andre.drougge@gmail.com> wrote:
>
> > Adding that, I can now see a list of only the relevant articles in
> @page.articles - great! But, is this really how categories should be
> formatted? I would prefer if I could keep my metadata:
> >
> > "Categories: backbone, javascript, github, project”
>
> I can see that’d be more convenient for you, but Nesta obviously wouldn’t
> know where to look for your category pages. I think this may be
> highlighting a way you could improve the structure of your site.
>
> Let’s consider your URLs. How useful is having “/categories” in your URLs?
> It’s not a word that really adds any semantic value to the URL, so it’s not
> that much help to people or computers (i.e. search engines).
>
> Have you thought about dropping the “/categories” part and moving all
> these pages up into the site’s root?
>



-- 
André Drougge
Web Developer
portfolio.miphe.com

Re: [nesta] List articles title and/or summaries by category

From:
Graham Ashton
Date:
2014-04-24 @ 20:22
On 24 Apr 2014, at 20:32, André <andre.drougge@gmail.com> wrote:

> That makes sense yes. I wanted to keep the categories flat, without the 
parent directory partly because I would render it in meta data and titles.
The way I would handle that is with a with a Link text (updated to latest 
version now). Is that correct?

I’m afraid I have no idea what any of that means. ;-)

> [snip]
> 
> Also, in the articles template I have a custom title for when the 
template is rendering a list of category articles instead of all articles.
> 
> = @page.metadata('Link text') || 'Articles'
> 
> This works well - is it how it was meant to be used?

I don’t know enough about what you’ve done to give you a definitive yes or
no; I’d need to see (all) your code to know.

I have a suspicion that you’re using too many templates in the views 
folder, and that it’s complicating things for you. But I may be wrong.

Can I have access to a git repo with the site in it?

If you let me see it I’ll look at what you’ve got so far and say “yes 
that’s good” or “I’d do it like this...”

Don’t feel as though that’d be an imposition - it’d actually be a lot 
quicker for me.

> I still have the issue with rendering all articles/summaries with a 
certain category. The /categories/ URL prefix could be skipped that's 
true.. but for now I would like to keep it. I made an attempt on creating 
a couple of helpers that would:
> 
> 1) Render one article in a certain way
> 2) Filter all articles received as argument to match a certain category

It sounds like your second helper might be duplicating what the #articles 
method does.

i.e. Page.find_by_path(‘/categories/bananas’).articles

It’s not exactly what you’ve described, but I bet it’s more useful.

> I would like to grab all articles from the posts folder with category cat-1.

Why do you want to do it by folder? What does the HTML that you want to 
render need to have in it, and what should be excluded?

Searching by folder feels like a smell to me (though I’m sure there are 
valid use cases, in general I’d filter by a metadata key instead).

> I have a feeling that I am missing something and that I'm taking the 
long way around. You mentioned `category.articles` - where would I be able
to use this?

Anywhere that you have access to a page object (i.e. @page for the current
page, or a page that you’ve grabbed from its path [see above]). It’ll 
return all pages that have specified page object’s path in its categories.

Cheers,
Graham