Skip to content

Commit 99772ed

Browse files
committed
docs: improvements
1 parent 9b6c8fa commit 99772ed

9 files changed

Lines changed: 61 additions & 57 deletions

File tree

docs/versions/v3/guides/configurations.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ If you are using VSCode and change the `TemplateExt` setting to anything other t
5858

5959
## Global Data
6060

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:
6262

6363
```go
6464
import (
@@ -77,7 +77,7 @@ tpl, err = textwire.NewTemplate(&config.Config{
7777
})
7878
```
7979

80-
You can access your global data in any Textwire template using the `global` object. Here is an example:
80+
You can access your global data in any Textwire template using the `global` object. Example:
8181

8282
```textwire
8383
@if(global.env == "development")

docs/versions/v3/guides/eval-file.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: Learn how to evaluate files containing Textwire code in your Go app
66
# Evaluating Files
77
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.
88

9-
Here is an example:
9+
Example:
1010

1111
```go
1212
path := "templates/email-template.tw"

docs/versions/v3/guides/eval-string.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description: Learn how to evaluate strings containing Textwire code in your Go a
66
# Evaluating Strings
77
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.
88

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:
1010

1111
```go
1212
inp := `Hello <b>{{ name }}</b>! Congratulations on your {{ age }} birthday!`

docs/versions/v3/guides/loops.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ It's a useful feature that eliminates the need for additional variables to track
110110

111111
## Else Statement
112112

113-
Use `@else` to render content when arrays are empty. This works with both `@for` and `@each` loops. Here is an example:
113+
Use `@else` to render content when arrays are empty. This works with both `@for` and `@each` loops. Example:
114114

115115
```textwire
116116
@each(name in [])

docs/versions/v3/guides/template-usage.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ If your template files are not showing up after you've created them and you are
9999
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`.
100100

101101
### 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:
103103

104104
```textwire
105105
<!DOCTYPE html>
@@ -120,7 +120,7 @@ This layout reserves spaces for the page title and content. These placeholders c
120120
### Inserting Content into Reserved Spaces
121121
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.
122122

123-
Here is an example `templates/views/home.tw` template:
123+
Example `templates/views/home.tw` template:
124124

125125
```textwire
126126
@use("layouts/main")
@@ -140,7 +140,7 @@ Here is an example `templates/views/home.tw` template:
140140
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.
141141

142142
## 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:
144144

145145
```go
146146
import (

docs/versions/v3/language-elements/directives.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ Textwire directives provide powerful control over your templates through command
1919

2020
## @if
2121

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:
2423

2524
```textwire :no-line-numbers
2625
@if(name == "Anna")
@@ -54,7 +53,7 @@ If you pass `nil` or an empty string to the `@if` directive, it will be treated
5453

5554
You can use regular for loops to iterate over an array or a range of numbers.
5655

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:
5857

5958
```textwire
6059
{{ names = ["Ann", "Serhii"] }}
@@ -83,7 +82,7 @@ Read more about loops in the [Loops guide](/v3/guides/loops).
8382

8483
## @each
8584

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:
8786

8887
```textwire
8988
{{ names = ["Ann", "Serhii"] }}
@@ -99,7 +98,7 @@ Read more about loops in the [Loops guide](/v3/guides/loops).
9998

10099
`@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.
101100

102-
Here is an example of using use directive:
101+
Example:
103102

104103
```textwire :no-line-numbers
105104
@use("layouts/main")

docs/versions/v3/language-elements/expressions.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ description: You can find here all the information about expressions in Textwire
1313
- [Function calls](#function-calls) <code v-pre>{{ name.split(" ") }}</code>
1414

1515
## Ternary
16-
You can use ternary expressions to conditionally render content. Here is an example of using ternary expressions:
16+
You can use ternary expressions to conditionally render content. Example:
1717

1818
```textwire :no-line-numbers
1919
<span>{{ x == 1 ? "yes" : "no" }}</span>
@@ -22,15 +22,15 @@ You can use ternary expressions to conditionally render content. Here is an exam
2222
The advantage of a "ternary expression" over an "if statement" is that it can be used inside of any other expressions.
2323

2424
## Prefix
25-
You can use prefix expressions to negate or invert a boolean value. Here is an example of using prefix expressions:
25+
You can use prefix expressions to negate or invert a boolean value. Example:
2626

2727
```textwire :no-line-numbers
2828
<span>{{ !isTall ? "Not tall" : "Is tall" }}</span>
2929
<span>{{ -x }}</span>
3030
```
3131

3232
## Infix
33-
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:
3434

3535
```textwire
3636
<ul>
@@ -44,15 +44,15 @@ You can use infix expressions to perform arithmetic operations. Here is an examp
4444
```
4545

4646
## 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:
4848

4949
```textwire :no-line-numbers
5050
<span>{{ x++ }}</span> <!-- Increment -->
5151
<span>{{ x-- }}</span> <!-- Decrement -->
5252
```
5353

5454
## Comparison
55-
Comparison expressions produce a boolean value. Here is an example of using comparison expressions:
55+
Comparison expressions produce a boolean value. Example:
5656

5757
```textwire :no-line-numbers
5858
@if(x == 1)
@@ -78,7 +78,7 @@ You can use function calls to call functions. Textwire has a few built-in functi
7878

7979
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.
8080

81-
Here is an example of using function calls:
81+
Example:
8282

8383
```textwire :no-line-numbers
8484
{{ name.split(" ") }}

docs/versions/v3/language-elements/literals.md

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
title: Literals - v3
33
description: Learn about Textwire literals like string, int, float, bool, nil, array, objects, etc.
4+
outline: [2, 3]
45
---
56

67
# Literals
@@ -14,7 +15,8 @@ description: Learn about Textwire literals like string, int, float, bool, nil, a
1415
- [Object](#object) <code v-pre>{{ { "name": "John", "age": 25 } }}</code>
1516

1617
## String
17-
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:
1820

1921
```textwire :no-line-numbers
2022
{{ "Hello" + 'World!' }}
@@ -27,23 +29,30 @@ When you print a string, it will be automatically escaped. If you want to print
2729
```
2830

2931
## 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:
3134

3235
```textwire :no-line-numbers
3336
<span>{{ 1 + 2 }}</span>
3437
```
3538

3639
## 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:
3842

3943
```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>
4248
@end
4349
```
4450

51+
Printing `nil` results in an empty string.
52+
4553
## 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:
4756

4857
```textwire :no-line-numbers
4958
<span>{{ 1.534 + 2.5 }}</span>
@@ -54,7 +63,8 @@ Most languages (including Textwire) use **IEEE 754 standard** for floating-point
5463
:::
5564

5665
## 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:
5868

5969
```textwire :no-line-numbers
6070
@if(true)
@@ -63,7 +73,8 @@ You can use boolean literals to check if a variable is true or false. Here is an
6373
```
6474

6575
## 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:
6778

6879
```textwire
6980
{{ names = ["John", "Jane", "Jack"] }}
@@ -75,7 +86,7 @@ Defining an array in Textwire is done is a similar way as in other languages. He
7586
</ul>
7687
```
7788

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:
7990

8091
```textwire
8192
{{ 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
8899
</ul>
89100
```
90101

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+
```
113117
114118
## 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:
116121
117122
```textwire :no-line-numbers
118123
{{ person = {"name": "John", "age": 25} }}
@@ -124,7 +129,7 @@ You can also use key names without quotes if your keys are valid identifiers:
124129
{{ person = { name: "John", age: 25 } }}
125130
```
126131

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:
128133

129134
```textwire
130135
{{ user = {age: 25, name: {first: "Anna", last: "Cho"}} }}
@@ -144,9 +149,9 @@ You can access values in an object by using a key. Here is an example of accessi
144149
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.
145150
:::
146151

147-
148152
#### 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:
150155

151156
```textwire :no-line-numbers
152157
{{ name = "John"; age = 25 }}

docs/versions/v3/language-elements/other.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ description: Learn about trailing commas and comments in Textwire, including how
1111

1212
## Trailing Commas
1313

14-
You can use trailing commas in arrays, objects, and function arguments. Here is an example of using trailing commas:
14+
You can use trailing commas in arrays, objects, and function arguments. Example:
1515

1616
```textwire
1717
{{ names = ["John", "Jane", "Jack",] }}
@@ -25,7 +25,7 @@ You can use trailing commas in arrays, objects, and function arguments. Here is
2525

2626
## Comments
2727

28-
You can use comments in Textwire to write notes or to comment out code. Here is an example of using comments:
28+
You can use comments in Textwire to write notes or to comment out code. Example:
2929

3030
```textwire
3131
{{-- This is a Textwire comment --}}
@@ -38,7 +38,7 @@ HTML comment will be displayed in the final HTML output, but Textwire comment wi
3838

3939
## Variable Declaration
4040

41-
You can assign and declare variables by using the `=` operator. Here is an example of declaring variables:
41+
You can assign and declare variables by using the `=` operator. Example:
4242

4343
```textwire :no-line-numbers
4444
{{ x = 5 }}

0 commit comments

Comments
 (0)