Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Struggling with Categories #224

Open
robertbird opened this issue Mar 1, 2015 · 6 comments
Open

Struggling with Categories #224

robertbird opened this issue Mar 1, 2015 · 6 comments

Comments

@robertbird
Copy link

Hi there,

Is there a way to render a list of categories and tags on my homepage?

For example i've tried the following in my index.md and layout.html files:
{% for category in site.categories %}
{{ category | first }}
{% endfor %}

Sorry I'm new to this project and also to Jekyll, so this is probably a stupid question. I agree with previous comments that more documentation would be useful, if I can get up and running with this I am happy to contribute some docs.

I guess I'm struggling to understand what useful data/variables are available based on my posts alone. So for categories and tags, should I be able to get a list automatically at the site level, or do I need to declare them in my _config.yaml?

Thanks for any help you can give me!!

Rob

@laedit
Copy link
Member

laedit commented Mar 2, 2015

Hi,
Sorry for the lack of documentation, but like it's said in the ReadMe, Pretzel is very like Jekyll which has an awesome doc.
For the categories, you can access them through:

{% for category in site.categories %}
{{ category.name }}
{% endfor %}

Category have another property: posts, which regroup all posts having this particular category.

@robertbird
Copy link
Author

Thankyou @laedit for your response! I really appreciate it.

I have made some progress, but am getting some strange results.

I realised that I had not specified my categories correctly. After changing to the following format in my _config.yaml file I got a bit further:

categories: 
- cat 1
- cat 2

But I then got an error when running the site. Actually the page loaded fine but where each category name should be it showed the message:

Liquid syntax error: Object 'Pretzel.Logic.Templating.Context.Category' is invalid because it is neither a built-in type nor implements ILiquidizable

So I was getting pretty desperate and found a few posts showing how to do it with razor syntax. Seeing as the reason I tried this in the first place was loving the idea that I could use .net and razor I thought I would give it a go.

Using exactly the same blog post files with the same categories, I tried the following razor:

@foreach(var category in Model.Site.Categories)
{
  <a href="/blog/category/@category.Name">@category.Name (@category.Posts.Count())</a>
}

This worked perfectly. So I thought I was on to a winner, but then I couldn't get the pagination to work with razor.

@Model.Paginator just seems to be null. I also came accross this blog post (http://lunarfrog.com/blog/2012/05/14/pretzel/) and tried following it. I noticed that it used the _config.yaml setting of paginate_link: instead of paginate_path: which I had seen in Jekyll, but neither worked for me.

So I was stuck! Either use Razor and not be able to page my blog posts or use Liquid and not be able to list categories. Nightmare.

Any advice would be amazing!!

Thanks again,
Rob

@robertbird
Copy link
Author

Update: So it gets even more confusing!

I finally got to the point where locally I was there with everything except for tags and categories so I pushed it to github pages to call it done for the night. (This is with the Jekyll/liquid syntax)

Turns out tags and categories are working on github pages! Which is great, problem solved, no weird error messages.

BUT - now the pagination is not working! Arghh - I'm giving up for the night, but this is getting very frustrating. Feels like i'm close each time but no cigar.

For pagination I had:

{% for post in paginator.posts %}

which worked fine locally.

Again, any help appreciated!

@laedit
Copy link
Member

laedit commented Mar 3, 2015

Yeah, there is some differences between Jekyll and Pretzel, I started to list them here but I haven't made the all tour yet.

You don't need to declare all categories in _config.yml, you can declare them on each post.

The use of categories in liquid will be available in the next version, but you can use the source to try it now.

I haven't played with pagination in Pretzel or Jekyll for now, but it seems that Jekyll use paginator too: http://jekyllrb.com/docs/pagination/

@JackUkleja
Copy link

I'm struggling to get categories to work too. No matter what category I put in the front matter, it just seems to be ignored. No errors with the --debug switch either. Do I have to have an explicit permalink that include the category?

@laedit
Copy link
Member

laedit commented Jul 14, 2017

If you want the category to be in the url, yes, otherwise you just have access to categories through site.categories.
It's been a while since I take a look at categories, so I don't remember well how it works. If you still have trouble, can you link the code?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants