olado/doT

custom property accessor

mfandl opened this issue · 7 comments

Instead of . in {{=it.something.prop}} I would like to use custom property accessor. for example {{=it-something-prop}}.

Is this possible somehow atm? If not, is there interest in such a feature? I will be happy to make a PR.

This feature is very redundant. I don't believe there is a template engine will particularly do it since you can always preprocess your string by string.replace() before the compilation if you really need it.

Also, it.something is a standard JS syntax. What is really happened is doT take the regular expression which matches {{= (...) }}, where (...) is a JS variable and run the variable, so that you can also do something like {{= it[something][prop] }} that will work. So are you going to tell me you want to change the JS syntax here? I don't think so.

Thanks for your reply @leoyli. I get your points. To clarify - my use case may sound weird, but I would like to use the delimiters in an scss file and still the scss file be syntactically correct so it can play well with other tools. Something like this:

##{$it-prop1-prop1a} {
  color: red;
}

And scss does not support . in variable names. I understand that this is probably a niche use-case and does not belong to doT.

Regarding the second paragraph, namely the last 2 sentences. Before posting the question, I read the source code of doT and saw what is happening under the hood. In the issue, I asked whether what I seek is possible and if not, whether there is interest in such a feature, volunteering to implement it if so. I did not insist on anything and do not understand the offensive tone in your reply.

My idea on how to implement this is as follows:

  • accessor string is . - change nothing
  • accessor string is different - alter the generated function by including search and replace code to transform it-prop1-prop1a to it.prop1.prop1a

That said, I am not going to tell you that I want to change JS syntax. Putting your asumptions away, you can re-read the issue again, consider what I wrote in this reply, and see for yourself.

My apologies if I made the tone that you felt being offended.

In the first place, you did not clarify what is the purpose for such a feature, which made me feel you did not read doT source code, and don't know how the interpreter should work at all. Again, the function {{= (...) }} current works that (...) is a valid and executable JS object, where it behaviour should never be altered. And yes, you were right, you did not say you want to change the syntax of JS, but your words was made me feel like this way (and that is based on anything wraps by {{= (...) }} must be a valid JS variable -- my real assumption). Your implementation change the functional response (replace - by ., which is not what it should do).

As for your point, basically your suggestion have to be fulfilled by adding another interpreter (e.g. {{- }}, {{$ }}, etc.) to avoid confusions or potentially breaking the current codes in user's project. Because anyone can name it as it-something-prop, it.something-prop, or it.something$prop, and even not leading by it (dynamically assigned). Putting that kind of feature in {{= }} interpreter is not "safe". However, I think you also agree that it is not belonging to doT.

The application you want to achieve is awesome, but as you said it is niche. doT have a "minimize" philosophy, and that is why it is simple and fast. For such kind of lib, I don't think it should do anything that is fancy. And any user can feel safely to use doT. For example, I built up my web app based on doT with Node/Express, and I put those fancy stuffs (e.g. markdown support) in my customized view engine. What I did is I preprocess my template string and do what I want before I send it to doT.

Again, if you feel being offended, I'm sorry.

Noes :) everything is alright and you are right that I could have been more specific in the issue. Thanks a lot for a thorough answer.

@mfandl, I am not sure if I understood your needs correctly, if so, I think preprocessing the template like this could help:

var template = `
##{$it-prop1-prop1a} {
   color: red;
}
##{$it-prop2-prop1a} {
color: blue;
}
##{$it-prop3-prop1a} {
color: green;
}`
template = template.split('{$it-')
template = template.join('{{=it.')
template = template.split('} ')
template = template.join('}} ')

The implementation is a bit naive, but could be a starting point.

@wagnerpv thanks :) I already solved the problem by preprocessing the template

The main reason why it’s not possible is that in {{= x }} “x” can be ANY valid JavaScript expression, so “-“ will be (and must be) interpreted as “minus” operator.