How to use variable to store elements

0 votes
asked Sep 23, 2021 in Question / help by Ben

I want to store an element in a variable for later use.

%set_variable_value($children,"VPCNATGateway(NAT,'NAT Gateway', 'VPC')")
However, when In use the variable I get a syntax error:
rectangle "==<color:#FF0000>Test</color>" as "alias"  <<sg>>{
        !$content=%string($children)
        VPCNATGateway(NAT,'NAT Gateway', 'VPC')
        $content
}
What is the correct way to do this?

1 Answer

0 votes
answered Sep 23, 2021 by The-Lu (64,760 points)

Hello B.,

Since we are not on a string context, we must prefer to use procedure for do that, like:

@startuml
!include <awslib/AWSCommon>
!include <awslib/NetworkingAndContentDelivery/VPCNATGateway>

!procedure $children()
  VPCNATGateway(NAT2,'NAT2 Gateway', 'VPC')
!endprocedure

rectangle "==<color:#FF0000>Test</color>" as "alias"  <<sg>>{
        VPCNATGateway(NAT,'NAT Gateway', 'VPC')
        $children()
}
@enduml

If that can help,
Regards,
Th.

commented Sep 24, 2021 by Martin (8,360 points)

You can achieve the same effect by setting the $children variable to the result of the macro rather than the macro text, by removing the double-quotes:

%set_variable_value($children, VPCNATGateway(NAT2,'NAT2 Gateway', 'VPC'))

click diagram for online server

commented Sep 25, 2021 by The-Lu (64,760 points)

Thanks @Martin,

You can also suppress the string conversion... like:

@startuml
!include <awslib/AWSCommon>
!include <awslib/NetworkingAndContentDelivery/VPCNATGateway>

%set_variable_value($children,VPCNATGateway(NAT2,'NAT2 Gateway', 'VPC'))

rectangle "==<color:#FF0000>Test</color>" as "alias"  <<sg>>{
        VPCNATGateway(NAT,'NAT Gateway', 'VPC')
        $children
}
@enduml

Enjoy...

commented Sep 30, 2021 by Martin (8,360 points)

PS I think Th's feature request for the following preprocessing improvement is relevant to link to here: https://forum.plantuml.net/14046/preprocessing-allow-the-preprocessor-run-more-than-one-time?show=14046#q14046

...