Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quickly inspecting Page/Category object in python interpreter for site #1612

Open
machow opened this issue Feb 4, 2015 · 7 comments
Open
Assignees

Comments

@machow
Copy link

machow commented Feb 4, 2015

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).

@avaris
Copy link
Member

avaris commented Apr 20, 2015

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'

@justinmayer
Copy link
Member

@machow: Is there anything else we can do to help out here?

@machow
Copy link
Author

machow commented Jun 17, 2015

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.

@justinmayer
Copy link
Member

@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?

@justinmayer justinmayer self-assigned this Jul 23, 2019
@hemebond
Copy link

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.

@avaris
Copy link
Member

avaris commented Mar 26, 2024

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
}

@hemebond
Copy link

JINJA_ENVIRONMENT = {
    'extensions': ['jinja2.ext.debug'],  # set extension
}

This is very helpful, thank you.

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

No branches or pull requests

4 participants