How to use the render function defined in .mako (.html) directly in mako

Use the render function defined in .mako directly in mako

When you define the helper function of a template engine, you may want to use it directly from outside the template. For mako, use get_def.

Use mako (preface)

$ pip install mako

Render by directly specifying a character string

The method of rendering by directly specifying the character string is as follows.

# -*- coding:utf-8 -*-
from mako.template import Template
template = Template(u"""\
hello ${name}
""")

print(template.render(name="world"))
# hello world

Define and call a helper somewhere else.

The method to call the helper function defined in another place from another place (show.html) is as follows. When the file structure is as follows.

$ tree
.
|-- greeting.html
|-- hello_with_otherfile.py
`-- show.html

Register the top-level path used when searching for a template in Template Lookup. And it calls hello of greeting.html through the show.html template.

hello_with_otherfile.py

# -*- coding:utf-8 -*-
from mako.lookup import TemplateLookup
import os.path
here = os.path.abspath(os.path.dirname(__file__))

lookup = TemplateLookup([here])
template = lookup.get_template("show.html")
print(template.render(name="world"))

# hello world

show.html

<%namespace file="./greeting.html" name="g"/>
${g.hello(name)}

greeting.html

<%def name="hello(name)">
hello ${name}
</%def>

Use the render function defined in .mako directly in mako

Finally the main subject. How to use the render function defined in .mako directly in mako. In this case, the hello function defined in greeting.html is the function you want to use. This uses get_def as follows.

# -*- coding:utf-8 -*-
from mako.lookup import TemplateLookup
import os.path
here = os.path.abspath(os.path.dirname(__file__))

lookup = TemplateLookup([here])
hello = lookup.get_template("greeting.html").get_def("hello")

print(hello.render("world"))
# hello world

Recommended Posts

How to use the render function defined in .mako (.html) directly in mako
Use render function as pseudo html tag in mako
How to use the C library in Python
How to use the generator
How to use the exists clause in Django's queryset
How to use the model learned in Lobe in Python
How to use the decorator
How to use the __call__ method in a Python class
How to set the html class attribute in Django's forms.py
Notes on how to use marshmallow in the schema library
How to use the optparse module
How to use SQLite in Python
How to use Mysql in python
How to use ChemSpider in Python
How to use PubChem in Python
How to use python zip function
How to use the ConfigParser module
[Introduction to Python] How to use the in operator in a for statement?
How to use calculated columns in CASTable
How to use the Spark ML pipeline
How to use Google Test in C
[python] How to use __command__, function explanation
How to use the Linux grep command
How to use Anaconda interpreter in PyCharm
How to use __slots__ in Python class
How to use the IPython debugger (ipdb)
How to use Map in Android ViewPager
How to use is and == in Python
[Python] How to use the enumerate function (extract the index number and element)
[C language] How to use the crypt function on Linux [Password hashing]
How to use MkDocs for the first time
How to use Python Image Library in python3 series
How to use the graph drawing library Bokeh
Summary of how to use MNIST in Python
How to use the Google Cloud Translation API
How to use the NHK program guide API
[Algorithm x Python] How to use the list
How to get the files in the [Python] folder
How to use tkinter with python in pyenv
[Python] How to use hash function and tuple.
Use pygogo to get the log in json.
How to Mock a Public function in Pytest
How to use Python-shell
How to retrieve the nth largest value in Python
[For beginners] How to use say command in python!
How to use tf.data
How to get the variable name itself in python
How to use virtualenv
How to run the Ansible module added in Ansible Tower
How to use Seaboan
How to use shogun
How to get the number of digits in Python
How to use Pandas 2
How to use Virtualenv
[Introduction to Python] How to iterate with the range function?
How to know the current directory in Python in Blender
How to define Decorator and Decomaker in one function
How to use numpy.vectorize
How to use pytest_report_header
How to use the Raspberry Pi relay module Python
A memorandum on how to use keras.preprocessing.image in Keras