tera icon indicating copy to clipboard operation
tera copied to clipboard

macros doesn't work

Open XyLyXyRR opened this issue 4 years ago • 5 comments

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.

XyLyXyRR avatar Nov 08 '20 03:11 XyLyXyRR

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...

Keats avatar Nov 08 '20 08:11 Keats

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!

XyLyXyRR avatar Nov 08 '20 11:11 XyLyXyRR

Ah yes that's an issue in the Tera grammar, the imports need to be the first statements of the files >_>

Keats avatar Nov 08 '20 11:11 Keats

And also 'extends'.

XyLyXyRR avatar Nov 23 '20 08:11 XyLyXyRR

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

za-songguo avatar Dec 23 '22 10:12 za-songguo