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

     1  {% include navigation.html %}
     2  {% raw %}
     3  # Literals protection
     4  
     5  ## E-Mail protection
     6  
     7  The razor convertor is designed to detect email address such as `john.doe@company.com` or `alert@127.0.0.1`.
     8  
     9  But it you type something like `{{ $.john.doe }}{{ $.company.com }}`, it will try to resolve variable john.doe and company.com.
    10  
    11  The result would be `<no value><no value>` unless you have defined:
    12  
    13  ```go
    14  {{- assertWarning (isNil $.john) "$.john has already been declared, use = to overwrite existing value" }}{{- set $ "john" (data "doe = 123.45") }}
    15  {{- assertWarning (isNil $.company) "$.company has already been declared, use = to overwrite existing value" }}{{- set $ "company" (data "com = {{ $.Math.Pi }}") }}
    16  ```
    17  
    18  In that case, the result of `{{ $.john.doe }}{{ $.company.com }}` will be `123.453.141592653589793`.
    19  
    20  ## "&#64;" protection
    21  
    22  You can also render the "&#64;" characters by writing &#64;&#64;.
    23  
    24  So this `@` will render &#64;.
    25  
    26  ## "&#123;&#123;" protection
    27  
    28  You can also render "&#123;&#123;" without being interpretated by go template using the following syntax `{{ "{{" }}`.
    29  
    30  So this `{{ "{{" }}` will render &#123;&#123;.
    31  
    32  ## Space management
    33  
    34  With go template, the way to indicate that previous or leading spaces between expression should be removed is expressed
    35  that way `{{- "expression" -}}`. The minus sign at the beginning and at the end mean that the spaces should be remove while
    36  `{{- "expression" }}` means to remove only at the beginning and `{{ "expression" -}}` means to remove only at the end.
    37  
    38  The `{{ "expression" }}` will keep the spaces before and after expression as they are.
    39  
    40  With razor, assignation will render go template code with - on left side.
    41  
    42  * `{{- assertWarning (isNil $.expr) "$.expr has already been declared, use = to overwrite existing value" }}{{- set $ "expr" "expression" }}` => `{{- set $ "expr" "expression" }}`
    43  * `{{- $expr := "expression" }}` => `{{- $expr := "expression" }}`
    44  
    45  But for variables and other expressions, you have to specify the expected behavior.
    46  
    47  | Razor expression | Go Template      | Note
    48  | ---------------- | -----------      | ----
    49  | `{{ $.expr }}`          | `{{ $.expr }}`   | No space eater
    50  | `{{- $.expr }}`         | `{{- $.expr }}`  | Left space eater
    51  | `{{ $.expr -}}`        | `{{ $.expr -}}`  | Right space eaters
    52  | `{{- $.expr -}}`        | `{{- $.expr -}}` | Left and right space eaters
    53  
    54  This signify that in the following sentence:
    55  
    56  ```text
    57      The word {{ $.expr }} will stay in the normal flow,
    58      but {{- $.expr }} will be struck on the previous word
    59  ```
    60  
    61  results in:
    62  
    63  ```text
    64      The word expression will stay in the normal flow,
    65      butexpression will be struck on the previous one
    66  ```
    67  
    68  You can also specify that the expression should be preceded by a new line:
    69  
    70  ```text
    71      The word {{- $.NEWLINE }}{{ $.expr }} will be on a new line
    72  ```
    73  
    74  results in:
    75  
    76  ```text
    77      The word
    78      expression will be on a new line
    79  ```
    80  
    81  ### Indent using current indentation
    82  
    83  This line will be rendered with 4 spaces before each word:
    84  
    85  ```go
    86  {{- $.NEWLINE }}{{- spaceIndent (`    `) (wrap 15 "This is a long line that should be rendered with a maximum 15 characters per line") }}
    87  ```
    88  
    89  results in :
    90  
    91  ```text
    92      This is a long
    93      line that should
    94      be rendered with
    95      a maximum 15
    96      characters per
    97      line
    98  ```
    99  
   100  While this line will be rendered with 4 spaces and a caret before each word:
   101  
   102  ```go
   103  list:
   104  {{- $.NEWLINE }}{{- spaceIndent (`  - `) (list "item 1" "item 2" "item 3") }}
   105  {{- $.NEWLINE }}{{- spaceIndent (`    - `) (list "sub 1" "sub 2" "sub 3") }}
   106  ```
   107  
   108  results in:
   109  
   110  ```text
   111    - item 1
   112    - item 2
   113    - item 3
   114      - sub 1
   115      - sub 2
   116      - sub 3
   117  ```
   118  
   119  While this line will be rendered with 4 spaces and `**` before each word:
   120  
   121  ```go
   122  {{- $.NEWLINE }}{{- spaceIndent (`    ** `) (list "item 1" "item 2" "item 3") }}
   123  ```
   124  
   125  results in:
   126  
   127  ```text
   128      ** item 1
   129      ** item 2
   130      ** item 3
   131  ```
   132  
   133  It is also possible to automatically wrap list elements with the surrounding context:
   134  
   135  ```go
   136  {{- $.NEWLINE }}{{ join "\n" (formatList "    => This is Item #[%v]!" (to 5)) }}
   137  ```
   138  
   139  results in:
   140  
   141  ```text
   142      => This is Item #[1]!
   143      => This is Item #[2]!
   144      => This is Item #[3]!
   145      => This is Item #[4]!
   146      => This is Item #[5]!
   147  ```
   148  
   149  {% endraw %}