tera
tera copied to clipboard
macros doesn't work
Hi there, I'm trying use macros in my templates.
index.tpl
{% import "another.tpl" as list %}
{{ list::main() }}
another.tpl
{% macro main() %}
some content
{% endmacro main %}
but tera is keep saying this:
* Failed to parse "tpls/index.tpl"
--> 10:1
|
10 | {% import "another.tpl" as list %}␊
| ^---
|
= unexpected tag; expected end of input or some content
Is this function not fully implemented yet?
Thank you.
It is implemented. Do you have empty lines or anything before the import in index.tpl by any chance? Your example is pretty much the same as the one in the Tera examples: https://github.com/Keats/tera/blob/master/examples/basic/templates/base.html#L1 which does work...
Sorry for the late reply, I just finished my project.
So, I just created a new file to test again, I found that as long as I add a set
statement in front of ʻimport` this problem will appear.
index.tpl
{%- set name = "xy" %}
{%- import "another.tpl" as ano -%}
{{- ano::main(name=name) -}}
and the output is
* Failed to parse "tests/tpls/index.tpl"
--> 3:1
|
3 | {%- import "another.tpl" as ano -%}␊
| ^---
|
= unexpected tag; expected end of input or some content```
Thank you very much. Have a good day!
Ah yes that's an issue in the Tera grammar, the imports need to be the first statements of the files >_>
And also 'extends'.
Ah yes that's an issue in the Tera grammar, the imports need to be the first statements of the files >_>
I also encountered this problem, your answer is very useful to me, thank you very much