How to include Post Categories in the Homepage (if possible, not directly in index.html.mako)

42 views
Skip to first unread message

EJelome

unread,
Sep 9, 2012, 5:19:47 AM9/9/12
to blogofil...@googlegroups.com
Hello,

It's me again, I'm getting quite happy so far playing with Mako, and just recently I wanted to do something but encountered a brick wall.
So this is the problem. I can't seem to include the list of post categories (along with their corresponding path/URL) in the homepage.

In the example (http://docs.blogofile.com/en/latest/templates.html#adding-blogofile-features-to-our-templates), I was able to place the top 5 recent posts in the index.html.mako. How can I possible do that but for the categories instead?

I tried:

<ul>
% for category in bf.config.blog.categories:
  <li><a href="${category.path}">${category.name}</a></li>
% endfor
</ul>
# I saw the category.path and category.name in the /_templates/blog/post.mako, so I think that's the right property for it (category).

And also, can I possible do that by NOT placing in the necessary codes in the index.html.mako, but rather on a different mako file inside the /_templates/ directory? Because placing it directly inside the the index.html.mako defeats the purpose of inheritance.
What I'm thinking is to place it in /_templates/local-navigation.mako, then call it inside /_templates/site.mako, where /_templates/site.mako will be called by index.html.mako in the root directory.

Mike Pirnat

unread,
Sep 13, 2012, 11:05:49 AM9/13/12
to blogofil...@googlegroups.com
It's definitely possible, and shouldn't be too hard. I'm on 0.7 out
of laziness, so this might not translate 100% to the 0.8 line. I have
my categories in my common right rail/sidebar thing, so it ends up
being nested a few levels -- my site.mako includes my sidebar.mako
which in turn includes my sidebar/categories.mako as well as other
things.

The categories.mako looks like:

<div class="sidebar_item">
<h3>Categories</h3>
<ul>
% for category, num_posts in bf.config.blog.all_categories:
<li><a href="${category.path}">${category}</a> (${num_posts})</li>
% endfor
</ul>
</div>

Then in my sidebar.mako:

<%include file="search.mako" />

<%include file="ads.mako" />

<%include file="recent.mako" />

%if hasattr(bf.config, "tweets"):
<%include file="tweets.mako" />
%endif

%if hasattr(bf.config, "flickr"):
<%include file="flickr.mako" />
%endif

<%include file="categories.mako" />

<%include file="archives.mako" />

And then I just include the sidebar in my site.mako:

...
<%include file="sidebar/sidebar.mako" />
...


--
Mike Pirnat
mpi...@gmail.com
http://mike.pirnat.com/

EJelome

unread,
Sep 13, 2012, 3:33:24 PM9/13/12
to blogofil...@googlegroups.com
Thanks Mike! That worked perfectly.

Also, I'm curious, where did you get those: 'categories', 'num_posts', and 'all_categories'? 
One thing that I'm having trouble at is finding all the possible keys and/or variables (like the one you used) to control and/or fetch data.
How and where did you possibly got those keys and/or variables?

Again, thank you very much for the help.

Mike Pirnat

unread,
Sep 13, 2012, 11:58:25 PM9/13/12
to blogofil...@googlegroups.com
On Thu, Sep 13, 2012 at 3:33 PM, EJelome <eje...@gmail.com> wrote:
> Thanks Mike! That worked perfectly.
>
> Also, I'm curious, where did you get those: 'categories', 'num_posts', and
> 'all_categories'?
> One thing that I'm having trouble at is finding all the possible keys and/or
> variables (like the one you used) to control and/or fetch data.
> How and where did you possibly got those keys and/or variables?

In 0.7, they get populated into the magical global "blog" variable in
_controllers/blog/__init__.py during `run`, right after the posts are
parsed and before the different pages start being emitted.

EJelome

unread,
Sep 14, 2012, 2:46:49 AM9/14/12
to blogofil...@googlegroups.com
Thanks a bunch Mike. In 0.8, I've found two __init__.py which contains some of the keys/variables you've used:
* /blogofile_blog/blogofile_blog/site_src/_controllers/blog/__init__.py
* /blogofile_blog/blogofile_blog/__init__.py

I think they're a good starting point for newbs like me and in my opinion, there should be a reference for such list, that tells what they are and how to use them.

Something like: http://codex.wordpress.org/Function_Reference

Thanks again Mike ^^,

Regards,
EJelome
Reply all
Reply to author
Forward
0 new messages