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

can.sub with strict properties mode #349

Closed
schovi opened this issue Apr 5, 2013 · 4 comments
Closed

can.sub with strict properties mode #349

schovi opened this issue Apr 5, 2013 · 4 comments

Comments

@schovi
Copy link
Contributor

schovi commented Apr 5, 2013

Now when any of values is missing the can.sub returns null
I understand to needs of this in can.control and templated bindings.

I came with idea, how to improve can.sub as templating system.
And I call it strict mode and can be controlled with fourth param in can.sub function

sub: function( str, data, remove, strict ) { ... }

When true is passed it returns null when any value is missing.
When false is passed it returns new template where missing values are kept as template (meta-template).

There is example of usage strict param:

// Without strict
can.sub("a:{a} b:{b}", {b:"hello"}) // -> "a: b:hello"

// Strict to true (third param is for remove value)
can.sub("a:{a} b:{b}", {b:"hello"}, false, true) // -> null

// Strict to false
can.sub("a:{a} b:{b}", {b:"hello"}, false, false) // -> "a:{a} b:hello"

There is initial implementation https://github.com/schovi/canjs/compare/can-sub-strict

@justinbmeyer
Copy link
Contributor

That's not how much templating systems work. For example: <%= b %> and {{b}} would not leave the template tags in place if b does not exist. Is this really what you need, or do you need a way to pass the equivalent of EJS's <%%= %>?

@schovi
Copy link
Contributor Author

schovi commented Apr 6, 2013

@justinbmeyer The idea came from my last project.

Feature i need most is to keep result even there are missing values ("a:{a}" -> "a:"). Same as ejs or mustache work <span>{{a}}</span> will render <span></span> (if i skip fact that undefined or null will be there).

And in one case i found very useful what I called meta-templates. In one place i prerender template with few values and then reuse this meta-template. I know that i can achieve same result with keeping object and extend it with new values, but this seem more straightforward for me.

First time, when i tried to use can.sub and i get null as result it took me lot of time to do debug that and then i found it is "feature" and i ask myself "Who the hell use this." :) Then i found it is primary for can.control templated bindings. So I tried to make API for this as easy as possible and understandable. Plus the code for this feature is not aggresive.

@schovi
Copy link
Contributor Author

schovi commented Apr 12, 2013

@justinbmeyer Any suggestions?

I worked on it little more and found, that variant with strict = false is not that sexy as I thought. You can get same result with keeping object and extending it.
But otherwise still can imagine usecase, where first time i want to pass some params and i dont care if they are rendered or not. And later i will render that meta-template with rest params and strict = true

Without this feature do you think the strict mode is fine?
Or it will be better to keep same api as now (without any options and when any value is missing then null is returned). And strict option can be changed with something like keepResult and it will ignore missing values.

can.sub("a:{a}", {}) // -> null
// with keepResult
can.sub("a:{a}", {}, false, true) // -> "a:"

@justinbmeyer
Copy link
Contributor

@schovi Thanks for submitting this issue. However, I don't think it's something most people need, and its not something the framework needs. can.sub is just a means to an end. Hopefully you can get this in lodash or some other micro-templating library. Thanks!

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