コード例 #1
0
ファイル: mako.py プロジェクト: xiejian0182/awifi_test
    def __init__(self, params):
        params = params.copy()
        options = params.pop("OPTIONS").copy()
        super(MakoTemplates, self).__init__(params)

        # Defaut values for initializing the MakoTemplateLookup class
        # You can define them in the backend OPTIONS dict.
        options.setdefault("directories", self.template_dirs)
        options.setdefault("module_directory", tempfile.gettempdir())
        options.setdefault("input_encoding", "utf-8")
        options.setdefault("output_encoding", "utf-8")
        options.setdefault("encoding_errors", "replace")
        options.setdefault("collection_size", 500)
        options.setdefault(
            "default_filters",
            settings.MAKO_DEFAULT_FILTERS
            if hasattr(settings, "MAKO_DEFAULT_FILTERS") else [],
        )

        # Use context processors like Django
        context_processors = options.pop("context_processors", [])
        self.context_processors = context_processors

        # Use the mako template lookup class to find templates
        self.lookup = MakoTemplateLookup(**options)
コード例 #2
0
def render_mako_templates(templates, context=None):
    """
    Render all template files in a directory and return the content. A file is considered a template
    if it starts with '_' and ends with '.html'.
    """

    template_dir = pkg_resources.resource_filename('discussion_app',
                                                   'templates/discussion')
    lookup = MakoTemplateLookup(directories=[template_dir])

    def is_valid_file_name(file_name):
        return (file_name in templates
                ) and file_name.startswith('_') and file_name.endswith('.html')

    def read_file(file_name):
        return open(template_dir + '/' + file_name, "r").read().decode('utf-8')

    def template_id_from_file_name(file_name):
        return file_name.rpartition('.')[0]

    def process_mako(template_content):
        tpl = MakoTemplate(text=template_content, lookup=lookup)
        return tpl.render_unicode(**context)

    def make_script_tag(id, content):
        return u"<script type='text/template' id='{0}'>{1}</script>".format(
            id, content)

    return u'\n'.join(
        make_script_tag(template_id_from_file_name(file_name),
                        process_mako(read_file(file_name)))
        for file_name in os.listdir(template_dir)
        if is_valid_file_name(file_name))
コード例 #3
0
ファイル: resources.py プロジェクト: xirdneh/xblock-utils
 def render_mako_template(self, template_path, context=None):
     """
     Evaluate a mako template by resource path, applying the provided context
     """
     context = context or {}
     template_str = self.load_unicode(template_path)
     lookup = MakoTemplateLookup(directories=[pkg_resources.resource_filename(self.module_name, '')])
     template = MakoTemplate(template_str, lookup=lookup)
     return template.render(**context)
コード例 #4
0
def render_mako_template(template_path, context={}):
    """
    Evaluate a mako template by resource path, applying the provided context
    """

    template_dir = pkg_resources.resource_filename('discussion_app',
                                                   'templates/discussion')
    lookup = MakoTemplateLookup(directories=[template_dir])

    template_str = load_resource(template_path)
    template = MakoTemplate(text=template_str, lookup=lookup)
    return template.render_unicode(**context)
コード例 #5
0
ファイル: renderer.py プロジェクト: Himanshu372/opentitan
    def _get_mako_template_lookup(self) -> MakoTemplateLookup:
        """ Get a Mako TemplateLookup object """

        if self._lookup is None:
            # Define the directory containing the IP template as "base"
            # directory, allowing templates to include other templates within
            # this directory using relative paths.
            # Use strict_undefined to throw a NameError if undefined variables
            # are used within a template.
            self._lookup = MakoTemplateLookup(
                directories=[str(self.ip_template.template_path)],
                strict_undefined=True)
        return self._lookup
コード例 #6
0
    def __init__(self, params):
        params = params.copy()
        options = params.pop('OPTIONS').copy()
        super(MakoTemplates, self).__init__(params)

        # Defaut values for initializing the MakoTemplateLookup class
        # You can define them in the backend OPTIONS dict.
        options.setdefault('directories', self.template_dirs)
        options.setdefault('module_directory', tempfile.gettempdir())
        options.setdefault('input_encoding', settings.FILE_CHARSET)
        options.setdefault('output_encoding', settings.FILE_CHARSET)
        options.setdefault('encoding_errors', 'replace')
        options.setdefault('collection_size', 500)
        options.setdefault(
            'default_filters', settings.MAKO_DEFAULT_FILTERS if hasattr(
                settings, 'MAKO_DEFAULT_FILTERS') else [])

        # Use context processors like Django
        context_processors = options.pop('context_processors', [])
        self.context_processors = context_processors

        # Use the mako template lookup class to find templates
        self.lookup = MakoTemplateLookup(**options)