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

Dot notation for object keys with dots in the name #343

Closed
ghost opened this issue Nov 19, 2013 · 23 comments
Closed

Dot notation for object keys with dots in the name #343

ghost opened this issue Nov 19, 2013 · 23 comments

Comments

@ghost
Copy link

ghost commented Nov 19, 2013

It would be really handy for me, and maybe others† if this could be supported. A backend database library I use supports this notation so I end up with objects like this:

{
  abc: { 'something.with.dots': 42 }
}

I wish I could do something like this in Mustache:

{{abc.somehting\.with\.dots}}

or

{{abc[something.with.dots]}}

It's not that hard to work around, but I thought I'd throw it out there.

http://stackoverflow.com/questions/14901683/how-do-i-reference-a-field-name-that-contains-a-dot-in-mustache-template

@janl
Copy link
Owner

janl commented Nov 20, 2013

that would break dot notation for diving into nested objects: https://github.com/janl/mustache.js#variables (last example)

@janl janl closed this as completed Nov 20, 2013
@ghost
Copy link
Author

ghost commented Nov 20, 2013

How would it break it? I'm saying it would be useful to be able to escape dots in variable names, so that dot notation can still be used, but also allow variable names and nested var names that themselves contain dots.

Sent from my iPhone

On Nov 20, 2013, at 9:12 AM, Jan Lehnardt notifications@github.com wrote:

that would break dot notation for diving into nested objects: https://github.com/janl/mustache.js#variables (last example)


Reply to this email directly or view it on GitHub.

@ghost
Copy link
Author

ghost commented Nov 20, 2013

This naive solution is ugly but would solve the issue:

{{abc.contains.dots.doesnt}}

Sent from my iPhone

On Nov 20, 2013, at 9:12 AM, Jan Lehnardt notifications@github.com wrote:

Closed #343.


Reply to this email directly or view it on GitHub.

@janl
Copy link
Owner

janl commented Nov 20, 2013

ah, escapes, that could work :)

On 20 Nov 2013, at 16:28 , William P. Riley-Land notifications@github.com wrote:

How would it break it? I'm saying it would be useful to be able to escape dots in variable names, so that dot notation can still be used, but also allow variable names and nested var names that themselves contain dots.

Sent from my iPhone

On Nov 20, 2013, at 9:12 AM, Jan Lehnardt notifications@github.com wrote:

that would break dot notation for diving into nested objects: https://github.com/janl/mustache.js#variables (last example)


Reply to this email directly or view it on GitHub.

Reply to this email directly or view it on GitHub.

@janl janl reopened this Nov 20, 2013
@janl
Copy link
Owner

janl commented Nov 20, 2013

I’d +1 a patch.

@bobthecow
Copy link

You know, I was thinking the other day that it would be really handy for me if PHP could have spaces in variable names. It's not that hard to work around it:

${'foo bar'} = 'baz';

… but it really chafes that there are, like, 10 characters I'm not allowed to use in variable names. I wish I could do something more like this:

$foo\ bar = 'baz';

So rather than change my code and data to adapt to the (few) restrictions placed on me by the language, I'd like the core language to adapt to something that's more convenient to me. I just thought I'd throw it out there.

:)

@ghost
Copy link
Author

ghost commented Nov 20, 2013

Haha, might work better if you file a bug on the PHP issue tracker ;)

I'll give the patch a go!

@bobthecow
Copy link

Seriously though, Mustache lets you do almost anything you want. You can't start a name with #^/!><={& or ], and you can't have }} or . anywhere in a name. Other than that it's pretty wide open.

Mustache's stance has always been that you should make your data / view model fit the template, not the other way around.

@janl
Copy link
Owner

janl commented Nov 20, 2013

@bobthecow sure, but adding -escapes seems innocent enough, if it can be done nicely.

@bobthecow
Copy link

@janl But what about the previously perfectly acceptable {"foo\\": {bar: 'baz'}}? Now you need to escape the escaping: {{ foo\\.bar }}

@janl
Copy link
Owner

janl commented Nov 20, 2013

so?

@bobthecow
Copy link

It's a backwards compatibility break and a non-standard mustache behavior to work around something that should probably be solved by a proper view model anyway. But it's your call, not mine. I'm just sharing my opinion :)

@dasilvacontin
Copy link
Collaborator

I'll close this issue due to inactivity. If it's still relevant, leave a comment and I'll reopen.

@alaamebrahim
Copy link

you can replace dots with "" before you deserialize your json
so the code should actually look like this
{ abc: { 'somethingwithdots': 42 } }

@newasmod
Copy link

Dudes, please help! i have object key properties with dots in name. I need to set nested properties, it doesnot work. set(object, 'a.b.c.d',12345) sets obj = {a: {b: {c:{d:12345}}}} ,
but i need obj = {'a.b.c.d' : 12345}

@allenrowland
Copy link

You should be able to call it like this: obj['a.b.c.d'];

@DanielUranga
Copy link

Was affected by this bug, been able to workaround but would be very good to see it fixed.

@FabricioJardin
Copy link

@DanielUranga yah :'(

@FabricioJardin
Copy link

I solved with the dot-object lib (https://github.com/rhalff/dot-object) before passing my obj with values to render:

import * as dot from 'dot-object'
import * as Mustache from 'mustache'

const vars = dot.object({
"PRODUCT.TITLE": "All Star Test",
"PRODUCT.BRAND": "Converse All Star",
})

Mustache.render("{{PRODUCT.TITLE}} - {{PRODUCT.BRAND}}", obj)

@charlysotelo
Copy link

Was affected by this.

Escaping characters is basic behavior guys... How is this not supported?

@anlesk
Copy link

anlesk commented Mar 3, 2020

Yeah, sadly, I've faced that as well.

@mnmami
Copy link

mnmami commented Dec 7, 2021

Is this still not considered?

@ThorbenJ
Copy link

I know this is closed, but I still wish dedotting was not necessary. If escaping is not the way, what about quoting?
e.g. {{{ a."b.c".d }}}
Or allow a custom tokeniser that takes a string and passes back an array.
e.g. the current implementation would be:

M = require("mustache")
M.tokenise_path = function (path) {
    return path.split('.')
}

However advanced users could provide their own tokenisers and decide for themselves how to handle various characters.

FYI for anyone else, this is currently what I do:

    function deDot (obj) {
        for (var k in obj) {
            if (k.indexOf('.')>0) {
                var nk = k.replaceAll('.', '~')
                delete Object.assign(obj, {[nk]: obj[k] })[k]
            }
            if (obj[k] instanceof Object) {
                deDot(obj[k])
            }
        }
    }

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