Permalink
Browse files

Add partial templates

  • Loading branch information...
1 parent 1275bd2 commit d38d037e687752a3ddf8c0ba1c0634acbe4bd51c @mdiep committed Jul 7, 2011
Showing with 63 additions and 24 deletions.
  1. +27 −6 README.mdown
  2. +36 −18 fireproof.py
View
@@ -64,6 +64,33 @@ In your template, you want to list out the 10 most recent entries:
Pretty simple! It's not too much harder move your entries into subdirectories
based on year and month and provide archives of all your entries.
+If you want to add more complicated markup for the entries—and share it between
+the individual pages and main index—you can move the logic into a _partial_
+template.
+
+Create `templates/_entry.html`:
+
+ <h1>{{ entry.title }}</h1>
+ {{ entry.text }}
+
+Now `{{ entry }}` will return the value of this partial template in the other
+templates.
+
+If you create a partial for a page type but don't create a normal template, you
+can create a default template and use the value of a partial. Since all pages
+are pages, this default template should be called `page.ext`.
+
+So create `templates/page.html`:
+
+ <html>
+ <head>
+ <title>{{ page.title }} ({{ page.date|strftime("%B %d) }})</title>
+ </head>
+ <body>{{ page }}</body>
+ </html>
+
+Now `templates/entry.html` can be deleted.
+
## The Site
The site object contains only one real standard property. Any other properties
@@ -219,12 +246,6 @@ for more information on how to use it.
#### Add a notion of drafts
#### Add a template function to retrieve other types of files, like images
#### Add examples
-#### Add a convention for includable templates for stringification of pages.
-
-The plan here is to add `_page-type.ext` files that are rendered when
-stringifying the page inside a template (`{{ page }}`). This will reduce
-duplication of template code between individual pages and list pages.
-
#### Make the template directory configurable
#### Support for sites that don't exist at the domain root
#### Pagination
View
@@ -59,7 +59,12 @@ def __init__(self, site, path):
setattr(self, key, value)
def __str__(self):
- return self.text
+ try:
+ template = '_' + self.type + self.site.page_exts[self.site.current_page.type]
+ template = self.site.template_env.get_template(template)
+ return self.site.render(self, template)
+ except jinja2.TemplateNotFound:
+ return self.text
@property
def absolute_url(self):
@@ -113,7 +118,15 @@ def __init__(self, dir):
# 1) find templates and infer page types
self.find_templates()
- # 2) make a list of all directories and files: pages, images
+ # 2) set up template environment
+ loader = jinja2.FileSystemLoader(self.template_dir)
+ env = jinja2.Environment(loader=loader)
+ env.filters['rfc3339'] = lambda x: pyrfc3339.generate(x, accept_naive=True)
+ env.filters['strftime'] = datetime.strftime
+ env.globals['pages'] = find_pages
+ self.template_env = env
+
+ # 3) make a list of all directories and files: pages, images
for dirpath, dirs, files in os.walk(self.directory):
self.add_dirs_and_files(dirpath, dirs, files)
@@ -169,6 +182,9 @@ def find_templates(self):
continue;
name, ext = os.path.splitext(file)
+ if name[0] == '_':
+ name = name[1:]
+
self.page_exts[name] = ext
self.pages[name] = []
@@ -188,30 +204,32 @@ def render_to_dir(self, output_dir):
shutil.copyfile(src_path, dest_path)
# 3) process all pages
- # template environment
- loader = jinja2.FileSystemLoader(self.template_dir)
- env = jinja2.Environment(loader=loader)
- env.filters['rfc3339'] = lambda x: pyrfc3339.generate(x, accept_naive=True)
- env.filters['strftime'] = datetime.strftime
- env.globals['pages'] = find_pages
-
for type in self.pages:
ext = self.page_exts[type]
for page in self.pages[type]:
- template = type + self.page_exts[page.type]
- template = env.get_template(template)
+ self.current_page = page
+ try:
+ template = type + self.page_exts[type]
+ template = self.template_env.get_template(template)
+ except jinja2.TemplateNotFound:
+ template = 'page' + self.page_exts['page']
+ template = self.template_env.get_template(template)
fullpath = os.path.join(output_dir, page.file)
stream = codecs.open(fullpath, 'w', encoding='UTF-8')
- context = {
- 'site': self,
- 'page': page,
- type: page,
- 'now': datetime.now(),
- }
- for line in template.stream(**context):
+ for line in self.render(page, template):
if ext == '.html':
line = line.encode('ascii', 'named_entities')
stream.write(line)
+ self.current_page = None
+
+ def render(self, page, template):
+ context = {
+ 'site': self,
+ 'page': page,
+ page.type: page,
+ 'now': datetime.now(),
+ }
+ return template.render(**context)
def find_pages(site, types=[], directory=None, limit=None, order_by=[]):
if not types:

0 comments on commit d38d037

Please sign in to comment.