Skip to content

Enhancement proposal: destructuring args declaration #275

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

Closed
wants to merge 1 commit into from

Conversation

complyue
Copy link

I find doT quite elegant and lean 👍, lovin it.

And I propose the enhancement conveyed in this PR, it seems to be competing with #266 (and #29 for the non-nesting case) to address the wondering raised by #214 (with my +1 to it).

This PR introduces a new args templateSettitng, which defines a delimiter ({{@ }} by default) allowed at the very beginning of a template, to declare interesting variable names out of the single it object, then with such a declaration present, the mentioned variables are used without prefix throughout the template. Note that with presence of such a declaration, templateSettings.varname will be ignored in processing of the template instance, thus it or another name will NOT be available to the template. Templates without the {{@ }} declaration will maintain old behaviour intact so it'll be automatically backward compatible.

I think the test demonstrates it well enough:

		test([
			'{{@one,two}}{{=one}}{{=two}}',
			'{{@one,two}}{{= one}}{{= two}}',
			'{{@one,two}}{{= one }}{{= two }}'
		], {one:1, two: 2}, '12');
		test([
			'{{@ one, two=2 }}{{=one}}{{=two}}',
		], {one:1}, '12');

The implementation leverages es6 destructuring to selectively accept arguments from it (with default value spec, feel like an extra bonus 😃), I feel it sensible at language level, and shouldn't incur unreasonable performance penalty tho I'm not experienced enough with doT to play with the benchmarks.

@epoberezkin @olado @1mike12

@coveralls
Copy link

coveralls commented Jun 19, 2018

Coverage Status

Coverage increased (+0.2%) to 98.305% when pulling c9a8d6f on complyue:destruct-args into 299b4da on olado:master.

@epoberezkin
Copy link
Collaborator

@complyue thank you for the PR.

The main idea of doT is to allow any JS in the template so what you propose can already be achieved with {{ const {one, two} = it; }}, provided your current JS environment supports destructuring, otherwise you can achieve the same with two assignments.

So it doesn’t really seem necessary to have any special syntax for what’s already possible.

@complyue
Copy link
Author

@epoberezkin you're right. but I'd think this can make possible for the unnecessary variable it from appearing, so the templating paradigm can be a bit more declarative than imperative.

I'm currently using doT to update many small pieces of html contents, such as href of <a/>, text of short <span/> etc. and no data is being iterated, so I'd prefer data over it. but writing http://xxx.info/{{=data.symbol}} feels some how like embedding assembly code. tho I admit {{@symbol}}http://xxx.info/{{=symbol}} feels rather overkilling. but {{const {symbol} = data; }}http://xxx.info/{{=symbol}} feels much worse.

may be I can finally train my brain to recognise it. without other sensation.

doT is amazing any way, thanks for the perfect work!

@epoberezkin
Copy link
Collaborator

So, possibly it could be just an option passed to doT.template to define property names that will be included as variables. Using template instruction seems messy, as they can be in the middle, repeated etc.

@epoberezkin epoberezkin reopened this Jul 26, 2020
@epoberezkin
Copy link
Collaborator

in v2 as argName: string[] option

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

Successfully merging this pull request may close these issues.

3 participants