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

     1  {% include navigation.html %}
     2  {% raw %}
     3  
     4  # Assignation
     5  
     6  ## Global variables
     7  
     8  | Razor expression                            | Go Template                                              | Note
     9  | ----------------                            | -----------                                              | ----
    10  | `{{- assertWarning (isNil $.string) "$.string has already been declared, use = to overwrite existing value" }}{{- set $ "string" "string value" }}`                | `{{- set $ "string" "string value" }}`                   | Global assignation of string
    11  | `{{- assertWarning (isNil $.numeric1) "$.numeric1 has already been declared, use = to overwrite existing value" }}{{- set $ "numeric1" 10 }}`                          | `{{- set $ "numeric1" 10 }}`                             | Global assignation of integer
    12  | `{{- assertWarning (isNil $.numeric2) "$.numeric2 has already been declared, use = to overwrite existing value" }}{{- set $ "numeric2" 1.23 }}`                        | `{{- set $ "numeric2" 1.23 }}`                           | Global assignation of floating point
    13  | `{{- assertWarning (isNil $.numeric3) "$.numeric3 has already been declared, use = to overwrite existing value" }}{{- set $ "numeric3" 4E+4 }}`                        | `{{- set $ "numeric3" 4E+4 }}`                           | Global assignation of large scientific notation number
    14  | `{{- assertWarning (isNil $.numeric4) "$.numeric4 has already been declared, use = to overwrite existing value" }}{{- set $ "numeric4" 5E-3 }}`                        | `{{- set $ "numeric4" 5E-3 }}`                           | Global assignation of small scientific notation number
    15  | `{{- assertWarning (isNil $.hexa1) "$.hexa1 has already been declared, use = to overwrite existing value" }}{{- set $ "hexa1" 0x100 }}`                          | `{{- set $ "hexa1" 0x100 }}`                             | Global assignation of hexadecimal number
    16  | `{{- assertWarning (isNil $.result1) "$.result1 has already been declared, use = to overwrite existing value" }}{{- set $ "result1" (mul (add 2 3) 4) }}`                      | `{{- set $ "result1" (mul (add 2 3) 4) }}`               | Global assignation of mathematic expression
    17  | `{{- assertWarning (isNil $.result2) "$.result2 has already been declared, use = to overwrite existing value" }}{{- set $ "result2" ((String "hello world!").Title) }}` | `{{- set $ "result2" ((String "hello world!").Title) }}` | Global assignation of generic expression
    18  
    19  ## Local variables
    20  
    21  | Razor expression                             | Go Template                                        | Note
    22  | ----------------                             | -----------                                        | ----
    23  | `{{- $string := "string value" }}`                | `{{- $string := "string value" }}`                 | Local assignation of string
    24  | `{{- $numeric1 := 10 }}`                          | `{{- $numeric1 := 10 }}`                           | Local assignation of integer
    25  | `{{- $numeric2 := 1.23 }}`                        | `{{- $numeric2 := 1.23 }}`                         | Local assignation of floating point
    26  | `{{- $numeric3 := 4E+4 }}`                        | `{{- $numeric3 := 4E+4 }}`                         | Local assignation of large scientific number
    27  | `{{- $numeric4 := 5E-3 }}`                        | `{{- $numeric4 := 5E-3 }}`                         | Local assignation of small scientific number
    28  | `{{- $hexa1 := 0x100 }}`                          | `{{- $hexa1 := 0x100 }}`                           | Local assignation of hexadecimal number
    29  | `{{- $result1 := mul (add 2 3) 4 }}`                      | `{{- $result1 := mul (add 2 3) 4 }}`               | Local assignation of mathematic expression
    30  | `{{- $result2 := (String "hello world!").Title }}` | `{{- $result2 := (String "hello world!").Title }}` | Local assignation of generic expression
    31  
    32  ### Exception
    33  
    34  | Razor expression                                | Go Template                                        | Note
    35  | ----------------                                | -----------                                        | ----
    36  | `{{- $invalid := $.print }} "hello" "world" | upper`   | `{{- $invalid := print }} "hello" "world" | upper` | Using a mixup of go template expression and razor expression could lead to undesired result
    37  | `{{- $valid := print "hello" "world" | upper }}`     | `{{- $valid := print "hello" "world" | upper }}`   | Enclosing the whole assignation statement within {} ensures that the whole expression is assigned
    38  | `{{ $valid := print "hello" "world" | upper }}`    | `{{- $valid := print "hello" "world" | upper }}`   | Using that syntax give the exact same result
    39  
    40  ### Assignation within expression
    41  
    42  ```go
    43  {{- range $value := to 10 }}
    44      {{ $value }}
    45  {{- end }}
    46  ```
    47  
    48  ```go
    49  {{- range $index, $value := to 10 }}
    50      {{ $index }} = {{ mul $value 2 }}
    51  {{- end }}
    52  ```
    53  
    54  ```go
    55  {{- if $result := eq (add 2 2) 4 }}
    56      result = {{ $result }}
    57  {{- end }}
    58  ```
    59  
    60  ```go
    61  {{- with $value := add 2 2 }}
    62      value = {{ $value }}
    63  {{- end }}
    64  ```
    65  
    66  {% endraw %}