Added more docs

This commit is contained in:
Antonio de la Rosa 2022-03-20 00:33:32 +01:00
parent 131e93c679
commit eb93be38ea
2 changed files with 88 additions and 3 deletions

View file

@ -18,7 +18,14 @@ def env_theme(module, cache_enabled=True, cache_impl='', cache_args={}, module_d
Args:
module (str): The module where the templates can be founded
cache_enabled (boolean): If True
cache_enabled (boolean): If True then mako template cache is enabled, is False, mako template cache is disabled.
cache_args (dict): Cache Args dict parameter for TemplateLookup function from Mako templates. View Mako Templates documentation.
module_directory (str): Module directory parameter for TemplateLookup function from Mako templates. View Mako Templates documentation.
Returns:
TemplateLookup: Return TemplateLookup object
"""
ext=module[len(module)-3:]
@ -45,10 +52,26 @@ def env_theme(module, cache_enabled=True, cache_impl='', cache_args={}, module_d
return TemplateLookup(directories=search_folders, default_filters=['h'], input_encoding='utf-8', encoding_errors='replace', cache_enabled=cache_enabled, cache_impl=cache_impl, cache_args=cache_args, module_directory=module_directory, filesystem_checks=config.reloader)
class PTemplate:
"""A class used how shortcuts for Mako template functions.
"""
templates_loaded={}
def __init__(self, environment, prepare_gettext=True):
def __init__(self, environment):
"""A class used how shortcuts for Mako template functions.
This class is used to have a set of shortcuts and hooks to Mako templates functions and methods over a series of default options.
Args:
environment (TemplateLookup): A TemplateLookup object generated with env_theme function
Attributes:
autoescape_ext (set): A set of extensions file where automatic autoescape is used
environment (TemplateLookup): A TemplateLookup object generated with env_theme function
filters (list): A list of functions used for add filters to your templates.
js (list): A list of javascript sources for generate js html load tags.
"""
self.autoescape_ext=('html', 'htm', 'xml', 'phtml', 'js')
@ -75,6 +98,17 @@ class PTemplate:
self.add_filter(self.load_js)
def add_js(self, js, module=''):
"""Function for add js to self.js attribute
Add a js file name to an attribute list called self.js with the <script> tag to add it to the header of an html code
Args:
js (str): The name of js file. Normally you save the file in media/js folder in module or theme
module (str): Optional. Where is saved the js file.
Returns:
None: Dummy return necessary because mako expect return something
"""
if not js in self.js:
self.js[js]='<script language="Javascript" src="{}"></script>'.format(make_media_url('js/'+js, module))
@ -91,6 +125,18 @@ class PTemplate:
"""
def load_template(self, template_file, **arguments):
"""Load a mako template and return the result
Load a mako template and return the results with different arguments applied
Args:
template_file (str): The name of template file. The template is searched using configuration defined in self.env
**arguments (mixed): Extra arguments with variables passed to template
Returns:
Return a template rendered using mako class from self.env
"""
"""
if self.prepare_gettext:
@ -111,6 +157,11 @@ class PTemplate:
return template.render(**arguments)
def guess_autoescape(self, template_name):
"""Simple helper method for get an extension from filename
Args:
template_name (str): The template name
"""
if template_name is None or '.' not in template_name:
return False
@ -120,6 +171,17 @@ class PTemplate:
return ext in self.autoescape_ext
def render_template(self, template_file, **arguments):
"""Experimental method for parse a template
Experimental method for parse a template, similar to load_template but try cache the template loaded
Args:
js (str): The name of js file. Normally you save the file in media/js folder in module or theme
module (str): Optional. Where is saved the js file.
Returns:
None: Dummy return necessary because mako expect return something
"""
if not str(self.env.directories)+'/'+template_file in PTemplate.templates_loaded:
PTemplate.templates_loaded[str(self.env.directories)+'/'+template_file]=self.env.get_template(template_file)
@ -129,6 +191,13 @@ class PTemplate:
return PTemplate.templates_loaded[str(self.env.directories)+'/'+template_file].render(**arguments)
def add_filter(self, filter_name):
"""Method for add filters to self.filters attributes
Method for add filters to self.filters attributes for use in templates
Args:
filter_name (function): Filter function
"""
self.filters[filter_name.__name__]=filter_name