You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/versions/v3/guides/configurations.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,7 @@ If you are using VSCode and change the `TemplateExt` setting to anything other t
58
58
59
59
## Global Data
60
60
61
-
Global data allows you to share values from your Go code across all Textwire templates. This is useful for environment variables, authenticated user data, and similar global information. Use the `GlobalData` configuration for this purpose. Here is an example:
61
+
Global data allows you to share values from your Go code across all Textwire templates. This is useful for environment variables, authenticated user data, and similar global information. Use the `GlobalData` configuration for this purpose. Example:
Copy file name to clipboardExpand all lines: docs/versions/v3/guides/eval-file.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ description: Learn how to evaluate files containing Textwire code in your Go app
6
6
# Evaluating Files
7
7
Use the `EvaluateFile` function to evaluate a file containing Textwire code. The function accepts a file path and a map of variables to inject into the template. This approach is ideal for processing individual template files without setting up a full template engine.
Copy file name to clipboardExpand all lines: docs/versions/v3/guides/eval-string.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ description: Learn how to evaluate strings containing Textwire code in your Go a
6
6
# Evaluating Strings
7
7
Use the `EvaluateString` function to evaluate a string containing Textwire code. The function accepts a string template and a map of variables to inject. After evaluation, it returns the processed string and any error encountered.
8
8
9
-
This approach is ideal for dynamic content generation such as email templates, configuration files, or any scenario requiring template-based string processing without file-based templates. Here is an example:
9
+
This approach is ideal for dynamic content generation such as email templates, configuration files, or any scenario requiring template-based string processing without file-based templates. Example:
10
10
11
11
```go
12
12
inp:=`Hello <b>{{ name }}</b>! Congratulations on your {{ age }} birthday!`
Copy file name to clipboardExpand all lines: docs/versions/v3/guides/template-usage.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,7 +99,7 @@ If your template files are not showing up after you've created them and you are
99
99
Defining a layout in Textwire is straightforward. Create a layout file anywhere within your `templates` directory. Many developers organize layouts in a `templates/layouts/` directory to manage different layouts such as `main.tw`, `admin.tw`, and `user.tw`.
100
100
101
101
### Reserving Space in Layouts
102
-
The [@reserve](/v3/language-elements/directives#reserve) directive reserves placeholders for dynamic content that can be inserted later. For example, you can reserve a space for the page title and then populate it from other templates such as `about-me.tw` or `contact-us.tw`. Here is an example layout file:
102
+
The [@reserve](/v3/language-elements/directives#reserve) directive reserves placeholders for dynamic content that can be inserted later. For example, you can reserve a space for the page title and then populate it from other templates such as `about-me.tw` or `contact-us.tw`. Example layout file:
103
103
104
104
```textwire
105
105
<!DOCTYPE html>
@@ -120,7 +120,7 @@ This layout reserves spaces for the page title and content. These placeholders c
120
120
### Inserting Content into Reserved Spaces
121
121
The [@insert](/v3/language-elements/directives#insert) directive inserts content into reserved placeholders. It can be used in two ways: with or without a body. In the following example, we insert content for "title" without a body and for "content" with a body.
122
122
123
-
Here is an example`templates/views/home.tw` template:
123
+
Example`templates/views/home.tw` template:
124
124
125
125
```textwire
126
126
@use("layouts/main")
@@ -140,7 +140,7 @@ Here is an example `templates/views/home.tw` template:
140
140
You can read more about [@use](/v3/language-elements/directives#use), [@insert](/v3/language-elements/directives#insert) and [@reserve](/v3/language-elements/directives#reserve) directives on [this](/v3/language-elements/directives) page if you need more information about the syntax.
141
141
142
142
## Configuration
143
-
The `NewTemplate` function accepts a `*config.Config` parameter with several properties to customize template behavior. Common use cases include overriding the default file format or specifying the template directory. Here is an example of configuration:
143
+
The `NewTemplate` function accepts a `*config.Config` parameter with several properties to customize template behavior. Common use cases include overriding the default file format or specifying the template directory. Example:
Copy file name to clipboardExpand all lines: docs/versions/v3/language-elements/directives.md
+4-5Lines changed: 4 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,8 +19,7 @@ Textwire directives provide powerful control over your templates through command
19
19
20
20
## @if
21
21
22
-
You can use if directives to conditionally render content. You can construct `@if` directive using the `@if`, `@elseif`, `@else` and `@end` directives.
23
-
Here is an example of using if directives:
22
+
You can use if directives to conditionally render content. You can construct `@if` directive using the `@if`, `@elseif`, `@else` and `@end` directives. Example:
24
23
25
24
```textwire :no-line-numbers
26
25
@if(name == "Anna")
@@ -54,7 +53,7 @@ If you pass `nil` or an empty string to the `@if` directive, it will be treated
54
53
55
54
You can use regular for loops to iterate over an array or a range of numbers.
56
55
57
-
This is a basic for loop that you can use. It has a declaration, condition and post directive. `for <declaration>; <condition>; <post>`. They are all optional. Here is an example of using for loop:
56
+
This is a basic for loop that you can use. It has a declaration, condition and post directive. `for <declaration>; <condition>; <post>`. They are all optional. Example:
58
57
59
58
```textwire
60
59
{{ names = ["Ann", "Serhii"] }}
@@ -83,7 +82,7 @@ Read more about loops in the [Loops guide](/v3/guides/loops).
83
82
84
83
## @each
85
84
86
-
Each directive is a special form of `for` loop that you can use to iterate over an array. It has a declaration and an array. `@each(<declaration> in <array>)`. Here is an example of using each loop:
85
+
Each directive is a special form of `for` loop that you can use to iterate over an array. It has a declaration and an array. `@each(<declaration> in <array>)`. Example:
87
86
88
87
```textwire
89
88
{{ names = ["Ann", "Serhii"] }}
@@ -99,7 +98,7 @@ Read more about loops in the [Loops guide](/v3/guides/loops).
99
98
100
99
`@use` directives allow you to specify a layout file that will be used to render the current template. This feature is useful for creating reusable layouts that can be applied to multiple templates.
You can use infix expressions to perform arithmetic operations. Here is an example of using infix expressions:
33
+
You can use infix expressions to perform arithmetic operations. Example:
34
34
35
35
```textwire
36
36
<ul>
@@ -44,15 +44,15 @@ You can use infix expressions to perform arithmetic operations. Here is an examp
44
44
```
45
45
46
46
## Postfix
47
-
You can use postfix expressions to increment or decrement a variable. Here is an example of using postfix expressions:
47
+
You can use postfix expressions to increment or decrement a variable. Example:
48
48
49
49
```textwire :no-line-numbers
50
50
<span>{{ x++ }}</span> <!-- Increment -->
51
51
<span>{{ x-- }}</span> <!-- Decrement -->
52
52
```
53
53
54
54
## Comparison
55
-
Comparison expressions produce a boolean value. Here is an example of using comparison expressions:
55
+
Comparison expressions produce a boolean value. Example:
56
56
57
57
```textwire :no-line-numbers
58
58
@if(x == 1)
@@ -78,7 +78,7 @@ You can use function calls to call functions. Textwire has a few built-in functi
78
78
79
79
Functions in Textwire are type-specific, which means that you can't call a function on a variable that is not of the same type as the function. For example, you can't call a `split` function on an integer variable.
You can use string literals and concatenate them with other strings. You can use double or single quotes for strings. Here is an example of using string literals:
18
+
19
+
You can use string literals and concatenate them with other strings. You can use double or single quotes for strings. Example:
18
20
19
21
```textwire :no-line-numbers
20
22
{{ "Hello" + 'World!' }}
@@ -27,23 +29,30 @@ When you print a string, it will be automatically escaped. If you want to print
27
29
```
28
30
29
31
## Integer
30
-
You can use integer literals and perform arithmetic operations with them. Here is an example of using integer literals:
32
+
33
+
You can use integer literals and perform arithmetic operations with them. Example:
31
34
32
35
```textwire :no-line-numbers
33
36
<span>{{ 1 + 2 }}</span>
34
37
```
35
38
36
39
## Nil
37
-
You can use nil literal to check if a variable is nil. Here is an example of using nil literal:
40
+
41
+
Unlike Go, Textwire converts `nil` to `false` in boolean context. This means that `nil` is considered falsy in Textwire. Example:
38
42
39
43
```textwire :no-line-numbers
40
-
@if(nil)
41
-
<p>It will not be displayed</p>
44
+
{{ authUser = nil }}
45
+
46
+
@if(!authUser)
47
+
<p>You are not logged in!</p>
42
48
@end
43
49
```
44
50
51
+
Printing `nil` results in an empty string.
52
+
45
53
## Float
46
-
You can use float literals and perform arithmetic operations with them. Here is an example of using float literals:
54
+
55
+
You can use float literals and perform arithmetic operations with them. Example:
47
56
48
57
```textwire :no-line-numbers
49
58
<span>{{ 1.534 + 2.5 }}</span>
@@ -54,7 +63,8 @@ Most languages (including Textwire) use **IEEE 754 standard** for floating-point
54
63
:::
55
64
56
65
## Boolean
57
-
You can use boolean literals to check if a variable is true or false. Here is an example of using boolean literals:
66
+
67
+
You can use boolean literals to check if a variable is true or false. Example:
58
68
59
69
```textwire :no-line-numbers
60
70
@if(true)
@@ -63,7 +73,8 @@ You can use boolean literals to check if a variable is true or false. Here is an
63
73
```
64
74
65
75
## Array
66
-
Defining an array in Textwire is done is a similar way as in other languages. Here is an example of defining an array:
76
+
77
+
Defining an array in Textwire is done is a similar way as in other languages. Example:
67
78
68
79
```textwire
69
80
{{ names = ["John", "Jane", "Jack"] }}
@@ -75,7 +86,7 @@ Defining an array in Textwire is done is a similar way as in other languages. He
75
86
</ul>
76
87
```
77
88
78
-
You can access values in an array by using an index. Here is an example of accessing values in an array:
89
+
You can access values in an array by using an index. Example:
79
90
80
91
```textwire
81
92
{{ names = ["John", "Jane", "Jack"] }}
@@ -88,31 +99,25 @@ You can access values in an array by using an index. Here is an example of acces
88
99
</ul>
89
100
```
90
101
91
-
:::info Array Index Returns Nil
92
-
Accessing array on non-existant index returns `nil` instead of resulting in error.
93
-
:::
94
-
95
-
### Best Practices
96
-
Always check array access with index for `nil` before using it to prevent using functions on `nil` errors. Here are 2 ways of performing this check:
97
-
98
-
#### If Statement
99
-
```textwire
100
-
{{ names = [] }}
101
-
102
-
@if(names[0] != nil)
103
-
{{ names[0].upper() }}
104
-
@end
105
-
```
106
-
107
-
#### Ternary Expression
108
-
```textwire
109
-
{{ names = [] }}
110
-
111
-
{{ names[0] == nil ? '' : names[0].upper() }}
112
-
```
102
+
### Important Notes
103
+
104
+
- Accessing array on non-existant index returns `nil` instead of resulting in error.
105
+
- Printing array will convert it to comma seperated values. Example:
106
+
```textwire :no-line-numbers
107
+
<span>{{ [1, 2, 3] }}</span>
108
+
```
109
+
```html :no-line-numbers
110
+
<span>1, 2, 3</span> <!-- Output -->
111
+
```
112
+
- Always check array access with index for `nil` before using it to prevent using functions on `nil` errors. Example:
113
+
```textwire :no-line-numbers
114
+
{{ names = [] }}
115
+
{{ names[0] ? names[0].upper() : '' }}
116
+
```
113
117
114
118
## Object
115
-
Objects in Textwire are very similar to JavaScript object with key-value pairs. Here is an example of defining an object:
119
+
120
+
Objects in Textwire are very similar to JavaScript object with key-value pairs. Example:
116
121
117
122
```textwire :no-line-numbers
118
123
{{ person = {"name": "John", "age": 25} }}
@@ -124,7 +129,7 @@ You can also use key names without quotes if your keys are valid identifiers:
124
129
{{ person = { name: "John", age: 25 } }}
125
130
```
126
131
127
-
You can access values in an object by using a key. Here is an example of accessing values in an object:
132
+
You can access values in an object by using a key. Example:
@@ -144,9 +149,9 @@ You can access values in an object by using a key. Here is an example of accessi
144
149
Textwire automatically converts Go structs to objects, but **only exported fields** are converted. Since Go doesn't export fields that start with lowercase letters, Textwire cannot access them. Make sure to capitalize field names if you want them available in your templates.
145
150
:::
146
151
147
-
148
152
#### Shorthand Property Notation
149
-
Similar to objects in JavaScript, you can use shorthand property notation to define an object. Here is an example of using shorthand property notation:
153
+
154
+
Similar to objects in JavaScript, you can use shorthand property notation to define an object. Example:
0 commit comments