github.com/coveo/gotemplate@v2.7.7+incompatible/docs/doc_test/collections.md (about)

     1  {% include navigation.html %}
     2  {% raw %}
     3  
     4  # Data collections (lists/slices and dicts/maps)
     5  
     6  ## Maps
     7  
     8  | Razor                                           | Gotemplate                                          | Note
     9  | ---                                             | ---                                                 | ---
    10  | `@razorDict := dict("test", 1, "test2", 2);`    | `{{- set $ "goDict" (dict "test" 1 "test2" 2) }}`   | Creation
    11  | `@razorDict2 := dict("test3", 3, "test5", 5);`  | `{{- set $ "goDict2" (dict "test3" 3 "test5" 5) }}` | Creation
    12  | `@set(.razorDict, "test2", 3);`                 | `{{- set .goDict "test2" 3 }}`                      | Update
    13  | `@set(.razorDict, "test3", 4);`                 | `{{- set .goDict "test3" 4 }}`                      | Update
    14  | `@razorDict = merge(razorDict, razorDict2);`    | `{{- set $ "goDict" (merge .goDict .goDict2) }}`    | Merge (First dict has priority)
    15  | `@razorDict.test3;`                             | `{{ $.goDict.test3 }}`                              | Should be `4`
    16  | `@razorDict.test5;`                             | `{{ $.goDict.test5 }}`                              | Should be `5`
    17  | `@razorDict["test", "test3", "undef"]`          | `{{ extract $.goDict "test" "test3" "undef" }}`     | Extract values, should be `[1,4,null]`
    18  | `@razorDict["test":"test9"]`                    | `{{ slice $.goDict "test" "test9" }}`               | Slice values, should be `[1,3,4,5]`
    19  | `@razorDict["test9":"test"]`                    | `{{ slice $.goDict "test9" "test" }}`               | Slice values, should be `[5,4,3,1]`
    20  | `@keys(razorDict)`                              | `{{ keys $.goDict }}`                               | Get keys, should be `["test","test2","test3","test5"]`
    21  | `@values(razorDict)`                            | `{{ values $.goDict }}`                             | Get values, should be `[1,3,4,5]`
    22  
    23  ### Looping
    24  
    25  #### Razor
    26  
    27  ```go
    28  @-foreach($key, $value := razorDict)
    29      @{key}, @{value}, @get($.razorDict, $key)
    30  @-end foreach
    31  ```
    32  
    33  #### Gotemplate
    34  
    35  ```go
    36  {{- range $key, $value := .goDict }}
    37      {{ $key }}, {{ $value }}, {{ get $.goDict $key }}
    38  {{- end }}
    39  ```
    40  
    41  #### Result
    42  
    43  ```go
    44      test, 1, 1
    45      test2, 3, 3
    46      test3, 4, 4
    47      test5, 5, 5
    48  ```
    49  
    50  ## Slices
    51  
    52  | Razor                                            | Gotemplate                                             | Note
    53  | ---                                              | ---                                                    | ---
    54  | `@razorList := list("test1", "test2", "test3");` | `{{- set $ "goList" (list "test1" "test2" "test3") }}` | Creation
    55  | `@razorList = append(razorList, "test4");`       | `{{- set $ "goList" (append .goList "test4") }}`       | Append
    56  | `@razorList = prepend(razorList, "test0");`      | `{{- set $ "goList" (prepend .goList "test0") }}`      | Prepend
    57  | `@razorList;`                                    | `{{ $.goList }}`                                       | Should be `["test0","test1","test2","test3","test4"]`
    58  | `@contains(razorList, "test1", "test2");`        | `{{- contains .goList "test1" "test2" }}`              | Check if element is in list
    59  | `@has("test1", razorList);`                      | `{{- has "test1" .goList }}`                           | has is an alias to contains
    60  | `@has(razorList, "test1");`                      | `{{- has .goList "test1" }}`                           | has Support inversion of argument if the first one is not a list
    61  | `@has(razorList, "test1", "test2");`             | `{{- has .goList "test1" "test2" }}`                   | has can also test for many elements
    62  | `@razorList.Contains("test1", "test2");`         | `{{ .goList.Contains "test1" "test2" }}`               | List also support using methods
    63  | `@razorList.Reverse();`                          | `{{ .goList.Reverse }}`                                | Should be `["test4","test3","test2","test1","test0"]`
    64  
    65  ### Looping
    66  
    67  #### Razor
    68  
    69  ```go
    70  @-foreach($index, $value := razorList)
    71      @{index}, @{value}, @extract($.razorList, $index)
    72  @-end foreach
    73  ```
    74  
    75  #### Gotemplate
    76  
    77  ```go
    78  {{- range $index, $value := .goList }}
    79      {{ $index }}, {{ $value }}, {{ extract $.goList $index }}
    80  {{- end }}
    81  ```
    82  
    83  #### Result
    84  
    85  ```go
    86      0, test0, test0
    87      1, test1, test1
    88      2, test2, test2
    89      3, test3, test3
    90      4, test4, test4
    91  ```
    92  
    93  {% endraw %}