This training, Tinderbox Training Video 49 - Demystifying Action Code Updating a Note's Display Expression, provides a simple example of action code that counts the children of a note and displays the results in the notes $DisplayExpression attribute.
$DisplayExpression is a really cool attribute.
Normally, when you are visually looking at a note in any of Tinderbox’s views you see the value of the $Name attribute, i.e. the name of the note. However, if you put a value in the $DisplayExpression attribute Tinderbox will show that value in the views rather than the $Name value. The ability to visually see more information in the views without changing the actual name of the note is extremely powerful from a metta cognition and data analysis perspective. This gives you the ability to generate visual semantic value while retaining the purity of your note name.
TIP: Use a user generated $DisplayExpressionCache attribute
If you’re going to use a lot of display expressions, especially in a file with thousands of notes, I recommend that you create a $DisplayExpressionCache variable. This is an important step to optimize the performance of your Tinderbox file.
Why? Every time Tinderbox draws a view it will run the associated action code assigned to each note. So, if you put your action code directly into the $DisplayExpression attribute of a note every time Tinderbox refreshes its view it will run the code, which is unnecessary as the name of your notes won’t change that often. It is more efficient for Tinderbox to run this action code as an Edict. Have the Edict do the work and update a $DisplayExpressionCache attribute. Have the $DisplayExpression attribute value equal to $DisplayExpressionCache. By doing this, when Tinderbox goes to refresh a view it is not executing action code and taking up precious processing power each time it refreshes a note, rather it is simply pulling in a value.
Here is the code:
$MyNumber=collect_if(descendants,$Prototype!="pFolder",$Name).count;$DisplayExpressionCache=$Name+" ("+$MyNumber+")";
In English, what we are saying with this code is “Collect and count only the descendants of a note that are NOT assigned the pFolder prototype and put the result in $MyNumber, then, set the value of $DisplayExpressioCache to the $Name of the current note plus the value of $MyNumber surrounded by parentheses.”
If you just want to count the children and don’t want to count the children’s children, i.e. descendants, or care about ignoring child containers, you could use this:
$MyNumber=collect(children,$Name).count;$DisplayExpressionCache=$Name+" ("+$MyNumber+")";
In English, what we are saying with this code is “Collect and count the children of a note and put the result in $MyNumber. Then, set the value of $DisplayExpressioCache to the $Name of the current note plus the value of $MyNumber surrounded by parentheses.”
Explanation of the Syntax
- != means “not equal to”
- ; separates the two action code expressions
- .count is the dot operator that counts the result of the operator collect_if
- = is an assignment instruction, apply the value of the right side of the equation to the attribute specified on the left side of the equation
- , delineates the arguments collect_if operator