Configurations
Textwire provides configuration options to customize template behavior. You can apply these settings in two ways:
-
Using
textwire.Configure: Call thetextwire.Configurefunction to set global configurations for the library. -
Passing to
textwire.NewTemplate: Pass the configuration directly to thetextwire.NewTemplatefunction when creating templates.
Setting configurations with Configure and NewTemplate functions is only applied when you use Textwire as templating system.
For simple string evaluations with EvaluateString or EvaluateFile, configurations are ignored.
Setting Configurations
For detailed instructions on using textwire.NewTemplate, refer to the Usage with Templates guide. Below is a basic example of setting configurations with textwire.Configure:
import (
"github.com/textwire/textwire/v3"
"github.com/textwire/textwire/v3/config"
)
func main() {
textwire.Configure(&config.Config{
TemplateDir: "my-templates",
DebugMode: true,
})
}
In the example above, TemplateDir applies only when using Textwire as a template engine for your project. For evaluating individual Textwire files or strings, this configuration is not required.
All Textwire configurations are optional and include sensible default values. Read more about the available configurations below.
Available Configurations
| Property | Type | Description | Default value |
|---|---|---|---|
TemplateDir | string | The directory where Textwire searches for template files. | "templates" |
TemplateFS | fs.FS | Provides an optional fs.FS filesystem for template access. Use this field to embed templates into your binary using Go's embed package. Read more. | os.DirFS(TemplateDir) |
TemplateExt | string | The file extension for template files. | ".tw" |
ErrorPagePath | string | The path to the custom error page, relative to the TemplateDir directory. Custom error pages are displayed only when DebugMode is false. | "" |
DebugMode | bool | A flag that enables debug mode. When enabled, error messages are displayed in the browser. Read more in the Error Handling guide. | false |
GlobalData | map[string]any | Global data accessible in all Textwire files. Read more. | map[string]any{} |
If you are using VSCode and change the TemplateExt setting to anything other than .tw, you will lose syntax highlighting for Textwire files provided by the Textwire extension. To maintain full extension functionality, use .tw as the extension for Textwire files.
Global Data
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:
import (
"os"
"github.com/textwire/textwire/v3"
"github.com/textwire/textwire/v3/config"
)
tpl, err = textwire.NewTemplate(&config.Config{
ErrorPagePath: "error-page",
DebugMode: true,
GlobalData: map[string]any{
"env": os.Getenv("APP_ENV"),
"auth": auth.User(),
},
})
You can access your global data in any Textwire template using the global object. Here is an example:
@if(global.env == "development")
<p>You are in development mode</p>
@end
@if(global.auth == nil)
<p>Please log in.</p>
@else
<p>Welcome, {{ global.auth.name }}!</p>
@end
The global identifier is a reserved word and cannot be used for variable names. Read more about reserved variables.