我的jinja2模板接收可能包含任意數量不同語言的代碼塊。我想將正確的詞法分析器傳遞給模板,並使用jinja2-highlight插件(pygments)進行相應的渲染。 我使用'command'變量呈現我的模板,該變量是一個保存模板其餘部分所需的所有數據的字典。理想情況下,我想沿着這些線路jinja2中的變量詞法分析器突出顯示
{% highlight "{{ command.lexer }}", lineno='table' %}{{ command.script }}{% endhighlight %}
我試圖做一些事情:
{% highlight command.lexer, lineno='table' %}
{% highlight 'command.lexer', lineno='table' %}
{% highlight '{{ command.lexer }}', lineno='table' %}
甚至
{% set lexer = command.lexer %}
{% highlight 'lexer', lineno='table' %}
{% highlight '{{ lexer }}', lineno='table' %}
我似乎無法找出渲染的組合jinja2和jinja2-highlight/pygments之間的規則。
我真的想不這樣做:
{% if command.lexer == 'bash' %}
{% highlight 'bash', lineno='table' %}
{% elif command.lexer == 'perl' %}
{% highlight 'perl', lineno='table' %}
...
{% endif %}
它似乎有與Jinja2的解析器類的事,但我有點卡住..感覺就像我忽視的東西不重要的。
各種錯誤消息看起來都像:
jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got ','