github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/tests/functests/c0039.yml (about)

     1  doc_meta: |
     2    folder: template
     3    title: advanced usage
     4    head: |
     5      Demo usecases of builtin with, block, definition
     6  
     7      Also demo the dynamic templating using expand so that you can compose multiple templates
     8  
     9    sections:
    10      - title: Demo
    11        log: yes
    12  
    13  notes:
    14    goal:
    15      - showcase template usage of "with"
    16      - showcase template block
    17      - showcase template definition
    18      - showcase dynamic template using expand so that you can compose multiple templates
    19  
    20    bug:
    21      - due to the viper's lower case all var keys, please use lower case as reference
    22  
    23    output: |
    24      dvar> usage_of_with:
    25      "  student name: Tom\n  school name: Sydney Grammar\n  Male\n  "
    26  
    27      dvar> usage_of_with_else:
    28      "  i am somebody  "
    29  
    30      dvar> template_def_plain:
    31      "\n\n\nONE TWO\"\n"
    32  
    33      dvar> template_def_consume:
    34      "\nshow example of how you can define reusable template\nand how you can pass in the parameters\n\n\n\n\nstudent:\n  name: ONE: Tom\n  gender: TWO: Male\n"
    35  
    36      dvar> template_def_consume_dynamic:
    37      "\n\n\n\nstudent:\n  name: ONE: Tom\n  gender: TWO: Male\n\n"
    38  
    39      dvar> template_use_block:
    40      "\n\nstudent:\n  name: ONE: Tom\ngender: TWO: Male\n\n"
    41  
    42        executing shell commands
    43      cmd( 1):
    44        echo "value\n {{.usage_of_with}}"
    45        value
    46      student name: Tom
    47      school name: Sydney Grammar
    48        Male
    49      cmd( 2):
    50        echo "{{.usage_of_template}}"
    51        None
    52      cmd( 3):
    53        echo "{{.template_def_consume}}"
    54        show example of how you can define reusable template
    55        and how you can pass in the parameters
    56  
    57      student:
    58        name: ONE: Tom
    59        gender: TWO: Male
    60      cmd( 4):
    61        echo "{{.template_def_consume_dynamic_t1}}"
    62        {{define T1}}ONE: {{.}}{{end}}
    63      cmd( 5):
    64        echo "{{.template_def_consume_dynamic_t2}}"
    65        {{define T2}}TWO: {{.}}{{end}}
    66      cmd( 6):
    67        echo "{{.template_def_consume_dynamic_t3}}"
    68        {{define T3}}student:
    69      name: {{template T1 .name}}
    70      gender: {{template T2 .gender}}{{end}}
    71        {{template T3 .student}}
    72      cmd( 7):
    73        echo "{{.template_def_consume_dynamic}}"
    74      student:
    75        name: ONE: Tom
    76        gender: TWO: Male
    77      cmd( 8):
    78        echo "{{.template_use_block}}"
    79      student:
    80        name: ONE: Tom
    81      gender: TWO: Male
    82  
    83  
    84  vars:
    85    ns: prod
    86    pod_name: web_app
    87    ha: true
    88    age: 34
    89    old: 54
    90    nobody:
    91    admins: [tom, jason, alice]
    92    managers:
    93      - tom
    94      - jason
    95      - alice
    96    student:
    97      name: Tom
    98      gender: Male
    99      teachers:
   100        - tom
   101        - jason
   102        - alice
   103      address:
   104        suburb:
   105          name: sydney
   106          postcode: 2000
   107          CBD: yes
   108        school: Sydney Grammar
   109  
   110  #due to the viper's lower case all var keys, please use lower case as reference
   111    template_def_consume_dynamic_T1: |
   112          {{define "T1"}}ONE: {{.}}{{end}}
   113  
   114    template_def_consume_dynamic_T2: '{{define "T2"}}TWO: {{.}}{{end}}'
   115  
   116    template_def_consume_dynamic_T3: |
   117      {{define "T3"}}student:
   118        name: {{template "T1" .name}}
   119        gender: {{template "T2" .gender}}{{end}}
   120      {{template "T3" .student}}
   121  
   122  
   123    template_def_consume_dynamic_main: |
   124      {{.template_def_consume_dynamic_t1}}
   125      {{.template_def_consume_dynamic_t2}}
   126      {{.template_def_consume_dynamic_t3}}
   127      {{template "T3" .student}}
   128  
   129  
   130  dvars:
   131  
   132    - name: usage_of_with
   133      value: '
   134      {{ with .student }}
   135        student name: {{ println .name}}
   136        {{ with .address}}
   137          school name: {{ println .school}}
   138        {{ end}}
   139        {{ println .gender}}
   140      {{ end}}
   141      '
   142      flags: [vvvv,]
   143  
   144    - name: usage_of_with_else
   145      value: '
   146      {{ with .nobody }}
   147      i am nobody
   148      {{else}}
   149      i am somebody
   150      {{ end}}
   151      '
   152      flags: [vvvv,]
   153  
   154    - name: template_def_plain
   155      value: |
   156        {{define "T1"}}ONE{{end}}
   157        {{define "T2"}}TWO{{end}}
   158        {{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
   159        {{template "T3"}}"
   160      flags: [vvvv,]
   161  
   162  
   163    - name: template_def_consume
   164      value: |
   165  
   166        show example of how you can define reusable template
   167        and how you can pass in the parameters
   168  
   169        {{define "T1"}}ONE: {{.}}{{end}}
   170        {{define "T2"}}TWO: {{.}}{{end}}
   171        {{define "T3"}}student:
   172          name: {{template "T1" .name}}
   173          gender: {{template "T2" .gender}}{{end}}
   174        {{template "T3" .student}}
   175      flags: [vvvv,]
   176  
   177  #  NOTE: this will not work, as the value itself serve as plain text as template rather than
   178  #  dynamic rendering, so this reusable definition of template should go to vars section
   179  #-------------------------------------------------------
   180  #  - name: template_def_consume_dynamic_T1
   181  #    value: |
   182  #      {{define "T1"}}ONE: {{.}}{{end}}
   183  #    flags: [vvvv,]
   184  #
   185  #  - name: template_def_consume_dynamic_T2
   186  #    value: |
   187  #      {{define "T1"}}ONE: {{.}}{{end}}
   188  #    flags: [vvvv,]
   189  #
   190  #  - name: template_def_consume_dynamic_T3
   191  #    value: |
   192  #      {{.template_def_consume_dynamic_T1}}
   193  #      {{.template_def_consume_dynamic_T2}}
   194  #      {{define "T3"}}student:
   195  #        name: {{template "T1" .name}}
   196  #        gender: {{template "T2" .gender}}{{end}}
   197  #      {{template "T3" .student}}
   198  #    flags: [vvvv,]
   199  #-------------------------------------------------------
   200  
   201    - name: template_def_consume_dynamic
   202      value: |
   203        {{.template_def_consume_dynamic_main}}
   204      flags: [vvvv,]
   205      expand: 3
   206  
   207    - name: template_use_block
   208      desc: |
   209        A block is shorthand for defining a template
   210          {{define "name"}} T1 {{end}}
   211        and then executing it in place
   212          {{template "name" pipeline}}
   213        The typical use is to define a set of root templates that are
   214        then customized by redefining the block templates within.
   215  
   216        In this case the define of T3 and execution using template
   217        are merged into one block statement
   218      value: |
   219        {{define "T1"}}ONE: {{.}}{{end}}
   220        {{define "T2"}}TWO: {{.}}{{end}}
   221        {{block "T3" .student}}student:
   222          name: {{template "T1" .name}}
   223        gender: {{template "T2" .gender}}
   224        {{end}}
   225      flags: [vvvv,]
   226  
   227  tasks:
   228    -
   229      name: task
   230      task:
   231        -
   232          func: shell
   233          do:
   234            - echo "value\n {{.usage_of_with}}"
   235            - echo "{{.usage_of_template}}"
   236            - echo "{{.template_def_consume}}"
   237            - echo "{{.template_def_consume_dynamic_t1}}"
   238            - echo "{{.template_def_consume_dynamic_t2}}"
   239            - echo "{{.template_def_consume_dynamic_t3}}"
   240            - echo "{{.template_def_consume_dynamic}}"
   241            - echo "{{.template_use_block}}"