Skip to content

Commit

Permalink
docs: explain export/import of components (#701)
Browse files Browse the repository at this point in the history
Co-authored-by: Adrian Hesketh <adrianhesketh@hushmail.com>
  • Loading branch information
gabrielvincent and a-h authored May 11, 2024
1 parent c2a2a13 commit 1ceb72c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
50 changes: 48 additions & 2 deletions docs/docs/03-syntax-and-usage/09-template-composition.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ templ right() {
</div>
```

# Children
## Children

Children can be passed to a component for it to wrap.

Expand Down Expand Up @@ -64,7 +64,7 @@ The use of the `{ children... }` expression in the child component.
</div>
```

# Components as parameters
## Components as parameters

Components can also be passed as parameters and rendered using the `@component` expression.

Expand Down Expand Up @@ -160,3 +160,49 @@ func main() {
<p>Dynamic contents</p>
</div>
```

## Sharing and re-using components

Since templ components are compiled into Go functions by the `go generate` command, templ components follow the rules of Go, and are shared in exactly the same way as Go code.

templ files in the same directory can access each other's components. Components in different directories can be accessed by importing the package that contains the component, so long as the component is exported by capitalizing its name.

:::tip
In Go, a _package_ is a collection of Go source files in the same directory that are compiled together. All of the functions, types, variables, and constants defined in one source file in a package are available to all other source files in the same package.

Packages exist within a Go _module_, defined by the `go.mod` file.
:::

:::note
Go is structured differently to JavaScript, but uses similar terminology. A single `.js` or `.ts` _file_ is like a Go package, and an NPM package is like a Go module.
:::

### Exporting components

To make a templ component available to other packages, export it by capitalizing its name.

```templ
package components
templ Hello() {
<div>Hello</div>
}
```

### Importing components

To use a component in another package, import the package and use the component as you would any other Go function or type.

```templ
package main
import "github.com/a-h/templ/examples/counter/components"
templ Home() {
@components.Hello()
}
```

:::tip
To import a component from another Go module, you must first import the module by using the `go get <module>` command. Then, you can import the component as you would any other Go package.
:::
3 changes: 1 addition & 2 deletions docs/docs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"

Expand Down Expand Up @@ -83,7 +82,7 @@ func main() {
for _, item := range items {
fmt.Println(item.Path)
if item.IsFile {
ioutil.WriteFile(item.Path, []byte(item.Content), 0644)
os.WriteFile(item.Path, []byte(item.Content), 0644)
continue
}
os.Mkdir(item.Path, 0755)
Expand Down

0 comments on commit 1ceb72c

Please sign in to comment.