Skip to content

Where else does Dust search if the value is not defined? A.K.A. Dust Scoping

smfoote edited this page Apr 24, 2012 · 3 revisions

Dust Scoping

When working with complex JSON where keys are not necessarily defined, we might get surprised when dust.js finds values in other places. For example, if you have the JSON key "name" in multiple places, but misspell one to "nom" you might be surprised when dust.js walked up the JSON tree and found "name" in the parent or one of it immediate child.

Find here only

Example 1

{?name}
  {.} 
{/name} 

-- will display the value of name if it exists

Example 2

{#loop_name}
  {.name} 
{/loop_name} 

-- will display name inside loop_name

Example 3

{#loop_name}
  {apple.name}
{/loop_name}

 --  will display name defined in apple defined in loop_name 

###Find here and in all my parents

Example

 {#loop_name}
    {name}
  {/loop_name} 

 --  will display: name inside loop_name OR the first name it finds in its parents!

Find here and in my parents up until X

Example 1

 {#D:B}
  {name} 
 {/D} 
 --  will display name inside D OR name inside C OR name inside B

Example 2

  {#D:D}
    {name}
  {/D}
 
  is same as doing

   {#D:D}
    {.name} 
   {/D} 

Find here and in this section

Example 1

    {#C}
     {#D:.}
      {name}
     {/D}
    {/C} 

--  will display name inside D OR name inside C!

Example 2

  {#C}
    {#D:.}
     {name}
    {/D}
  {/C} 

 is same as 
  {#C}
     {#D:C}
      {name}
    {/D}
  {/C} 

Example 3

  {#C}
   {#D:.D2}
    {name}
   {/D}
  {/C} 
   --  will display name inside D OR name inside C.D2!

Some more details

If the variable is not defined here, lets look at the parents

: For normal (absolute / no dots) dust variables and unrestricted (no specified context) section loops

E.g

     {#loop_name}
        {name} without dots
     {/loop_name}

then variables are evaluated by:

  • immediate children of the current context OR
  • the immediate children of the parent context (looping until it hits the root)

If the variable is not defined here, stop looking... oh by the way look at these grand children.

: Variables can be "paths" (relative to the current context e.g. loop_name)

E.g

    {#loop_name}
     {.name}with dots, {.} just dot or {drill.down.name}
    {/loop_name}  

then variables are evaluated by:

  • immediate children of the current context where dots drill DOWN the current scope

If the variable is not defined here, lets look at the parents ...but only to a specified parent

: You can add a specific context (Absolute scope version) to a section loop (e.g. {#loop_name:absolute_scope} with colon no dots in the specified context {.} {name} {/loop_name} then variables are evaluated by:

  • immediate children of the current context OR
  • the immediate children of the parent context (looping until the restricted scope specified by loop_name)

**Note: absolute context is limited to Parents and immediate children of parents. You can limit context to parents, grand parents, great grand uncles but not siblings, cousins.

If the variable is not defined here, let look at this specified parent

: You can add a specific context to fallback to (Relative to the scope of the section loop) E.g

  {#parent_loop}
   {#loop_name:relative.to.parent_loop}
     with colon with dots in the specified context {.} {name} 
   {/loop_name}
  {/parent_loop}

then variables are evaluated by:

  • immediate children of the current context OR
  • the immediate children of the specified context (No looping)