Jinja2是Python的快速模板引擎。它具有完整的Unicode支持,自动转义,继承,宏和许多其他功能。
(使用烧瓶,jinja2,css)我想将jinja占位符放入指向我的CSS样式表的链接中,以便我将在所有页面上应用一个基本样式,并为每个页面应用独特的样式表...
from jinja2 import Environment, PackageLoader, select_autoescape environment = Environment() EXAMPLE_BODY_1 = """ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Example</title> </head> <body> <div><h2>Example Contract Term</h2></div> <div> <p> Sometimes the example_field is in the middle so needs to be lowercase, {{ example_field }} is a good example. </p> <ul> <i>All terms in this example are binding.</i> <i>The terms are made up as this is an example.</i> <i>Terms can always change.</i> </ul> </div> <div><h2>Example Contract Other Term</h2></div> <div> <p> {{ example_field }} can also be at the start of a sentence to therefore needs to be capitalised. </p> <ul> <i>Sometimes it's like the first example.</i> <i>The terms are made up as this is an example.</i> <i>Terms can always change.</i> </ul> </div> </body> </html> """ def capitalize_if_start_of_html_sentence(value, html_content): # Normalize whitespace and remove HTML tags content = ' '.join(html_content.split()) # Split the content into sentences using common sentence-ending punctuation sentences = content.split('. ') for sentence in sentences: # Further split by other sentence-ending punctuation sub_sentences = sentence.split('? ') for sub_sentence in sub_sentences: sub_sub_sentences = sub_sentence.split('! ') for sub_sub_sentence in sub_sub_sentences: # Check if the sub-sentence starts with the example_field placeholder if sub_sub_sentence.startswith('{{ example_field }}'): return value.capitalize() return value.lower() # Add the custom filter to the environment environment.filters['capitalize_if_start_of_html_sentence'] = lambda value: capitalize_if_start_of_html_sentence(value, EXAMPLE_BODY_1) # Create a template from string template = environment.from_string(EXAMPLE_BODY_1) example_merge_field = "pots and pans" # Render the template with the custom filter applied print(template.render(example_field=example_merge_field)) # Apply the filter manually to the example_field capitalized_example_field = capitalize_if_start_of_html_sentence(example_merge_field, EXAMPLE_BODY_1) # Render the template with the manually capitalized field print(template.render(example_field=capitalized_example_field))
如何有条件地修改django模板中的<title>标签? 在儿童模板(dashboard.html)中,i
{%块标题%} {%endBlock%} |应用 在儿童模板(dashboard.html)中,i
如何从内部Jinja2块中剥离凹痕(不仅是线条)? [重复]
使用Python的Jinja,我关注的是代码可读性和正确的输出。 这是我的jinja模板: m3u_template = jinja2.template( textwrap.dedent(“”“ \ #Extm3U ...
"{{ var.json.my_api_key.api_key }}"
非宽容galaxy git存储库。
LLES说,我们有定义内容块的布局模板“ layout.html”。 {%block content%} {%endBlock%} 然后,我们创建另一个模板“ somepage.html” ...
我从已注册到task_Output的任务中具有Ansible输出。 “ ansible_loop_var”:“ item”, “更改”:false, “文件”:[ { “路径”:...