Example #1
0
def render_template(template_name, template_vals=None, theme=None):
  template_vals = get_template_vals_defaults(template_vals)
  template_vals.update({'template_name': template_name})
  old_settings = _swap_settings({'TEMPLATE_DIRS': TEMPLATE_DIRS})
  try:
    tpl = loader.get_template(template_name)
    rendered = tpl.render(template.Context(template_vals))
  finally:
    _swap_settings(old_settings)
  return rendered
Example #2
0
def render_template(template_name, template_vals=None, theme=None):
    template_vals = get_template_vals_defaults(template_vals)
    template_vals.update({'template_name': template_name})
    old_settings = _swap_settings({'TEMPLATE_DIRS': TEMPLATE_DIRS})
    try:
        tpl = loader.get_template(template_name)
        rendered = tpl.render(template.Context(template_vals))
    finally:
        _swap_settings(old_settings)
    return rendered
Example #3
0
 def wrap_render(context, orig_render=template.render): 
     URLNode = django.template.defaulttags.URLNode 
     save_urlnode_render = URLNode.render 
     old_settings = _swap_settings(new_settings) 
     try: 
         URLNode.render = _urlnode_render_replacement 
         return orig_render(context) 
     finally: 
         _swap_settings(old_settings) 
         URLNode.render = save_urlnode_render 
Example #4
0
def render_template(template_name, template_vals=None, theme=None):
    register = webapp.template.create_template_register()
    register.filter('xsrf_token', xsrfutil.xsrf_token)
    template.builtins.append(register)
    template_vals = get_template_vals_defaults(template_vals)
    template_vals.update({'template_name': template_name})
    old_settings = _swap_settings({'TEMPLATE_DIRS': TEMPLATE_DIRS})
    try:
        tpl = loader.get_template(template_name)
        rendered = tpl.render(template.Context(template_vals))
    finally:
        _swap_settings(old_settings)
    return rendered
Example #5
0
def render_template(template_name, template_vals=None, theme=None):
  register = webapp.template.create_template_register()
  register.filter('xsrf_token', xsrfutil.xsrf_token)
  template.builtins.append(register)
  template_vals = get_template_vals_defaults(template_vals)
  template_vals.update({'template_name': template_name})
  old_settings = _swap_settings({'TEMPLATE_DIRS': TEMPLATE_DIRS})
  try:
    tpl = loader.get_template(template_name)
    rendered = tpl.render(template.Context(template_vals))
  finally:
    _swap_settings(old_settings)
  return rendered
Example #6
0
def render_template(template_name, template_vals=None, theme=None):
  # side-step internal register mechanism
  if not template.libraries.get('bloggart_tags', None):
    import bloggart_tags as tag_lib
    template.libraries['bloggart_tags'] = tag_lib.register

  template_vals = get_template_vals_defaults(template_vals)
  template_vals.update({'template_name': template_name})
  old_settings = _swap_settings({'TEMPLATE_DIRS': TEMPLATE_DIRS})
  try:
    tpl = loader.get_template(template_name)
    rendered = tpl.render(template.Context(template_vals))
  finally:
    _swap_settings(old_settings)
  return rendered
Example #7
0
   def template_render(self, template_name, template_vals={}):
      """Render a given Django template with values and send
      the response."""

      # Add 'template_name' to values.
      template_vals.update({'template_name': template_name})
      old_settings = _swap_settings({'TEMPLATE_DIRS': TEMPLATE_DIRS})
      try:
         # Make a 'Template' object from file and render it.
         tpl = loader.get_template(template_name)
         # 'rendered' has class 'django.utils.safestring.SafeUnicode'.
         rendered = tpl.render(template.Context(template_vals))
      finally:
         _swap_settings(old_settings)

      self.response.out.write(rendered)
def render_template(template_name, template_vals=None):
   """Shameless port of the function of the same name
   by Nick Johnson from Bloggart."""

   old_settings = _swap_settings({'TEMPLATE_DIRS': TEMPLATE_DIRS})
   try:
      tpl = loader.get_template(template_name)
      rendered = tpl.render(template.Context(template_vals))
   except Exception, e:
      raise e
Example #9
0
def load(path, debug=False, template_dirs=()): 
    """Loads the Django template from the given path. 
    It is better to use this function than to construct a Template using the 
    class below because Django requires you to load the template with a 
    method if you want imports and extends to work in the template. 
    """ 

    abspath = os.path.abspath(path) 
    if not debug: 
        template = template_cache.get(abspath, None) 
    else: 
        template = None 

    if not template: 
        directory, file_name = os.path.split(abspath) 
        new_settings = { 
            'TEMPLATE_DIRS': (directory,) + template_dirs, 
            'TEMPLATE_DEBUG': debug, 
            'DEBUG': debug, 
        } 
        old_settings = _swap_settings(new_settings) 
        try: 
            template = django.template.loader.get_template(file_name) 
        finally: 
            _swap_settings(old_settings) 
        if not debug: 
            template_cache[abspath] = template 

        def wrap_render(context, orig_render=template.render): 
            URLNode = django.template.defaulttags.URLNode 
            save_urlnode_render = URLNode.render 
            old_settings = _swap_settings(new_settings) 
            try: 
                URLNode.render = _urlnode_render_replacement 
                return orig_render(context) 
            finally: 
                _swap_settings(old_settings) 
                URLNode.render = save_urlnode_render 
        template.render = wrap_render 
    return template
Example #10
0
TEMPLATE_DIRS = ['thegrandlocus_theme/templates', 'templates']


def render_template(template_name, template_vals=None):
   """Shameless port of the function of the same name
   by Nick Johnson from Bloggart."""

   old_settings = _swap_settings({'TEMPLATE_DIRS': TEMPLATE_DIRS})
   try:
      tpl = loader.get_template(template_name)
      rendered = tpl.render(template.Context(template_vals))
   except Exception, e:
      raise e
   finally:
      _swap_settings(old_settings)
   return rendered


class HardHTMLServer(webapp.RequestHandler):
   """Server for 'hard' HTML files. Provides only Django templating.
   All hard HTML files have to be in the 'templates' directory."""

   def get(self, path):
      path = re.sub('/$', '', path)
      if path == '':
         path = 'index.html'
      try:
         rendered = render_template(path)
      except Exception:
         rendered = render_template('404.html')