简体   繁体   English

从 Python Flask 中的变量指定模板名称(使用 Flask Frozen)

[英]Specify template name from variable in Python Flask (using Flask Frozen)

Is there a way to assign the "template_name_or_list" parameter in render_template via a variable when freezing a Flask app via Frozen Flask?当通过 Frozen Flask 冻结 Flask 应用程序时,有没有办法通过变量分配 render_template 中的“template_name_or_list”参数? I can't work out how it might be acheived.我无法弄清楚它是如何实现的。

The below is an abbreviation of the working code that specifies the 'index.html' template by name:以下是按名称指定“index.html”模板的工作代码的缩写:

from flask import Flask, render_template
from flask_frozen import Freezer

app = Flask(__name__)
path = 'C:\\Users\\Foo\\Bar\\'

@app.route('/')
def build_page():
    div_list = path + 'index' + '.pickle'
    return render_template('index.html', div_list=div_list)

def save_page():
    freezer = Freezer(app)
    freezer.freeze()

if __name__ == '__main__':
    save_page()

This creates the index.html file in the "build" folder as expected.这会按预期在“build”文件夹中创建 index.html 文件。

What I'd like to do is pass a template name on creation, for example:我想做的是在创建时传递模板名称,例如:

    save_page('index')

and have this feed through as a variable in build_page to pick up the pickle file to import and name the page in render_template.并将此馈送作为 build_page 中的变量,以获取要导入的 pickle 文件并在 render_template 中命名页面。

In this case there would have to be a template of whatever name was passed of course, but if there might also be the option of keeping the index.html as the template, but saving it in Flask Frozen's "build" folder as the passed name that would be even better.在这种情况下,当然必须有一个任何名称的模板,但如果还可以选择保留 index.html 作为模板,但将其保存在 Flask Frozen 的“build”文件夹中作为传递的名称那会更好。

I checked out the Freezer class parameters and https://flask.palletsprojects.com/en/1.0.x/api/#flask.render_template but being fairly new to flask and frozen flask, I didnt quite grasp whether this might be possible.我检查了 Freezer 类参数和https://flask.palletsprojects.com/en/1.0.x/api/#flask.render_template但对烧瓶和冷冻烧瓶相当陌生,我不太明白这是否可能。

Thanks vm.谢谢 vm。

In the end I couldn't find a way to do this in the way I had planned.最后,我找不到按照我计划的方式做到这一点的方法。

Essentially the question was about efficiently building different html pages with the same template (with content added via jinja2).本质上,问题是关于使用相同的模板(通过 jinja2 添加内容)有效构建不同的 html 页面。

I looked through standard static site generators that use jinja2 (such as Pelican) but they all seemed to be focued on blogs and adding loads of optionality I didnt need.我查看了使用 jinja2 的标准静态站点生成器(例如 Pelican),但它们似乎都集中在博客上,并添加了我不需要的可选性负载。

I settled on building my own using jinja template inheritance explained at http://zetcode.com/python/jinja/我决定使用http://zetcode.com/python/jinja/解释的 jinja 模板继承来构建我自己的

from jinja2 import Environment, FileSystemLoader

content = 'This is about page'

file_loader = FileSystemLoader('templates')
env = Environment(loader=file_loader)

template = env.get_template('about.html')

output = template.render(content=content)
print(output)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM