Jasonette / JASONETTE-iOS

📡 Native App over HTTP, on iOS
https://www.jasonette.com
MIT License
5.27k stars 352 forks source link

#if issue #341

Closed darenr closed 6 years ago

darenr commented 6 years ago

I can't see what I'm doing differently than the docs example, the loop runs properly because there are the right number of items, but each item is empty.

Reproducible test case:

https://jasonbase.com/things/dmmb.json

I really wanted each letter (A, B, C ..) to go in their own section but since the (and only the) first section has additional "items" besides loop expansion markup I don't see how the template engine can handle that unless it's possible to repeat the "sections" markup? (then sections[0] would be the preample markup, sections 1.. would be from an #each over the "data". If the template engine has the equivalent of "loop.first" (jinja2 http://jinja.pocoo.org/docs/2.10/templates/) that would be super helpful

gliechtenstein commented 6 years ago

#if, #elseif, and #else are all supposed to be an item in an array. Instead of:

                "{{#each artists}}": {
                  "{{#if this.length==1 }}": {
                    "class": "header_marker",
                    "type": "label",
                    "text": "{{this}}"
                  },
                  "{{#else)}}": {
                    "class": "artist_name",
                    "type": "label",
                    "text": "{{this}}"
                  }
                }

It should be:

"{{#each artists}}":  [
                  {
                    "{{#if this.length==1 }}": {
                      "class": "header_marker",
                      "type": "label",
                      "text": "{{this}}"
                    }
                  },
                  {
                    "{{#else}}": {
                      "class": "artist_name",
                      "type": "label",
                      "text": "{{this}}"
                    }
                  }
]

Try this https://jasonbase.com/things/m5W6

As for the second part of the question, I didn't really understand what you're asking. Could you open a new issue with this question and add an example of what you're trying to achieve?

darenr commented 6 years ago

thanks - I guess the array disappears once the condition is evaluated, just there to get around ordering of #elseif blocks.

To catch these sort of gotchas it would be great to have a CLI tool that evaluates templates and dumps onto stdout the resulting markup, for exaple:

jason foo/bar/baz.json { "$jason": { "head": { ....

The other parts were really about supporting more constructs in loops, like jinja2 has loop.first, loop.last etc which would allow more powerful templating. What I've arrived at now is use jinja2 to do the power template work and emit jasonette templates to minimize data transfer.

gliechtenstein commented 6 years ago

it would be great to have a CLI tool that evaluates templates and dumps onto stdout the resulting markup

The template engine is open source so you can try building something like that https://github.com/SelectTransform/st.js

As for the looping, I still don't understand which problem you're exactly trying to solve but i'm guessing you want to access the current index while looping. The template engine does support this with $index https://selecttransform.github.io/site/transform.html

darenr commented 6 years ago

thanks, I can build a cli with that easily enough.

index is useful, but not as much use as loop.first and loop.last (because loop.last would need length) - the diea is that it's common to want to do things on an array additionally (or differently) for the first and last element, like headers/footers for example. loop.length is useful also.

loop.first | True if first iteration. loop.last | True if last iteration. loop.length | The number of items in the sequence.