dmulyalin / ttp

Template Text Parser
MIT License
350 stars 34 forks source link

In this case, I want to call the previous variable after it has been recorded. Continue to be called when a new variable is logged. Until no variables are recorded. How do I write templates #114

Open bobo-xiaopeng opened 11 months ago

bobo-xiaopeng commented 11 months ago

RECORD {{ name | record(var_name) }}

var_name (mandatory) - template variable name that should be used to record match result Record match results in template variable with given name. That recorded variable can be referenced within other functions such as set or retrieved from ttp dictionary within macro.

Variables are recorded in two scopes: Per-Input scope - all groups that parse this particular input will have access to recorded variable; variable stored in ttp["parser_object"].vars dictionary Global scope - variable available from any group at any template; variable stored in ttp["global_vars"] dictionary Warning

record results override one another, meaning if several match variable record result in same template variable, match variable that was matched later will override previous match result.

Example

Template:

myswitch1#show run int interface Vlan778 ip vrf forwarding VRF_NAME_1 ip address 2002:fd37::91/124 !

myswitch2#show run int interface Vlan779 description some description input2 ! interface Vlan780 switchport port-security mac 4 !

interface {{ interface }} ip address {{ ip }}/{{ mask }} ip vrf forwarding {{ vrf | record("VRF") }} switchport port-security mac {{ sec_mac }} interface {{ interface }} description {{ description | ORPHRASE | record("my_description") }} switchport port-security mac {{ sec_mac }} {{ my_vrf | set("VRF") }} {{ my_descript | set("my_description") }}

Result:

[ [ { "interfaces": { "interface": "Vlan778", "ip": "2002:fd37::91", "mask": "124", "vrf": "VRF_NAME_1" } }, { "interfaces": [ { "description": "some description input2", "interface": "Vlan779", "my_descript": "some description input2", "my_vrf": "VRF_NAME_1" }, { "interface": "Vlan780", "my_descript": "some description input2", "my_vrf": "VRF_NAME_1", "sec_mac": "4" } ] } ] ] In above example {{ my_vrf | set("VRF") }} uses "VRF" variable from Global scope, while {{ my_descript | set("my_description") }} retrieves "my_description" variable value from per-input scope.

WARNING!!!!!! record results override one another, meaning if several match variable record result in same template variable, match variable that was matched later will override previous match result.

In this case, I want to call the previous variable after it has been recorded. Continue to be called when a new variable is logged. Until no variables are recorded. How do I write templates

bobo-xiaopeng commented 11 months ago

` {{start}} IPv4-family for VPN instance: {{vrf|record("VRF_parent")}}

      {{vrf|set("VRF_parent")}}####In this template, I want to call the variables previously recorded by vrf at this point, but due to the nature of variables overwriting each other. I can only call the last variable that matches.
     BGP Peer is {{peer|ORPHRASE}},  remote AS {{AS}}
     BGP current state: {{bgp_state}}
     BGP current state: {{bgp_state}}, Up for {{up_time|default('N/A')}}
     Received total routes: {{RCV}}
     Advertised total routes: {{ADV}}

Import route policy is: {{In_policy|default('N/A')}} Export route policy is: {{Out_policy|default('N/A')}} Peer soo is: {{Soo}} {{end}} `

dmulyalin commented 10 months ago

Hello, you might want to use group record function instead - https://ttp.readthedocs.io/en/latest/Groups/Functions.html#record