是否可以在表达式/变量中呈现表达式/变量?

2024-05-16 07:44:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我将从python方法向mako模板传递几个变量

假设变量是这样的

var_1 = "mentor_name"

var_2 = "mentor_pic_link"

var_3 = "<div><p>${var_1} has posted this pic ${var_2}</p></div>"

{}来自mongoDB。因此,我想在Mako模板中呈现时替换var_1var_2内的var_3

我尝试了文本替换,但不能对多个变量进行替换

代码如下所示

def send_data_to_mako():
    fragment_context = {
        'var_1': "mentor_name",
        'var_2': "mentor_pic_link",
        'var_3': "<div><p>${var_1} has posted this pic ${var_2}</p></div>",
        }
    fragment.add_content(self.system.render_template('vert_module.html', fragment_context))
<%page expression_filter="h"/>
<%! from openedx.core.djangolib.markup import HTML %>
<div class="vert-mod">
     ${HTML(var_3)}
</div>

目前产出:

${var_1} has posted this pic ${var_2}

预期产出:

mentor_name has posted this pic mentor_pic_link


Tags: namediv模板varmakohtmlcontextlink