pelican
pelican copied to clipboard
Quickly inspecting Page/Category object in python interpreter for site
Hello, I have a site built with Pelican (which has been great!). It's clear from looking at the unit tests, that I should be able to instantiate the objects (for example, Page) that are fed into the jinja templates. However, I wasn't able to tell if there's a quick way to do it for an existing site.
For example, if I have a blog category on the site, is there a straightforward way to create the Category object corresponding to blog (or if that's not what is passed into the jinja template, the object that is)? I was thinking something similar to Flask's methods for testing request contexts. This would very helpful for getting a handle on using the various objects passed to jinja templates (as well as debugging).
I'm happy to add documentation / code wherever useful for this (just need to be pointed in the right direction).
Yes, you can do that, but it probably requires some familiarity with pelican's code (how to instantiate objects, etc.).
Just to give you an idea
>>> from pelican.settings import read_settings
>>> from pelican.readers import Readers
>>> from pelican.contents import Page, Article
>>> from pelican.urlwrappers import Category
>>> settings = read_settings('pelicanconf.py')
>>> reader = Readers(settings)
>>> page = reader.read_file('content', 'test.rst', Page)
>>> page.metadata
{'title': 'My super title', 'summary': '<p class="first last">Short version for index and feeds</p>\n', 'tags': [<Tag thats>, <Tag awesome>], 'authors': [<Author Alexis Metaireau>, <Author Conan Doyle>], 'modified': SafeDatetime(2010, 10, 4, 18, 40), 'category': <Category yeah>, 'slug': 'my-super-post', 'reader': 'rst', 'date': SafeDatetime(2010, 10, 3, 10, 20)}
>>> page.save_as
'pages/my-super-post.html'
>>> page.url
'pages/my-super-post.html'
>>> cat = Category('foo bar', settings)
>>> cat.name
'foo bar'
>>> cat.slug
'foo-bar'
>>> cat.save_as
'category/foo-bar.html'
>>> cat.url
'category/foo-bar.html'
@machow: Is there anything else we can do to help out here?
Sorry for the very long delay! This is incredibly helpful.
This information seems like it might be valuable in the Creating Themes section of the documentation, since that describes common variables that are made available to the jinja templates, but this makes clear how you could explicitly check for what's available.
If that seems useful, I'm happy to add it--but in any case, appreciate the helpful response.
@machow: I imagine that would indeed be helpful for folks. Would you be willing to enhance the documentation as appropriate and submit a pull request?
Unfortunately this doesn't give the same context available to templates. Normally one would be able to use {% debug %}
in a Jinja template to see everything, but it's not available in Pelican, as far as I can see.
Unfortunately this doesn't give the same context available to templates. Normally one would be able to use
{% debug %}
in a Jinja template to see everything, but it's not available in Pelican, as far as I can see.
Sure it is, but it's not "normally available". You need to activate the extension:
JINJA_ENVIRONMENT = {
'trim_blocks': True, # keep the defaults
'lstrip_blocks': True, # keep the defaults
'extensions': ['jinja2.ext.debug'], # set extension
}
JINJA_ENVIRONMENT = { 'extensions': ['jinja2.ext.debug'], # set extension }
This is very helpful, thank you.