Skip to content

Commit

Permalink
Adding the template loader
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlovelltroy committed Feb 18, 2014
1 parent f86c6d7 commit ed4a2a0
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion classy_mail/template.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from django.template import Template, TemplateEncodingError
from django.template.base import (
Template,
TemplateDoesNotExist,
TemplateEncodingError,
)
from django.template.loader import make_origin
from django.template.loaders.filesystem import Loader as FilesystemLoader
from markdown import markdown
import yaml
#from BeautifulSoup import BeautifulSoup
Expand All @@ -22,3 +28,30 @@ def __init__(self, template_string, origin=None, name=None):
except UnicodeDecodeError:
raise TemplateEncodingError("Templates can only be constructed "
"from unicode or UTF-8 strings.")


class MarkdownTemplateLoader(FilesystemLoader):

def load_template(self, template_name, template_dirs=None):
source, display_name = self.load_template_source(template_name,
template_dirs)
origin = make_origin(display_name, self.load_template_source,
template_name, template_dirs)
try:
template = get_template_from_string(source, origin, template_name)
return template, None
except TemplateDoesNotExist:
# If compiling the template we found raises TemplateDoesNotExist,
# back off to returning the source and display name for the
# template we were asked to load. This allows for correct
# identification (later) of the actual template that does not
# exist.
return source, display_name


def get_template_from_string(source, origin=None, name=None):
"""
Returns a compiled Template object for the given template code,
handling template inheritance recursively.
"""
return MarkdownTemplate(source, origin, name)

0 comments on commit ed4a2a0

Please # to comment.