Skip to content

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

jimmyhchan edited this page Jul 5, 2012 · 3 revisions

Dust Scoping

When working with complex JSON where keys are not necessarily defined, we might get surprised when Dust 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 walked up the JSON tree and found "name" in the parent or one of it immediate child.

###Find here and in all my parents until the top/root of the JSON

Example

  {#projects}
    {name}
  {/projects} 

 --  will display: name inside projects OR the first name it finds in its parent JSON tree of projects

Dust references are evaluated by:

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

Find here only ( force local scope )


Example 1

{?name}
  {.} 
{/name} 

-- will display the value of name if it exists, if not gives up. The dot operator forces Dust to search in the scope of name only

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

Example 2

{#projects}
  {.name} 
{/projects} 

is same as

 {#projects:projects}
   {name} 
 {/projects}

-- will display name inside projects if it exists, if not gives up. The dot operator forces Dust to search for the "name" in the scope of projects only

Example 3

{#projects}
  {sprint.name}
{/projects}

 --  will display name defined in sprint defined in projects 

Find here and in my parents up until X (VB: verify )


Example 1

 {#projects:team}
  {name} 
 {/projects} 
 --  will display name inside projects OR name inside team. 
 --  projects take precedence over team.

Example 2

As discussed above,

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

   {#projects:projects}
    {.name} 
   {/projects} 

Find here and in this section X


Example 1

    {#groups}
     {#projects:.}
      {name}
     {/projects}
    {/groups} 

--  will display name inside projects OR name inside groups!

Example 2

  {#groups}
    {#projects:.}
     {name}
    {/projets}
  {/groups} 

 is same as 
  {#groups}
     {#projects:groups}
      {name}
    {/projects}
  {/groups} 

Example 3

  {#groups}
   {#projects:.team}
    {name}
   {/projects}
  {/groups} 
   --  will display name inside projects OR name inside groups.team!

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. ( VB : verify )

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)