Dust: The Difference Between Logical Partitions {?} And {#}

What is the difference between {?} And {#} ?

-

After a little test, listing all the truth / falsification values ​​for {?} And comparing them with {#} :

context

 { values: [ // false '', "", false, null, undefined, [], // true 0, "0", "null", "undefined", "false", {}, {a: 'a'} ] } 

template

 {#values} {?.}true{:else}false{/.} {/values} {~n} {#values} {#.}true{:else}false{/.} {/values} 

it outputs EXACTLY the same result:

 falsefalsefalsefalsefalsefalsetruetruetruetruetruetruetrue falsefalsefalsefalsefalsefalsetruetruetruetruetruetruetrue 

-

Is there any difference between the two?

+7
source share
1 answer

There is a difference between # and ? , although it is somewhat subtle and does not appear in your example.

? (exists): Checks the veracity of the given key. If the key is true, execute the body; otherwise, execute the body :else , if any.

# (section): Checks the veracity of this key. If the key is true, set the context in the key, and then execute the body. If the context is an array, execute the body once for each element of the array. If the key is not plausible, do not change contexts or execute the body :else if it exists.

So, if your template looked like this:

template:

 {?values} {?.}true{:else}false{/.} {/values} {~n} {#values} {#.}true{:else}false{/.} {/values} 

Then your result will be as follows:

 true falsefalsefalsefalsefalsefalsetruetruetruetruetruetruetrue 

The first line checks that values exist, but does not change the context. The second line checks for the current context (which in this case is the root context), and it prints true . Since ? does not go into the context and does not iterate over the array, true is printed only once.

+11
source share

All Articles