How to add a piece of code to another one?

Hello everyone! I’d like to add this piece of code into a container:

$Subtitle=$ChildCount

But, as you can see, this latter has already some code inside and when I add this new piece of code, it does not run. How could I do that? Thank you!

You have:

$Color=; 
if ($Type=-"Livre") {$Color="poppy"} ; 
if ($Type=="Article") {$Color="bright blue"};
if ($Type= "Communication") {$Color="green"};
if ($Type=="Remarque"){$Color="bright red"}; 
if ($Type=="Lecture"){$Color="blue"};

and you want to add to the rule:

$Subtitle=$ChildCount

which gives:

$Color=; 
if ($Type=-"Livre") {$Color="poppy"} ; 
if ($Type=="Article") {$Color="bright blue"};
if ($Type= "Communication") {$Color="green"};
if ($Type=="Remarque"){$Color="bright red"}; 
if ($Type=="Lecture"){$Color="blue"};
$Subtitle=$ChildCount;

When I test that, the note with the rule shows the correct child count in the subtitle.

I do notice that the extra code you listed had no semi-colon at the end of the line. If added at the end it doesn’t matter as it is the last expression. But, if added at the start of the rule:

$Subtitle=$ChildCount
$Color=;
// ...etc

That will fail as a closing semicolon is needed:

$Subtitle=$ChildCount;
$Color=;
// ...etc.

Otherwise is all seems to work OK. Does that help?

1 Like

OK! That’s exactly what I was looking for. I didn’t know that process. Thank you Mark!

1 Like

To explain, your rule with the new code is one rule that uses 7 discrete actions. Line breaks in the code—like I used above for layout clarity have no meaning for action code. It uses the semi-colon to work out where each discrete action ‘expression’ is within the overall rule.

The semicolon is optional for a single expression:

$Color="red"

but if we add an extra expression, we also need a semicolon:

$Color="red";$MyString="hello world"

Note the second expression—the last one in the rule doesn’t need a semicolon.

I generally always add a semicolon at the end of any expression as the I never trip up on this.

The code in your if()statements, inside the { } is treated the same way. You code that doesn’t have a semi-colon as the is onlt one expression before the closing }. You do correctly have a semi-colon after the } as that is the end of the if(){} complete expression.

… and excuse me not to have paste the code you kindly reproduced!

1 Like

(Note typo just before “Livre”, though that’s not my topic))

Another way to do this would be to use a dictionary.

  1. We make a note /config/colors
  2. Set its $MyDictionary to {“Live”:poppy; “Article”:“bright blue”; “Communication”:“green”…}
  3. You rule now becomes simpler:
$Color=$MyDictionary(/config/colors)["Type"];
if ($Color=="") {$Color=;}

That is, we set the color from the dictionary, but if the dictionary doesn’t have our type, we use the default color.

1 Like

Sorry, I missed those typos earlier but for later readers corrected code is as below.

$Color=; 
if ($Type=="Livre"){$Color="poppy";} ; 
if ($Type=="Article"){$Color="bright blue";};
if ($Type=="Communication"){$Color="green";};
if ($Type=="Remarque"){$Color="bright red";}; 
if ($Type=="Lecture"){$Color="blue";};
$Subtitle=$ChildCount;

Two of the if condition queries were not using == for an equality check though Tinderbox seems to be figuring you the user’s intent. Further to discussion above I’ve put semicolons at the end of all expressions . That said, those for the (last) expression inside { } might be considered overkill. Within the requirements feel free to leave out unneeded semicolons. If unsure where that is just put them all in and there will be no surprises.

All that said, I think @eastgate’s alternative solution is much better for the colour setting. Having a large number of sequential if() statements targetting the same attribute(s) is a hint that some improvement is likely possible.

Edit: white space between the closing) and opening { of an if(){} command is allowed (Tinderbox ignores it), but I think it makes the code’s intent clearer if such space is omitted.

1 Like