diff --git a/docs/settings.rst b/docs/settings.rst index 0b70e461..853b5745 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -34,6 +34,8 @@ Setting name what it does ? `DEFAULT_LANG` The default language to use. Default is 'en'. `DISPLAY_PAGES_ON_MENU` Display or not the pages on the menu of the template. Templates can follow or not this settings. +`DIRECT_TEMPLATES` Tuple, containing templates to render. There should be + a html file for each of these templates in the theme. `FALLBACK_ON_FS_DATE` If True, pelican will use the file system dates infos (mtime) if it can't get informations from the metadata? diff --git a/pelican/generators.py b/pelican/generators.py index 1f72b87e..6c67f07f 100755 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -16,7 +16,6 @@ from pelican.readers import read_file _TEMPLATES = ('index', 'tag', 'tags', 'article', 'category', 'categories', 'archives', 'page') -_DIRECT_TEMPLATES = ('index', 'tags', 'categories', 'archives') class Generator(object): @@ -30,21 +29,23 @@ class Generator(object): for arg, value in kwargs.items(): setattr(self, arg, value) - def get_templates(self): - """Return the templates to use. + # templates cache + self._templates = {} + self._templates_path = os.path.expanduser(os.path.join(self.theme, 'templates')) + self._env = Environment(loader = FileSystemLoader(self._templates_path)) + + def get_template(self, name): + """Return the template by name. Use self.theme to get the templates to use, and return a list of templates ready to use with Jinja2. """ - path = os.path.expanduser(os.path.join(self.theme, 'templates')) - env = Environment(loader=FileSystemLoader(path)) - templates = {} - for template in _TEMPLATES: + if name not in self._templates: try: - templates[template] = env.get_template('%s.html' % template) + self._templates[name] = self._env.get_template(name + '.html') except TemplateNotFound: raise Exception('[templates] unable to load %s.html from %s' % ( - template, path)) - return templates + name, self._templates_path)) + return self._templates[name] def get_files(self, path, exclude=[], extensions=None): """Return a list of files to use, based on rules @@ -130,23 +131,28 @@ class ArticlesGenerator(Generator): """Generate the pages on the disk TODO: change the name""" - templates = self.get_templates() write = partial( writer.write_file, relative_urls = self.settings.get('RELATIVE_URLS') ) - for template in _DIRECT_TEMPLATES: - write('%s.html' % template, templates[template], self.context, + for template in self.settings.get('DIRECT_TEMPLATES'): + write('%s.html' % template, self.get_template(template), self.context, blog=True) + + tag_template = self.get_template('tag') for tag, articles in self.tags.items(): - write('tag/%s.html' % tag, templates['tag'], self.context, tag=tag, + write('tag/%s.html' % tag, tag_template, self.context, tag=tag, articles=articles) + + category_template = self.get_template('category') for cat in self.categories: - write('category/%s.html' % cat, templates['category'], self.context, + write('category/%s.html' % cat, category_template, self.context, category=cat, articles=self.categories[cat]) + + article_template = self.get_template('article') for article in chain(self.translations, self.articles): write(article.save_as, - templates['article'], self.context, article=article, + article_template, self.context, article=article, category=article.category) def generate_context(self): @@ -252,9 +258,8 @@ class PagesGenerator(Generator): self.context['PAGES'] = self.pages def generate_output(self, writer): - templates = self.get_templates() for page in chain(self.translations, self.pages): - writer.write_file('pages/%s' % page.save_as, templates['page'], + writer.write_file('pages/%s' % page.save_as, self.get_template('page'), self.context, page=page, relative_urls = self.settings.get('RELATIVE_URLS')) diff --git a/pelican/settings.py b/pelican/settings.py index d931cf02..85f7a6a5 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -24,6 +24,7 @@ _DEFAULT_CONFIG = {'PATH': None, 'DEFAULT_LANG': 'en', 'TAG_CLOUD_STEPS': 4, 'TAG_CLOUD_MAX_ITEMS': 100, + 'DIRECT_TEMPLATES': ('index', 'tags', 'categories', 'archives'), } def read_settings(filename):