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

     1  {% include navigation.html %}
     2  {% raw %}
     3  
     4  # Assignation
     5  
     6  ## Global variables
     7  
     8  | Razor expression                            | Go Template                                              | Note
     9  | ----------------                            | -----------                                              | ----
    10  | `@string := "string value";`                | `{{- set $ "string" "string value" }}`                   | Global assignation of string
    11  | `@numeric1 := 10;`                          | `{{- set $ "numeric1" 10 }}`                             | Global assignation of integer
    12  | `@numeric2 := 1.23;`                        | `{{- set $ "numeric2" 1.23 }}`                           | Global assignation of floating point
    13  | `@numeric3 := 4E+4;`                        | `{{- set $ "numeric3" 4E+4 }}`                           | Global assignation of large scientific notation number
    14  | `@numeric4 := 5E-3;`                        | `{{- set $ "numeric4" 5E-3 }}`                           | Global assignation of small scientific notation number
    15  | `@hexa1 := 0x100;`                          | `{{- set $ "hexa1" 0x100 }}`                             | Global assignation of hexadecimal number
    16  | `@result1 := (2+3)*4;`                      | `{{- set $ "result1" (mul (add 2 3) 4) }}`               | Global assignation of mathematic expression
    17  | `@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 := (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  @-foreach ($value := to(10))
    44      @{value}
    45  @-end foreach
    46  ```
    47  
    48  ```go
    49  @-foreach ($index, $value := to(10))
    50      @{index} = @($value * 2)
    51  @-end foreach
    52  ```
    53  
    54  ```go
    55  @-if ($result := 2+2 == 4)
    56      result = @{result}
    57  @-end if
    58  ```
    59  
    60  ```go
    61  @-with ($value := 2+2)
    62      value = @{value}
    63  @-end with
    64  ```
    65  
    66  {% endraw %}