Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to call .length on a List #44

Closed
sethladd opened this issue Feb 14, 2015 · 11 comments
Closed

Unable to call .length on a List #44

sethladd opened this issue Feb 14, 2015 · 11 comments

Comments

@sethladd
Copy link
Contributor

Hi,

I'm trying to do this:

Data model:

class Foo {
  List<String> things = ['one', 'two'];
}

Template:

{{#foo.things.length}}
{{#foo.things}}
<ul>
  <li>{{}}</li>
</ul>
{{/foo.things}}
{{/foo.things.length}}

In other words, I'd like to check the length of a list to see if there's anything there, so I can render <ul>

The exception is:

Unhandled exception:
Closure call with mismatched arguments: function '_getMustachContext'

NoSuchMethodError: method not found: '_getMustachContext'
Receiver: Instance of '_IterableMustacheContextDecorator'
Arguments: ["length"]
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1      _MustacheContext._getContextForKey (package:mustache4dart/mustache_context.dart:75:37)
#2      _MustacheContext._getInThisOrParent (package:mustache4dart/mustache_context.dart:56:35)

Perhaps IterableMustacheContextDectorator is just dealing with iterables and not lists ?

@sethladd
Copy link
Contributor Author

Thanks for taking a look!

@valotas
Copy link
Owner

valotas commented Feb 15, 2015

From the mustache specs:

  - name: Empty List
    desc: Empty lists should behave like falsey values.
    data: { list: [ ] }
    template: '"{{#list}}Yay lists!{{/list}}"'
    expected: '""'

That said, the following should be sufficient:

{{#foo.things}}
<ul>
  <li>{{}}</li>
</ul>
{{/foo.things}}

@valotas
Copy link
Owner

valotas commented Feb 15, 2015

Is the codebase you are working on open? Maybe I can have a look at the mustache part.

@sethladd
Copy link
Contributor Author

I'll try that, thanks. I thought <ul> would be repeated for each element in things ?

Our new dartdoc uses mustache4dart: https://github.com/dart-lang/dartdoc

@valotas
Copy link
Owner

valotas commented Feb 15, 2015

Yes you are right. But your code does the same too. I'll have a closer look tomorrow.

@sethladd
Copy link
Contributor Author

The spec says what to do when you encounter an empty list: "do nothing" aka "kinda falsey". However, the inverse is not true. A non-empty list isn't truthy, it's for iteration.

Thanks for taking a look!

@sethladd
Copy link
Contributor Author

Hi! Can we reopen this issue? It's marked as "wontfix".

Thanks!

@valotas valotas reopened this Feb 28, 2015
@valotas valotas removed the wontfix label Feb 28, 2015
@valotas
Copy link
Owner

valotas commented Feb 28, 2015

I had a look at it. with 1.0.10 you should be able to do

{{^list.empty}}
<ul>
{{#list}}<li>{{.}}</li>{{/list}}
</ul>
{{/list.empty}}

I would like to avoid using the length. Is there any particular reason for using it?

@sethladd
Copy link
Contributor Author

sethladd commented Mar 7, 2015

Thanks! Can you push to pub? I'd love to start using this.

@valotas
Copy link
Owner

valotas commented Mar 15, 2015

It looks like this got off my radar. I'll publish a new version today night.

@sethladd
Copy link
Contributor Author

Thank you!

On Sun, Mar 15, 2015, 6:55 AM Georgios Valotasios notifications@github.com
wrote:

It looks like this got off my radar. I'll publish a new version today
night.


Reply to this email directly or view it on GitHub
#44 (comment)
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants