CloudyKit / jet

Jet template engine
Apache License 2.0
1.26k stars 106 forks source link

about block usage #140

Closed xiusin closed 4 years ago

xiusin commented 4 years ago

Sorry for my English, I hope you can understand it

{{block art(row=10, ids="", sort="") }}
    {{ range k,v := art(row, ids, sort) }}
    {{yield content}}
    {{end}}
{{end}}

I want to use the value of range with content

eg:

{{yield art() content}}
<div>{{k}} => {{v}}</div>
{{end}}

output:

<div>1 => 2</div>
<div>3 => 4</div>

Like this.

sauerbraten commented 4 years ago

With the first line, {{block art(row=10, ids="", sort="") }}, you defined a block named art with three arguments (and a default values). That's why the next line, {{ range k,v := art(row, ids, sort) }}, can't work: it tries to call a function called art (that does not exist) with the parameters passed into the block when it's used later. And you don't even pass in arguments to the block when you yield it later in your second code block.

Instead, try doing something like that, using a map to pass the actual values you range over into the block:

file1.jet:

{{ block pair(k, v) }}
    <div>{{k}} => {{v}}</div>
{{ end }}

{{ block art(kvPairs) }}
    {{ range k, v := kvPairs }}
    {{ yield pair(k, v) }}
    {{ end }}
{{ end }}

file2.jet:

{{ import "./file1.jet" }}
{{ pairs := map(1, 2, 3, 4) }}
{{ yield art(pairs) }}

You have to import the blocks from another file so they aren't rendered immediately (https://github.com/CloudyKit/jet/wiki/3.-Jet-template-syntax#simple-example).

I don't think what you want to do, providing a template to execute as the yield content, is possible.

xiusin commented 4 years ago

@sauerbraten thinks, i got it.