Esempio n. 1
0
    def load_blocks(self):
        """Loads the asset blocks defined in the template
        handles:
         * extends - to track template hierachy
         * css,javascript - start of asset
         * endcss, endjavascript - end of asset
         * {{ .. }} - expansion of variables to settings variables according to VAR_EXPANSIONS
         """
        try:
            template_string, _filepath = filesystem.load_template_source(self.templatepath)
        except TemplateDoesNotExist:
            template_string, _filepath = app_directories.load_template_source(self.templatepath)

        self.content_hash = hash(template_string)

        try:
            result = TemplateAssetBucket()

            l = Lexer(template_string, self.templatepath)
            within = None
            texts = []
            for m in l.tokenize():
                if m.token_type == TOKEN_BLOCK:
                    split = m.split_contents()
                    typ = split[0]

                    if typ == "extends":
                        if split[1].endswith('"') or split[1].endswith("'"):
                            self.extends = split[1].strip('"').strip("'")
                        else:
                            pass #TODO figure out support for variable expansion
                    elif typ in TemplateAssetBlock.BLOCKTAGS:
                        within = typ
                        prop = _parse_asset_parameters(m.split_contents())
                    elif typ.startswith('end'):
                        if typ[3:] == within:
                            within = None
                            result.append(TemplateAssetBlock(''.join(texts), template=self, **prop))
                        elif typ[3:] in TemplateAssetBlock.BLOCKTAGS:
                            assert false, "encountered dangling %s tag in '%s'" % (typ,self.templatepath)
                elif within:
                    if m.token_type == TOKEN_TEXT:
                        texts.append(m.contents)
                    elif m.token_type == TOKEN_VAR:
                        v = VAR_EXPANSIONS.get(m.contents,'')
                        if v:
                            texts.append(v)
                        #? else:
                        #assert False, "Variable replacement in client side magic not yet supported"

            return result
        except UnicodeDecodeError:
            return "/* could not load %s as a template */\n" % templatepath
Esempio n. 2
0
def do_include_raw(parser, token):
  """ Performs a template include without parsing the context, just dumps the template in.
  """
  bits = token.split_contents()
  if len(bits) != 2:
    raise template.TemplateSyntaxError, "%r tag takes one argument: the name of the template to be included" % bits[0]

  template_name = bits[1]
  if template_name[0] in ('"', "'") and template_name[-1] == template_name[0]:
    template_name = template_name[1:-1]

  source, path = load_template_source(template_name)

  return template.TextNode(source)
Esempio n. 3
0
def do_include_raw(parser, token):
    """
    Performs a template include without parsing the context, just dumps the template in.
    """
    bits = token.split_contents()
    if len(bits) != 2:
        raise TemplateSyntaxError, "%r tag takes one argument: the name of the template to be included" % bits[0]

    template_name = bits[1]
    if template_name[0] in ('"', "'") and template_name[-1] == template_name[0]:
        template_name = template_name[1:-1]

    source, path = load_template_source(template_name)

    return template.TextNode(source)
Esempio n. 4
0
def load_template_source(name, dirs=[]):
  app = apputil.get_current_app()
  if not app:
    logging.info("No app found while loading template %s" % name)
    raise TemplateDoesNotExist("No app: " + name)
  if app in APP_REMAP:
    app = APP_REMAP[app]
  # We can import the app, and then find it in sys.modules,
  # thereby, in cases of "a.b.c", finding the whole thing, and not
  # just "a".
  __import__(app)
  app_module = sys.modules[app]
  app_dir = os.path.dirname(app_module.__file__)
  default_dir = os.path.join(app_dir, 'templates')

  return app_directories.load_template_source(name, [default_dir])
Esempio n. 5
0
def load_template_source(name, dirs=[]):
    app = apputil.get_current_app()
    if not app:
        logging.info("No app found while loading template %s" % name)
        raise TemplateDoesNotExist("No app: " + name)
    if app in APP_REMAP:
        app = APP_REMAP[app]
    # We can import the app, and then find it in sys.modules,
    # thereby, in cases of "a.b.c", finding the whole thing, and not
    # just "a".
    __import__(app)
    app_module = sys.modules[app]
    app_dir = os.path.dirname(app_module.__file__)
    default_dir = os.path.join(app_dir, 'templates')

    return app_directories.load_template_source(name, [default_dir])
Esempio n. 6
0
def load_template_source(template_name, template_dirs=None):
    local_template_name = '%s/%s' % (get_language(), template_name)
    return app_directories.load_template_source(local_template_name, template_dirs)
Esempio n. 7
0
def load_template_source(template_name, template_dirs=None):
    local_template_name = '%s/%s' % (get_language(), template_name)
    return app_directories.load_template_source(local_template_name,
                                                template_dirs)