Саш*_*ных 4 python jinja2 python-3.x pelican
我无法在 Jinja2 示例中设置变量内部使用变量。
解决方案必须与Pelican兼容。
Any text
Goddess Kira greatest of all time!
Goddess Kristina greatest of all time!
Another text
Goddess Sasha greatest of all time!
Goddess Katya greatest of all time!
Run Code Online (Sandbox Code Playgroud)
Any text
Goddess Kira greatest of all time!
Goddess Kristina greatest of all time!
Another text
Goddess Sasha greatest of all time!
Goddess Katya greatest of all time!
Run Code Online (Sandbox Code Playgroud)
我有重复的Goddess和greatest of all time!循环的。我可以不使用重复项吗?
这个、这个、这个以及许多其他Stack Overflow 问题。
例如,以下代码不起作用:
"""Jinja2 nested variables example."""
from jinja2 import Template
KIRA_BLOCK = """
{% set first_list = ['Kira', 'Kristina'] %}
Any text
{% for name in first_list %}
Goddess {{name}} greatest of all time!
{% endfor %}
{% set second_list = ['Sasha', 'Katya'] %}
Another text
{% for name in second_list %}
Goddess {{name}} greatest of all time!
{% endfor %}
"""
print(Template(KIRA_BLOCK).render())
Run Code Online (Sandbox Code Playgroud)
我得到回溯:
"""Jinja2 nested variables example."""
from jinja2 import Template
KIRA_BLOCK = """
{% set she_greatest = 'Goddess' {{name}} 'greatest of all time!' %}
{% set first_list = ['Kira', 'Kristina'] %}
Any text
{% for name in first_list %}
{{she_greatest}}
{% endfor %}
{% set second_list = ['Sasha', 'Katya'] %}
Another text
{% for name in second_list %}
{{she_greatest}}
{% endfor %}
"""
print(Template(KIRA_BLOCK).render())
Run Code Online (Sandbox Code Playgroud)
为了防止XY 问题,我发布了截断的真实代码:
Traceback (most recent call last):
File "KiraJinja2.py", line 18, in <module>
print(Template(KIRA_BLOCK).render())
File "C:\Python37\lib\site-packages\jinja2\environment.py", line 945, in __new__
return env.from_string(source, template_class=cls)
File "C:\Python37\lib\site-packages\jinja2\environment.py", line 880, in from_string
return cls.from_code(self, self.compile(source), globals, None)
File "C:\Python37\lib\site-packages\jinja2\environment.py", line 591, in compile
self.handle_exception(exc_info, source_hint=source_hint)
File "C:\Python37\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python37\lib\site-packages\jinja2\_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<unknown>", line 2, in template
File "C:\Python37\lib\site-packages\jinja2\environment.py", line 497, in _parse
return Parser(self, source, name, encode_filename(filename)).parse()
File "C:\Python37\lib\site-packages\jinja2\parser.py", line 901, in parse
result = nodes.Template(self.subparse(), lineno=1)
File "C:\Python37\lib\site-packages\jinja2\parser.py", line 888, in subparse
self.stream.expect('block_end')
File "C:\Python37\lib\site-packages\jinja2\lexer.py", line 384, in expect
self.name, self.filename)
jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got '{'
Run Code Online (Sandbox Code Playgroud)
该示例显示了 static 的位置和变量的位置。
<div class="KiraCategory">First</div>
<a onclick="KiraFunction(document.all.sitename.value, 'https://Alpha.com');" class="Foo">Alpha</a>
<a onclick="KiraFunction(document.all.sitename.value, 'https://Bravo.ru');" class="Bar">Beta</a>
<div class="KiraCategory">Second</div>
<a onclick="KiraFunction(document.all.sitename.value, 'https://Charlie.net');" class="Baz">Gamma</a>
Run Code Online (Sandbox Code Playgroud)
我的尝试重复。
您正在寻找Jinja宏;调用时产生输出的函数:
{% macro she_greatest(name) -%}
Goddess {{name}} greatest of all time!
{%- endmacro %}
{% set first_list = ['Kira', 'Kristina'] %}
Any text
{% for name in first_list %}
{{ she_greatest(name) }}
{% endfor %}
{% set second_list = ['Sasha', 'Katya'] %}
Another text
{% for name in second_list %}
{{ she_greatest(name) }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
演示:
>>> from jinja2 import Template
>>> demo = '''
... {% macro she_greatest(name) -%}
... Goddess {{name}} greatest of all time!
... {%- endmacro %}
...
... {% set first_list = ['Kira', 'Kristina'] %}
... Any text
... {% for name in first_list %}
... {{ she_greatest(name) }}
... {% endfor %}
... {% set second_list = ['Sasha', 'Katya'] %}
... Another text
... {% for name in second_list %}
... {{ she_greatest(name) }}
... {% endfor %}
... '''
>>> print(Template(demo).render())
Any text
Goddess Kira greatest of all time!
Goddess Kristina greatest of all time!
Another text
Goddess Sasha greatest of all time!
Goddess Katya greatest of all time!
Run Code Online (Sandbox Code Playgroud)
或者,将其应用到您的现实生活示例中:
>>> from jinja2 import Template
>>> demo = '''
... {% macro she_greatest(name) -%}
... Goddess {{name}} greatest of all time!
... {%- endmacro %}
...
... {% set first_list = ['Kira', 'Kristina'] %}
... Any text
... {% for name in first_list %}
... {{ she_greatest(name) }}
... {% endfor %}
... {% set second_list = ['Sasha', 'Katya'] %}
... Another text
... {% for name in second_list %}
... {{ she_greatest(name) }}
... {% endfor %}
... '''
>>> print(Template(demo).render())
Any text
Goddess Kira greatest of all time!
Goddess Kristina greatest of all time!
Another text
Goddess Sasha greatest of all time!
Goddess Katya greatest of all time!
Run Code Online (Sandbox Code Playgroud)