Skip to content

Latest commit

 

History

History
67 lines (46 loc) · 1.26 KB

templates.md

File metadata and controls

67 lines (46 loc) · 1.26 KB

Templates

Templates help to keep snippets concise by hiding parts of the code behind the scenes.

Let's say you have a Go program:

package main

import "fmt"

func main() {
    msg := "Hello, World!"
    fmt.Println(msg)
}

And suppose you don't want to distract the reader with package and import. Instead, you'd rather focus on the main body. In this case, do the following:

  1. Prepare a template file main.go:
package main

import (
    "fmt"
)

func main() {
    ##CODE##
}
  1. Create a snippet with the actual code:
<pre>
msg = "Hello, World!"
fmt.Println(msg)
</pre>

<codapi-snippet sandbox="go" editor="basic" template="main.go">
</codapi-snippet>
  1. Host the main.go next to the web page containing the codapi-snippet.

Now codapi-snippet will preprocess the code using the template before sending it to the server.

Alternatively, you can use an in-page script tag with a code template and pass its id as a template:

<script id="main.py" type="text/plain">
def greet(name):
    print(f"Hello, {name}!")

##CODE##
</script>

<pre>
greet("World")
</pre>

<codapi-snippet sandbox="python" template="#main.py"></codapi-snippet>

The leading # in template and type = text/plain in script are required.