A couple more questions (Page, URI/URL, HTML Minification---not gzip)

60 views
Skip to first unread message

EJelome

unread,
Sep 16, 2012, 9:49:39 AM9/16/12
to blogofil...@googlegroups.com
Hi, it's me again.

I compiled a couple of questions in order to avoid spam-like questions here in our google groups. I'm nearly finished structuring the homepage but I'm having problems doing the following:

1. Current Page's Name:
    I just want to know how to display the current page's name. I was able to get the current post's name using post.title ...
        % if post:
            post.title ## e.g., Hello World
    ... but about for pages that are not under 'post'? like: About, Contact, or even the current Category's name (e.g., Python, Linux, Rants, etc.).

    An example (from WordPress):
    single_cat_title() // returns current category name
    the_title() // returns the current page or post title

2. Current Page's URL:
    Same situation as above but the current page or category's URL.

3. HTML Minification:
    Where can I modify the buffer (sorry not sure for the term) where I can use Regex to remove spaces, tabs, and newlines of the HTML page currently viewed.

I guess that's it for now, I'm enjoying Blogofile (esp. Mako) and these are trivial things but important ones that needs to be addressed.

If you're curious where I'd be using them, then #1 and #2 are for Breadcrumbs and Page Name (Usability), and #3 is for page response time (Web Performance).

Mike Pirnat

unread,
Sep 16, 2012, 4:59:32 PM9/16/12
to blogofil...@googlegroups.com
Not sure I can answer all at once (family is calling me to dinner),
but I'll start chipping away...

On Sun, Sep 16, 2012 at 9:49 AM, EJelome <eje...@gmail.com> wrote:
> 3. HTML Minification:
> Where can I modify the buffer (sorry not sure for the term) where I can
> use Regex to remove spaces, tabs, and newlines of the HTML page currently
> viewed.

It doesn't look like there is a good hook for this (someone PLEASE
correct me if I'm wrong!)--you'll probably have to do some surgery on
Blogofile itself.

In 0.7, I think you'd be looking at what's happening inside
blogofile.writer.Writer when materialize_template is called.

In pre-0.8, I think you want to add something to
blogofile.template.Template in the write method.

It would probably be really cool to make a hook that multiple plugins
could attach to; I could see doing CSS and Javascript minification
too!

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

Mike Pirnat

unread,
Sep 17, 2012, 12:25:41 AM9/17/12
to blogofil...@googlegroups.com
Oh, right, you can also write some code to be invoked after the build
is complete and add it to your _config.py:

def _post_build():
do_whatever(...)
Message has been deleted
Message has been deleted

EJelome

unread,
Sep 19, 2012, 8:15:51 PM9/19/12
to blogofil...@googlegroups.com
I can't find any examples how to use those _build() thingy so I might just skip it for now. Thanks a bunch Mike!

EJelome

unread,
Sep 19, 2012, 8:16:45 PM9/19/12
to blogofil...@googlegroups.com
That's ok Mike, family first :) Thanks for the input, and yes, there's more than just creating and designing a blog, because there's also other factors to consider like HTML, CSS, and JS minification which can help minimize filesize and improve speed loading time.

Sorry but I'm still not knowledgeable in both Python and Blogofile, when I tried to use ${bf.write.Writer}, it causes an error, and when I call ${bf.write}, it returns an empty dict "{}", I don't know where this materialize_template is and I don't know how to use them (also the blogofile.template.Template), so sorry for that.

But for #1 and #2, I think I finally have an idea how to solve them. it seems that the bf.template_context.render_
path contains the current URL so we'll just need to extract the necessary value we want to get (the title of the current page). I will post them when it's working so the code can be optimized in case you find better approach (I know there is, I just don't know how as of the moment).

EJelome

unread,
Sep 19, 2012, 8:23:05 PM9/19/12
to blogofil...@googlegroups.com
btw, just a quick question, is elif a valid Mako statement? It doesn't seem to work, just want to clarify because I've not seen an example on Mako docs (please point to me if there is) using elif, but only if / else. I also tried using it in <% %> but still not working.

<%
    if thing in things:
        ## do this
    elif object in things:
        ## do that
    else:
        ## do nothing
%>

% if thing:
% elif object:
% endif


EJelome

unread,
Sep 19, 2012, 11:36:18 PM9/19/12
to blogofil...@googlegroups.com
Nevermind this one, Mako supports elif and any possible python statements.

EJelome

unread,
Sep 19, 2012, 11:54:52 PM9/19/12
to blogofil...@googlegroups.com
After some hours, here's the breadcrumbs code (excluding archives---not there yet), please help me improve this one (if you have the time), other's are also welcome to copy and improve it:

## filename: breadcrumbs.mako (TODO)
## Pending breadcrumbs for: Archives
<%
import re

path = bf.template_context.render_path
page_slug = ''
page_type = ''
page_name = ''

if re.search(r'^./index.html$', path):
    page_slug = 'home'
    page_type = 'page'
elif re.search(r'^/blog/index.html$', path):
    page_slug = 'blog'
    page_type = 'page'
elif not post and re.search(r'^[\w.-]+/index.html$', path):
    page_slug = path.split('/')[0]
    page_type = 'page'
elif len(path.split('/')) >= 3:
    page_slug = path.split('/')[2]
    page_type = 'category'
# elif ???: (for archives)
    # page_slug = ''
    # page_type = 'archive'
elif post and re.search(r'^[\w.-]+/index.html$', path):
    page_slug = path.split('/')[0]
    page_type = 'post'
       
page_name = ' '.join(page_slug.split('-')).title()

if post:
    categories = []
    for category in post.categories:
        categories.append('<a href="%s">%s</a>' % (category.path,
                                                   category.name.title()))
%>
<div class="breadcrumbs">
    <span class="label">You are here:</span>
    % if page_slug == 'home':
    <strong class="current">${page_name}</strong>
    % else:
    <a href="${bf.config.site.url}">Home</a>
    <span class="separator">&rarr;</span>
        % if page_type == 'page':
        <strong class="current">${page_name}</strong>
        % elif page_type == 'category':
        <a href="${bf.config.blog.url}">Blog</a>
        <span class="separator">&rarr;</span>
        <strong class="current">${page_name}</strong>
        % elif page_type == 'post':
        ${', '.join(categories)}
        <span class="separator">&rarr;</span>
        <strong class="current">${page_name}</strong>
        % endif
    % endif
</div>

EJelome

unread,
Sep 20, 2012, 9:11:11 PM9/20/12
to blogofil...@googlegroups.com
Thanks a lot Mike, so finally the breadcrumbs is finished, but there lies again a problem, a templating issue but I'll just create a new post concerning that, so other people looking for solutions for the same problem can easily find it.

Kind regards,
EJelome
Reply all
Reply to author
Forward
0 new messages