### Scriggo Compilation Example
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/fixedbugs/issue399.html
Demonstrates the compilation and execution of a simple Go code snippet within the Scriggo environment. This example shows how to define and immediately invoke a function.
```go
{# compile #} {% (func() { a := 10 b := 20 })() %}
```
--------------------------------
### Scriggo Init Command
Source: https://github.com/open2b/scriggo/blob/main/cmd/scriggo/README.txt
Initializes an interpreter for Go programs. Requires Go to be downloaded and installed separately.
```bash
$ scriggo init
```
--------------------------------
### Scriggo Template Compilation Examples
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/functions.html
Demonstrates various ways to compile and execute Go functions within Scriggo templates. Includes examples of anonymous functions, function calls, and variable assignments.
```go
{# compile #} {% _ = func() { } %} {% _ = func() { } %} {% _ = func() { a := 5 _ = 5 } %} {% func() { }() %} {% func() { }() %} {% func() { a := 5 _ = a }() %}
```
```go
{{ func() int { return 5 }() }} {{ func() int { return 5 }() }}
```
--------------------------------
### Scriggo Error Checking Example
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/partial_not_exists.html
Demonstrates how Scriggo handles syntax errors during template rendering, specifically when a referenced file does not exist. This example shows the error message format.
```go
package main
import (
"fmt"
"github.com/open2b/scriggo"
)
func main() {
// Simulate a scenario where a template tries to render a non-existent file.
// The actual rendering logic would involve a Scriggo engine and context.
// This comment represents the error that would be caught:
// ERROR "syntax error: render path "foo.html" does not exist"
fmt.Println("Simulating Scriggo error checking.")
}
```
```html
{# errorcheck #} {% render "foo.html" %} // ERROR "syntax error: render path "foo.html" does not exist"
```
--------------------------------
### Scriggo Syntax Error Examples
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/no_statement.html
This snippet showcases different types of syntax errors in Scriggo, including missing statements, unexpected tokens, invalid characters, and misplaced control flow statements like 'return' and 'goto'. These examples are useful for understanding Scriggo's error reporting and parsing.
```go
{# errorcheck #} {% %} // ERROR `missing statement` {% : %} // ERROR `unexpected :, expecting statement` {% ∞ %} // ERROR `invalid character U+221E '∞'` {% } %} // ERROR `unexpected }, expecting statement` {% return %} // ERROR `return statement outside function body` {% goto %} // ERROR `goto statement outside function body` {{ } }} // ERROR `unexpected }, expecting expression`
```
--------------------------------
### Scriggo Select Statement Examples
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/leading_text.html
Shows the usage of the select statement in Scriggo, including scenarios with and without a default case.
```scriggo
{% select %} {% end %}
```
```scriggo
{% select %} {% default %} abc {% end %}
```
--------------------------------
### Scriggo Error Checking Examples
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/show.html
This section showcases different error conditions encountered in Scriggo templates. It includes examples of invalid variable usage, syntax errors in control structures, and incorrect function arguments.
```scriggo
{# errorcheck #} {{ _ }} // ERROR `cannot use _ as value` {% show _ %} // ERROR _cannot use _ as value` {% show(_) %} // ERROR `cannot use _ as value` {% show %} // ERROR `syntax error: unexpected %}, expecting expression` {% show , %} // ERROR `syntax error: unexpected comma, expecting expression` {% show() %} // ERROR `syntax error: unexpected ), expecting expression` {{ render "" }} // ERROR `syntax error: invalid file path: ""` {% show(1, 2) %} // ERROR `syntax error: unexpected comma, expecting )` {% show(if true {}) %} // ERROR `syntax error: unexpected if, expecting expression` {% show 1, 2, %} // ERROR `syntax error: unexpected %}, expecting expression`
```
--------------------------------
### Imported Macro Example
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/main_functions_import.dir/imported.html
This macro demonstrates how to import content from another file ('imported.html') and call a function ('MainSum') with specific arguments.
```scriggo
{% macro Imported %} Access from 'imported.html': {{ MainSum(90, 120) }} {% end macro %}
```
--------------------------------
### Scriggo Macro Usage
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/key-selector2.html
Demonstrates the basic usage of a macro in Scriggo to output a value.
```scriggo
{% macro M(x interface{}) %}{{ x }}{% end macro %} {{ M(n.M.b) }} {{ M(n.M.b.(int)) }}
```
--------------------------------
### Scriggo Switch Statement Examples
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/leading_text.html
Illustrates various ways to use the switch statement in Scriggo, including basic cases, type assertions, and default clauses.
```scriggo
{% switch %} {% end %}
```
```scriggo
{% switch %} {% case true %} abc {% end %}
```
```scriggo
{% switch (interface{}(nil)).(type) %} {% end %}
```
```scriggo
{% switch (interface{}(nil)).(type) %} {% case int %} abc {% end %}
```
--------------------------------
### Go Development Philosophy
Source: https://github.com/open2b/scriggo/wiki/Template-builtins:-how-to-write
Highlights the project's preference for ease of use over performance or completeness in Go development, guiding decisions on implementation choices.
```go
prefer ease of use to performance.
prefer ease of use to completeness.
```
--------------------------------
### Basic Scriggo Rendering Example
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/assignment.html
This snippet shows basic Scriggo syntax for variable declaration, assignment, and output. It includes integer and string variables, a length calculation, and rendering of these variables.
```scriggo
{# render #} {% a := 10 %} {% b := "string" %} {% c := len(b) + a %} {{ a }}{{ b }}{{ c }}
```
--------------------------------
### MainSum Function Example
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/main_functions_import.dir/partial.html
Demonstrates the usage of the MainSum function, which likely performs some form of summation or calculation. The function takes two integer arguments.
```Go
package main
import "fmt"
func MainSum(a, b int) int {
return a + b
}
func main() {
result := MainSum(40, 50)
fmt.Println(result) // Output: 90
}
```
--------------------------------
### Scriggo Extends Directive Errors
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/statements3.html
Demonstrates syntax errors related to the 'extends' directive in Scriggo, specifically when it's not placed at the beginning of the file.
```scriggo
{%% var a int; extends "layout.html" %%} // ERROR `syntax error: extends is not at the beginning of the file`
```
```scriggo
{%% var a int %%}{% extends "layout.html" %} // ERROR `syntax error: extends is not at the beginning of the file`
```
```scriggo
{% var a int %}{%% extends "layout.html" %%} // ERROR `syntax error: extends is not at the beginning of the file`
```
--------------------------------
### Scriggo Channel Operations
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/and-or-not.html
Illustrates the evaluation of nil and created channels in Scriggo templates using the 'not not' operator to check for their existence or truthiness.
```scriggo
{# render #} {% import "time" %} {% import "github.com/open2b/scriggo/test/compare/testpkg" %} ## CHANNELS (chan int)(nil): {{ not not (chan int)(nil)}} (chan \[\]int)(nil): {{ not not (chan \[\]int)(nil)}} make(chan string, 0): {{ not not make(chan string) }} make(chan string, 10): {{ not not make(chan string, 10) }}
```
--------------------------------
### Scriggo Syntax Errors: Increment/Decrement and 'using'
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/using2.html
Illustrates Scriggo syntax errors when increment (i++) or decrement operations are followed by the 'using' keyword or other incorrect syntax.
```scriggo
{% i++; using %}
// ERROR `syntax error: unexpected semicolon, expecting %}`
```
--------------------------------
### Define and Use Products Macro
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/extends.dir/partials/products.html
This macro, named 'Products', accepts a slice of strings and a slice of integers as input. It demonstrates how to use the 'len' function to get the length of these slices within the macro.
```scriggo
{% macro Products(a []string, b []int) %} {{ len(a) }} {{ len(b) }} {% end macro %}
```
--------------------------------
### Scriggo Syntax Errors: Assignment and 'using'
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/using2.html
Illustrates Scriggo syntax errors related to variable assignments (:=, +=, <-) when combined with the 'using' keyword or incorrect syntax following assignments.
```scriggo
{% a := itea; foo %} // ERROR `syntax error: unexpected foo, expecting using`
{% a += itea; %}
// ERROR `syntax error: unexpected semicolon, expecting %}`
{% c <- itea; %}
// ERROR `syntax error: unexpected semicolon, expecting %}`
{% c <- itea; foo %}
// ERROR `syntax error: unexpected foo, expecting using`
```
--------------------------------
### Define and Use Products Macro
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/extends_raw_strings.dir/partials/products.html
This macro, named 'Products', accepts a slice of strings and a slice of integers as input. It demonstrates how to use the 'len' function to get the length of these slices within the macro.
```scriggo
{% macro Products(a []string, b []int) %} {{ len(a) }} {{ len(b) }} {% end macro %}
```
--------------------------------
### Scriggo Boolean Conditionals
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/if.html
Illustrates the use of boolean values and their negations within Scriggo's conditional statements. It shows how `true`, `false`, and negated values are evaluated.
```scriggo
{# render #} ## Bool. {% if true %}if true{% end %} {% if not true %}bad{% else %}(if not true){% end %} {% if false %}bad{% else %}(if false){% end %} {% if not false %}if not false{% end %}
```
--------------------------------
### Scriggo Syntax Errors: Variable Declaration and 'using'
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/using2.html
Demonstrates Scriggo syntax errors related to variable declarations (`var`) when the 'using' keyword is misplaced or used with incorrect syntax.
```scriggo
{% var foo string %}
{% show itea; using foo %} // ERROR `syntax error: unexpected foo, expecting string, html, css, js, json, markdown, macro or %}`
{% var a = itea; foo %}
// ERROR `syntax error: unexpected foo, expecting using`
{% var a = itea; %}
// ERROR `syntax error: unexpected semicolon, expecting %}`
```
--------------------------------
### Scriggo Serve Command
Source: https://github.com/open2b/scriggo/blob/main/cmd/scriggo/README.txt
Runs a web server to serve Scriggo templates from the current directory. Supports HTML and Markdown rendering based on file extensions. Does not require a Go installation.
```bash
$ scriggo serve
```
--------------------------------
### Running Scriggo Comparison Tests
Source: https://github.com/open2b/scriggo/blob/main/test/compare/README.md
Steps to execute existing comparison tests for Scriggo. This involves ensuring the latest `scriggo` command is installed, navigating to specific directories, generating sources, building the `compare` command, and running the tests. It also mentions checking available options with `./compare -h`.
```bash
go generate
go build
./compare
./compare -h
```
--------------------------------
### Scriggo Syntax Error Example
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/var.html
This snippet demonstrates a common syntax error in Scriggo where a variable declaration is incorrectly terminated. The error message indicates that the '{' character was unexpected.
```go
{# errorcheck #} {% var ss []string {} %} // ERROR `syntax error: unexpected {, expecting %}`
```
--------------------------------
### Scriggo Template Syntax Example
Source: https://github.com/open2b/scriggo/blob/main/README.md
Illustrates the basic syntax of a Scriggo template, showcasing features like template inheritance, macro definitions, loops, rendering partials, and calling global functions.
```html
{% extends "layout.html" %}
{% import "banners.html" %}
{% macro Body %}
{{ render "pagination.html" }}
{{ Banner() }}
{% end %}
```
--------------------------------
### Scriggo Templating Example
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/markdown-in-html.dir/layout.html
Demonstrates the usage of Scriggo's templating language, including importing external files and calling functions with arguments. It shows how to render dynamic content within an HTML structure.
```scriggo
{% import "imports.md" %}
Markdown in HTML {{ A() }} {{ B(\[\]string{"item 1", "item 2", "item 3"}) }}
```
--------------------------------
### Scriggo Syntax Error: Unexpected Token After Label
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/label_syntax.html
This example illustrates an error where an unexpected token (B) appears after a label (A), instead of an expected control flow statement.
```scriggo
{# errorcheck #} {% A: B: %} // ERROR `unexpected B, expecting for, switch or select`
```
--------------------------------
### Scriggo Template Basic Operations
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/precompiled_packages.html
This snippet demonstrates importing standard Go packages like 'fmt' and 'math', along with custom packages. It shows variable assignment, calling imported functions (fmt.Sprint, Abs), and basic string manipulation using the 'strings' package.
```scriggo
{# render #} {% import "fmt" %} {% import . "math" %} {% import s "strings" %} {{ fmt.Sprint(10, 20) }} {% x := -123.1 %} The absolute value of {{ x }} is {{ Abs(x) }} {% for part in s.Split("this is a Scriggo template", " ") %}{{ part }},{% end for %}
```
--------------------------------
### Hello World in Scriggo
Source: https://github.com/open2b/scriggo/blob/main/playground/index.html
A basic Go program that prints 'Hello, playground' to the console. This demonstrates the fundamental usage of the Scriggo playground.
```go
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello, playground")
}
```
--------------------------------
### Scriggo Syntax Error: Invalid Statement Delimiter
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/switch_first_case.html
This example shows a syntax error caused by using an incorrect delimiter `%%` for a statement within the Scriggo template. The correct delimiter for statements is `{%%`.
```scriggo
{# errorcheck #} {% switch %}{%% a := 5 %%}{% end %} // ERROR `syntax error: unexpected {%%, expecting {%`
```
--------------------------------
### Build highlight.js with Scriggo Support
Source: https://github.com/open2b/scriggo/blob/main/highlighters/highlight.js/README.md
This snippet demonstrates the command-line steps required to build a custom highlight.js file that includes Scriggo syntax highlighting. It involves cloning the highlight.js repository, copying the Scriggo highlighter, and executing the build script.
```bash
git clone https://github.com/highlightjs/highlight.js
cd highlight.js
git checkout 11.2.0
npm install
cp /highlighters/highlight.js/scriggo.js src/languages
node tools/build.js scriggo
cp build/highlight.js
cp build/highlight.min.js
```
--------------------------------
### Scriggo Error Handling: Type Assertion Syntax
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/leading_text2.html
This example illustrates an error in Scriggo related to incorrect syntax for type assertions within control structures, highlighting how the parser expects specific tokens.
```scriggo
{% switch (interface{}(nil)).(type) %} abc // ERROR `unexpected text, expecting {%` {% end %}
```
--------------------------------
### Go Package and Naming Conventions
Source: https://github.com/open2b/scriggo/wiki/Template-builtins:-how-to-write
Recommendations for Go development, including the restriction to use only the Go standard library and preferences for naming conventions, such as using `unmarshalJSON` instead of `parseJSON`, mirroring the Go standard library's style.
```go
don't use the `unsafe` package.
naming things, prefer names used in the Go standard library. For example `unmarshalJSON` instead of `parseJSON`.
```
--------------------------------
### Scriggo Syntax Error: Unexpected Expression Delimiter
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/switch_first_case.html
This example illustrates a syntax error resulting from using double curly braces `{{` for an expression within a Scriggo template. The correct delimiter for expressions is `{` followed by the expression and then `}`.
```scriggo
{# errorcheck #} {% switch %}{{ a }}{% end %} // ERROR `syntax error: unexpected {{, expecting {%`
```
--------------------------------
### Scriggo Syntax Error: Unexpected }}
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/functions2.html
This example shows a syntax error in Scriggo where an unexpected '}}' token is found. This error often arises from incorrect Go function syntax or misplaced closing braces within the template.
```go
{{ func() int { }}
// ERROR `syntax error: unexpected }}, expecting }`
```
--------------------------------
### Go Error Handling and Panics
Source: https://github.com/open2b/scriggo/wiki/Template-builtins:-how-to-write
Details on how error messages should be prefixed with the builtin name and how errors should be created using `errors.New`. It also covers the preferred format for panic arguments and when to use panics versus fatals.
```go
error messages must have the builtin name as prefix: `parseInt: invalid base 1`
errors should be created with `errors.New`. Use a different error type only if it is necessary and in this case add its type as builtin type.
panic's argument should be a string with the same format of an error. If not a string, the `builtin` package must export a variable containing this value.
prefer panics to fatals. Fatals should be used only if the execution has reached an inconsistent state and cannot continue.
```
--------------------------------
### Scriggo Syntax Error: Unexpected Comment
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/switch_first_case.html
This example highlights a syntax error where a comment `{# ... #}` is placed incorrectly within a Scriggo statement block. Comments should be outside of statement delimiters or within specific comment syntax.
```scriggo
{# errorcheck #} {% switch %}{# comment #}{% end %} // ERROR `syntax error: unexpected comment, expecting {%`
```
--------------------------------
### Scriggo Template - Importing and Rendering
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/main_functions_import.dir/index.html
Demonstrates basic Scriggo template syntax for importing external templates, rendering partials, and calling functions within the template. It shows how to include content from other files and execute logic defined elsewhere.
```html
{% import . "imported.html" %}
Access from 'index.html': {{ MainSum(10, 20) }} {{ render "partial.html" }} {{ Imported() }}
```
--------------------------------
### Scriggo Syntax Error: Unexpected Variable Assignment
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/switch_first_case.html
This example demonstrates a syntax error where a variable is assigned within a switch case without proper syntax. The expected syntax involves using `:=` for declaration and assignment within a valid block.
```scriggo
{# errorcheck #} {% switch %}{% case true %}{% end %} {% switch %}{% default %}{% end %} {% switch %}{% a := 5 %}{% end %} // ERROR `syntax error: unexpected a, expecting case or default or end`
```
--------------------------------
### Scriggo: Nested For Loops with Else Clauses
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/syntax/for-else.html
Shows examples of nested 'for' loops in Scriggo where 'else' clauses are used incorrectly, leading to syntax errors. This covers cases with misplaced 'else' in inner loops or incorrect 'else' usage in outer loops.
```scriggo
{# errorcheck #} {% for a in "" %} {% if true %} {% else %} {% end %} {% else %} {% end %} // This is valid
{% for a in "" %} {% if true %} {% else if true %} {% end %} {% else %} {% end %} // This is valid
{% for a in "" %} {% for b in "" %} {% else %} {% end for %} {% else %} {% end for %} // ERROR `unexpected else`
```
--------------------------------
### Basic URL Rendering
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/markdown_url.md
Demonstrates rendering of basic URLs using Scriggo templates, including static and dynamically inserted hostnames.
```scriggo
{# render #}
http://www.open2b.com/
https://www.open2b.com/
http://{{ "www.open2b.com" }}/
https://{{ "www.open2b.com" }}/
```
--------------------------------
### Basic Rendering in Scriggo
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/hello_world.html
Demonstrates the basic rendering of a string literal within a Scriggo template. The `{# render #}` directive initiates the rendering process, and `{{ "Hello, World!" }}` outputs the string.
```scriggo
{# render #} {{ "Hello, World!" }}
```
--------------------------------
### Scriggo Raw Block Error
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/raw2.html
Illustrates an error when using `_` as a marker within a raw block, which is not permitted.
```scriggo
// ERROR `cannot use _ as marker` {% raw _ %}
```
--------------------------------
### Running Scriggo Tests
Source: https://github.com/open2b/scriggo/blob/main/CONTRIBUTING.md
Instructions for executing different types of tests for Scriggo, including standard Go tests and comparison tests against the gc compiler.
```go
go test ./...
```
```go
go test ./...
```
```go
go test ./...
```
--------------------------------
### Scriggo Raw in Quoted Attribute Error
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/raw2.html
Illustrates an error when `raw` is used within a quoted attribute, which is not allowed.
```scriggo
[// ERROR `cannot use raw in quoted attribute`]({% raw %}{% end %})
```
--------------------------------
### Scriggo Unexpected Raw After Code Block
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/raw2.html
Shows an error where `raw` is unexpectedly encountered after a `code` block, expecting `code`.
```scriggo
// ERROR `unexpected code, expecting raw` {% raw code %} {% end code %} {% end raw code %}
```
--------------------------------
### Define and Use Scriggo Macros
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/markdown-in-html.dir/imports.md
Demonstrates defining two macros: 'B' which takes a string slice and iterates over it, and 'b' which prints a header. The 'B' macro calls 'b' and then lists the items.
```scriggo
{% macro B(items []string) %}
{{ b() }}
{% for item in items %}
* {{ item }}
{% end %}
{% end macro %}
{% macro b %}
## items
{% end macro %}
```
--------------------------------
### Scriggo Unexpected If in Raw Block
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/raw2.html
Shows an error where `if` is unexpectedly encountered within a raw block, expecting an identifier, string, or `%`.
```scriggo
// ERROR `unexpected if, expecting identifier, string or %}` {% raw if %}
```
--------------------------------
### Scriggo Testing Modes Overview
Source: https://github.com/open2b/scriggo/blob/main/test/compare/README.md
This section outlines the various testing modes supported by Scriggo. Each mode specifies how a test case should be executed and validated, along with the file extensions it supports.
```go
// skip
// The test is skipped. Everything after the `skip` keyword is ignored.
```
```go
// compile
// build
// The test compiles successfully.
```
```go
// run
// The test compiles and runs successfully and the standard output is the same as the one returned by gc
```
```go
// rundir
// The test inside the _dir-directory_ (see below) associated to the test compiles and runs successfully and the standard output matches the content of the _golden file_ associated to the test (see below).
```
```go
// paniccheck
// The test panics and the error message matches the content of the _golden file_ associated to the test (see below).
```
```go
// errorcheck
// For each row ending with a comment `// ERROR error message`, the compilation fails with the error message reported in the comment. Error message must be enclosed between ` characters or " characters. While the former takes the error message as is, the latter support regular expression syntax. For instance, if the error message contains a " character, you can both enclose the error message in double quotes (escaping the character) or use the backtick without having to escape it.
```
--------------------------------
### Scriggo Syntax Error: Missing Statement
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/label_syntax.html
This snippet demonstrates an error where a label is followed by a missing statement, which is not allowed in Scriggo.
```scriggo
{# errorcheck #} {% A: %} // ERROR `missing statement after label`
```
--------------------------------
### Scriggo Time Operations
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/and-or-not.html
Shows the evaluation of zero-valued and current time values in Scriggo templates using the 'not not' operator.
```scriggo
{# render #} {% import "time" %} {% import "github.com/open2b/scriggo/test/compare/testpkg" %} ## Time time.Time{}: {{ not not time.Time{} }} time.Now(): {{ not not time.Now() }}
```
--------------------------------
### Execute a Scriggo Template with Globals
Source: https://github.com/open2b/scriggo/blob/main/README.md
Shows how to build and run a Scriggo template that includes dynamic content rendered using global variables and functions. It demonstrates passing Go variables and functions into the template execution context.
```go
// Build and run a Scriggo template.
package main
import (
"os"
"github.com/open2b/scriggo"
"github.com/open2b/scriggo/builtin"
"github.com/open2b/scriggo/native"
)
func main() {
// Content of the template file to run.
content := []byte(`
Hello
Hello, {{ capitalize(who) }}!
`)
// Create a file system with the file of the template to run.
fsys := scriggo.Files{"index.html": content}
// Declare some globals.
var who = "world"
opts := &scriggo.BuildOptions{
Globals: native.Declarations{
"who": &who, // global variable
"capitalize": builtin.Capitalize, // global function
},
}
// Build the template.
template, err := scriggo.BuildTemplate(fsys, "index.html", opts)
if err != nil {
panic(err)
}
// Run the template and print it to the standard output.
err = template.Run(os.Stdout, nil, nil)
if err != nil {
panic(err)
}
}
```
--------------------------------
### Scriggo Brace and Token Errors
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/statements3.html
Highlights syntax errors involving misplaced or incorrect braces and tokens within Scriggo code.
```scriggo
{%% { %%} // ERROR `syntax error: unexpected %%}, expecting }`
```
```scriggo
{%% } %%} // ERROR `syntax error: unexpected }, expecting statement`
```
```scriggo
{%% if true { %%}{% end %} // ERROR `syntax error: unexpected %%}, expecting }`
```
```scriggo
{% if true %}{%% } %%} // ERROR `syntax error: unexpected }, expecting statement`
```
```scriggo
{%% %} %%} // ERROR `syntax error: unexpected %}, expecting %%}`
```
```scriggo
{% %%} %} // ERROR `syntax error: unexpected %%}, expecting %}`
```
--------------------------------
### Execute a Go Program with Scriggo
Source: https://github.com/open2b/scriggo/blob/main/README.md
Demonstrates how to build and run a Go program embedded within an application using Scriggo. It involves creating a file system with the program source, building it, and then executing it.
```go
package main
import "github.com/open2b/scriggo"
func main() {
// src is the source code of the program to run.
src := []byte(`
package main
func main() {
println("Hello, World!")
}
`)
// Create a file system with the file of the program to run.
fsys := scriggo.Files{"main.go": src}
// Build the program.
program, err := scriggo.Build(fsys, nil)
if err != nil {
panic(err)
}
// Run the program.
err = program.Run(nil)
if err != nil {
panic(err)
}
}
```
--------------------------------
### Scriggo Unexpected Code with Quoted String
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/raw2.html
Highlights an error where `code` follows a quoted string within a raw block, expecting only `%`.
```scriggo
// ERROR `unexpected code, expecting %}` {% raw `code` code %}
```
--------------------------------
### Scriggo Templating Syntax
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/markdown.md
Demonstrates the basic syntax for rendering text and Markdown elements within Scriggo templates. This includes how to output static text, apply basic Markdown formatting, and render HTML tags.
```scriggo
{{ "# heading" }}
{{ "---" }}
{{ "===" }}
{{ " text text " }}
{{ "\ttext\ttext\t" }}
{{ "**bold**" }}
{{ "__bold__" }}
{{ "*italic*" }}
{{ "***bold and italic***" }}
{{ "~strikethrough~" }}
{{ "> blockquotes" }}
{{ ">> nested blockquotes" }}
{{ "> #### blockquote with heading" }}
{{ "1. ordered list" }}
{{ " 1. indented ordered list" }}
{{ "- unordered list" }}
{{ "+ unordered list" }}
{{ "* unordered list" }}
{{ "" }}
{{ "`code`" }}
{{ "``code``" }}
{{ "```code```" }}
{{ " code block" }}
{{ "\tcode block" }}
{{ "a\n***b***" }}
{{ "a\n***b***" }}
{{ "***" }}
{{ "---" }}
{{ "___" }}
{{ "[markdown](https://en.wikipedia.org/wiki/Markdown)" }}
{{ "[markdown](https://en.wikipedia.org/wiki/Markdown \"title\")" }}
{{ "" }}
{{ "" }}
{{ "[`code`](#code)" }}
{{ "[](https://en.wikipedia.org/wiki/Markdown)" }}
{{ "\\*" }}
{{ "word" }}
{{ "| | | | | |" }}
{{ "|---|---|---|---|---|" }}
```
--------------------------------
### Scriggo Unexpected Code in Raw Block
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/raw2.html
Demonstrates an error when `code` is unexpectedly found within a raw block, expecting a string or `%`.
```scriggo
// ERROR `unexpected code, expecting string or %}` {% raw code code %}
```
--------------------------------
### Compile Scriggo for WASM on Windows
Source: https://github.com/open2b/scriggo/blob/main/playground/README.md
This snippet outlines the commands for compiling Scriggo for WebAssembly on a Windows environment. It uses SET commands for environment variables and COPY for the execution script, reflecting Windows command prompt syntax.
```batch
SET GOOS=js
SET GOARCH=wasm
scriggo import -v -o packages.go
go build -tags osusergo,netgo -trimpath -o scriggo.wasm
copy "C:\Program Files\Go\misc\wasm\wasm_exec.js" .
```
--------------------------------
### Scriggo Function Variable Evaluation
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/and-or-not.html
Demonstrates the evaluation of function variables in Scriggo templates, checking for nil and assigned function literals.
```scriggo
{# render #} {% import "time" %} {% import "github.com/open2b/scriggo/test/compare/testpkg" %} ## Functions var f func(): {% var f func() %}{{ not not f }} var f = func() {}: {% f = func() {} %}{{ not not f }}
```
--------------------------------
### Scriggo Templating Basics
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/extends_raw_strings.dir/index.html
Demonstrates fundamental Scriggo syntax including template inheritance, macro definitions, variable declarations, and conditional logic.
```scriggo
{% extends `layouts/home.html` %} {% import . `partials/banners.html` %} {% import . `partials/products.html` %} {% macro Head %}Page head{% end macro %} {% macro Main %} {% translate := func(string) string { return `` } %} {% title := `Page title` %} {% if title != `` %}
{{ title }}
===========
{% end %} {% products := make([]string, 10) %} {% columns := make([]int, 10) %} {% if len(products) > 0 %}
{{ translate(`Featured`) }}
-----------------------------
{% show Products(products, columns) %} {% end if %} {{ Banner(1, true, `3000/500`) }} {% promotions := make([]int, 20) %} {% if len(promotions) > 0 %}
{{ translate(`Home promotions`) }}
------------------------------------
{# {{ render `partials/promotions.html` }} #} {% end if %} {% topSellers := make([]string, 10) %} {% topSellersColumns := make([]int, 10) %} {% if len(topSellers) > 0 %}
{{ translate(`Top sellers`) }}
--------------------------------
{{ Products(topSellers, topSellersColumns) }}
{% end if %} {% blogPosts := make([]string, 10) %} {% if len(blogPosts) > 0 %}
{{ translate(`News`) }}
-------------------------
{# {{ render `partials/blog-posts.html` }} #} {% end if %} {% end macro %}
```
--------------------------------
### Displaying Content with Scriggo
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/full2.dir/layouts/standard.html
Demonstrates how to display content within a Scriggo template using the `show` directive. This is typically used to render blocks defined elsewhere or to output specific components.
```scriggo
{% show Head() %}
```
```scriggo
{% show Main() %}
```
--------------------------------
### Scriggo If Statement Syntax Errors
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/end.html
Illustrates incorrect syntax with Scriggo 'if' statements, such as using 'end for' or 'end if abc' after an 'if' block.
```scriggo
{% if true %} {% end %} {% if true %} {% end if %} {% if true %} {% end for %} // ERROR `syntax error: unexpected for, expecting if or %}` {% if true %} {% end if abc %} // ERROR `syntax error: unexpected abc, expecting %}`
```
--------------------------------
### Go Type and Parameter Preferences
Source: https://github.com/open2b/scriggo/wiki/Template-builtins:-how-to-write
Guidelines for choosing data types for function and method parameters, favoring built-in types like `string`, `int`, and `float64` over their alternatives like `[]byte`, `rune`, `int32`, or `float32`. It also advises against using pointers unless strictly necessary.
```go
for function and method parameters, prefer `string` to `[]byte` and `rune`, `int` to other integer types and `float64` to `float32`.
for function and method parameters and for builtin variables don't use pointers unless absolutely necessary.
```
--------------------------------
### Scriggo Slice and Append Errors
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/key-selector2.html
Shows errors that occur when attempting to use the `append` function incorrectly, such as providing a non-slice type as the first argument.
```scriggo
{# errorcheck #} {%% m := map[string]int{} %%} {% append(m.X, 5) %} // ERROR `first argument to append must be slice; have int`
```
--------------------------------
### Scriggo Templating - Basic Structure
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/extends.dir/index.html
Demonstrates the basic structure of a Scriggo template, including extending layouts, importing partials, and defining macros. It shows how to set page titles and render content conditionally.
```html
{% extends "layouts/home.html" %} {% import "partials/banners.html" %} {% import "partials/products.html" %} {% macro Head %}Page head{% end macro %} {% macro Main %} {% translate := func(string) string { return "" } %} {% title := "Page title" %} {% if title != "" %}
{{ title }}
===========
{% end %} {% products := make(\[\]string, 10) %} {% columns := make(\[\]int, 10) %} {% if len(products) > 0 %}
{{ translate("Featured") }}
---------------------------
{{ Products(products, columns) }} {% end if %} {{ Banner(1, true, "3000/500") }} {% promotions := make(\[\]int, 20) %} {% if len(promotions) > 0 %}
{{ translate("Home promotions") }}
----------------------------------
{# {{ render "partials/promotions.html" }} #} {% end if %} {% topSellers := make(\[\]string, 10) %} {% topSellersColumns := make(\[\]int, 10) %} {% if len(topSellers) > 0 %}
{{ translate("Top sellers") }}
------------------------------
{{ Products(topSellers, topSellersColumns) }}
{% end if %} {% blogPosts := make(\[\]string, 10) %} {% if len(blogPosts) > 0 %}
{{ translate("News") }}
-----------------------
{# {{ render "partials/blog-posts.html" }} #} {% end if %} {% end macro %}
```
--------------------------------
### Scriggo Type Mismatch Errors
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/key-selector2.html
Illustrates errors where values of one type are assigned to variables of an incompatible type, or when operations are performed on incorrect types.
```scriggo
{# errorcheck #} {%% m := map[string]int{} %%} {% m.A = "A" %} // ERROR `cannot use "A" (type untyped string) as type int in assignment` {% m.B.C = "A" %} // ERROR `m.B.C undefined (type int has no field or method C)` {% m.B := "B" %} // ERROR `non-name m.B on left side of :=` {% var m.B int %} // ERROR `unexpected ., expecting type` {% m.A, m.B, m.C = "a", "b", 3 %} // ERROR `cannot use "a" (type untyped string) as type int in assignment`
```
--------------------------------
### Scriggo Templating Directives
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/full.dir/layouts/home.html
Demonstrates common Scriggo directives for importing partials, rendering templates, and displaying content. These directives are essential for structuring web pages and managing reusable components.
```html
{% import . "/partials/banners.html" %}
{% import . "/partials/menu.html" %}
{{ render "/partials/header.html" }}
{{ render "/partials/navigation.html" }}
{% show Banner(0, true, "6000/500") %}
{% show Main() %}
{{ render "/partials/footer.html" }}
```
--------------------------------
### Scriggo Switch Statement
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/label.html
Illustrates the usage of a switch statement in Scriggo. It includes a default case and demonstrates how to break out of the switch block using a label.
```scriggo
{# compile #} {% B: switch %}{% default %}{% break B %}{% end %}
```
--------------------------------
### Scriggo Syntax Error: Unexpected Semicolon
Source: https://github.com/open2b/scriggo/blob/main/test/compare/testdata/templates/label_syntax.html
This snippet shows an error caused by an unexpected semicolon immediately after a label, where a control flow statement is expected.
```scriggo
{# errorcheck #} {% A: ; %} // ERROR `unexpected semicolon, expecting for, switch or select`
```
--------------------------------
### Scriggo Testing Modes Overview
Source: https://github.com/open2b/scriggo/blob/main/test/compare/README.md
This section outlines the various testing modes supported by Scriggo. Each mode specifies how a test case should be executed and validated, along with the file extensions it supports.
```html
// skip
// The test is skipped. Everything after the `skip` keyword is ignored.
```
```html
// compile
// build
// The test compiles successfully.
```
```html
// errorcheck
// For each row ending with a comment `// ERROR error message`, the compilation fails with the error message reported in the comment. Error message must be enclosed between ` characters or " characters. While the former takes the error message as is, the latter support regular expression syntax. For instance, if the error message contains a " character, you can both enclose the error message in double quotes (escaping the character) or use the backtick without having to escape it.
```