### Core Application Setup in Go Source: https://context7.com/go-via/via/llms.txt Demonstrates the basic setup and configuration of a Via application. It shows how to create a new Via instance, configure options like logging level and document title, and append custom CSS and scripts to the document head and footer. The example concludes with starting the server. ```go package main import ( "github.com/go-via/via" "github.com/go-via/via/h" ) func main() { // Create new Via application with default configuration v := via.New() // Configure application options v.Config(via.Options{ LogLvl: via.LogLevelInfo, DocumentTitle: "My Via App", Plugins: nil, }) // Append custom CSS or scripts to document head v.AppendToHead( h.Link(h.Rel("stylesheet")), h.Href("/static/styles.css")), h.Script(h.Src("/static/custom.js")), ) // Append scripts to document footer v.AppendToFoot( h.Script(h.Raw(`console.log("App loaded");`))), ) // Start server on specified address v.Start(":3000") } ``` -------------------------------- ### Create a reactive counter with Via in Go Source: https://github.com/go-via/via/blob/main/README.md This example demonstrates how to create a reactive counter using Via in Go. It includes a view with a counter display, an input for step adjustment, and a button to increment the counter. The state is managed entirely in Go with real-time updates. ```go package main import ( "github.com/go-via/via" "github.com/go-via/via/h" ) type Counter struct{ Count int } func main() { v := via.New() v.Page("/", func(c *via.Context) { data := Counter{Count: 0} step := c.Signal(1) increment := c.Action(func() { data.Count += step.Int() c.Sync() }) c.View(func() h.H { return h.Div( h.P(h.Textf("Count: %d", data.Count)), h.Label( h.Text("Update Step: "), h.Input(h.Type("number"), step.Bind()), ), h.Button(h.Text("Increment"), increment.OnClick()), ) }) }) v.Start(":3000") } ``` -------------------------------- ### Go Actions and Event Handlers - Server-side Actions Source: https://context7.com/go-via/via/llms.txt Demonstrates how to register server-side actions that respond to user interactions. Shows how to create actions that modify state, bind them to HTML elements, and sync updates to the browser. Uses the via framework's Action API with Counter component example. ```go package main import ( "github.com/go-via/via" "github.com/go-via/via/h" ) type Counter struct { Count int } func main() { v := via.New() v.Page("/", func(c *via.Context) { data := Counter{Count: 0} step := c.Signal(1) // Create action that modifies state increment := c.Action(func() { data.Count += step.Int() c.Sync() // Push updates to browser }) decrement := c.Action(func() { data.Count -= step.Int() c.Sync() }) reset := c.Action(func() { data.Count = 0 step.SetValue(1) c.Sync() }) c.View(func() h.H { return h.Div( h.P(h.Textf("Count: %d", data.Count)), h.P(h.Span(h.Text("Step: ")), h.Span(step.Text())), h.Label( h.Text("Update Step: "), h.Input(h.Type("number"), step.Bind()), ), // Attach actions to button click events h.Button(h.Text("Increment"), increment.OnClick()), h.Button(h.Text("Decrement"), decrement.OnClick()), h.Button(h.Text("Reset"), reset.OnClick()), ) }) }) v.Start(":3000") } ``` -------------------------------- ### Reactive Signals and Two-Way Data Binding in Go Source: https://context7.com/go-via/via/llms.txt Explains how to create and utilize reactive signals within a Via application for two-way data binding. Signals are created with initial values and their states are automatically synchronized between the server and the browser. The example demonstrates displaying signal values reactively and binding them to input elements like text fields, number inputs, and checkboxes. ```go package main import ( "github.com/go-via/via" "github.com/go-via/via/h" ) func main() { v := via.New() v.Page("/", func(c *via.Context) { // Create signals with initial values name := c.Signal("World") age := c.Signal(25) isActive := c.Signal(true) c.View(func() h.H { return h.Div( // Display signal values reactively using Text() h.P(h.Span(h.Text("Hello, ")), h.Span(name.Text())), h.P(h.Span(h.Text("Age: ")), h.Span(age.Text())), h.P(h.Span(h.Text("Active: ")), h.Span(isActive.Text())), // Bind signals to input elements for two-way sync h.Label( h.Text("Name: "), h.Input(h.Type("text"), name.Bind()), ), h.Label( h.Text("Age: "), h.Input(h.Type("number"), age.Bind()), ), h.Label( h.Text("Active: "), h.Input(h.Type("checkbox"), isActive.Bind()), ), ) }) }) v.Start(":3000") } ``` -------------------------------- ### Go Component Composition and Reusability Source: https://context7.com/go-via/via/llms.txt Demonstrates creating reusable components with encapsulated state and behavior using the via framework. Shows how to create component functions, manage isolated state, handle multiple component instances, and compose components together for complex UI structures. ```go package main import ( "github.com/go-via/via" "github.com/go-via/via/h" ) func main() { v := via.New() v.Page("/", func(c *via.Context) { // Create multiple instances of the same component counterComp1 := c.Component(counterComponent) counterComp2 := c.Component(counterComponent) greetingComp := c.Component(greetingComponent) c.View(func() h.H { return h.Div( h.H1(h.Text("Counter 1")), counterComp1(), // Render first counter instance h.Hr(), h.H1(h.Text("Counter 2")), counterComp2(), // Render second counter instance h.Hr(), greetingComp(), // Render greeting component ) }) }) v.Start(":3000") } // Component function with isolated state func counterComponent(c *via.Context) { count := 0 step := c.Signal(1) increment := c.Action(func() { count += step.Int() c.Sync() }) c.View(func() h.H { return h.Div( h.P(h.Textf("Count: %d", count)), h.P(h.Span(h.Text("Step: ")), h.Span(step.Text())), h.Label( h.Text("Update Step: "), h.Input(h.Type("number"), step.Bind()), ), h.Button(h.Text("Increment"), increment.OnClick()), ) }) } func greetingComponent(c *via.Context) { greeting := c.Signal("Hello...") greetBob := c.Action(func() { greeting.SetValue("Hello Bob!") c.SyncSignals() }) greetAlice := c.Action(func() { greeting.SetValue("Hello Alice!") c.SyncSignals() }) c.View(func() h.H { return h.Div( h.P(h.Span(h.Text("Greeting: ")), h.Span(greeting.Text())), h.Button(h.Text("Greet Bob"), greetBob.OnClick()), h.Button(h.Text("Greet Alice"), greetAlice.OnClick()), ) }) } ``` -------------------------------- ### Advanced HTML utilities with Via (Go) Source: https://context7.com/go-via/via/llms.txt Shows usage of Via's HTML helper utilities for rendering raw HTML, formatted text, custom attributes, conditional content, attribute merging, and nested elements. Illustrates building complex UI structures with h package functions. Includes a toggle action to conditionally display advanced options. ```Go package main import ( "github.com/go-via/via" "github.com/go-via/via/h" ) func main() { v := via.New() v.Page("/", func(c *via.Context) { showAdvanced := false toggle := c.Action(func() { showAdvanced = !showAdvanced c.Sync() }) c.View(func() h.H { return h.Div( // Raw HTML (unescaped) h.Raw("Bold text via raw HTML"), // Formatted text (escaped) h.Textf("User count: %d, status: %s", 42, "active"), // Custom attributes h.Div( h.Attr("data-custom", "value"), h.Attr("aria-label", "Main content"), h.Text("Custom attributed element"), ), // Conditional rendering h.If(showAdvanced, h.Div( h.H3(h.Text("Advanced Options")), h.P(h.Text("This is shown conditionally")), )), // Join attributes (merge duplicate attributes) h.JoinAttrs("class", h.Div(h.Class("base")), h.Div(h.Class("extra")), ), // Multiple child elements h.Div( h.H2(h.Text("Features")), h.Ul( h.Li(h.Text("Fast")), h.Li(h.Text("Simple")), h.Li(h.Text("Pure Go")), ), ), h.Button(h.Text("Toggle Advanced"), toggle.OnClick()), ) }) }) v.Start(":3000") } ``` -------------------------------- ### Handle DOM events with Via actions (Go) Source: https://context7.com/go-via/via/llms.txt Demonstrates attaching actions to DOM events such as button clicks, input changes, and select changes using Via's Action and Signal mechanisms. Shows how to update state, synchronize the view, and execute client‑side scripts. Utilizes the via and via/h packages. ```Go package main import ( "github.com/go-via/via" "github.com/go-via/via/h" ) func main() { v := via.New() v.Page("/", func(c *via.Context) { clickCount := 0 inputValue := c.Signal("") selectValue := c.Signal("") // Action triggered on button click handleClick := c.Action(func() { clickCount++ c.Sync() }) // Action triggered on input change (debounced) handleInputChange := c.Action(func() { // This fires 200ms after user stops typing c.ExecScript(`console.log("Input changed to: " + "` + inputValue.String() + `")`) }) // Action triggered on select change handleSelectChange := c.Action(func() { c.ExecScript(`console.log("Selected: " + "` + selectValue.String() + `")`) }) c.View(func() h.H { return h.Div( h.P(h.Textf("Clicks: %d", clickCount)), // OnClick event h.Button( h.Text("Click Me"), handleClick.OnClick(), ), // OnChange event (debounced for inputs) h.Input( h.Type("text"), h.Placeholder("Type something..."), inputValue.Bind(), handleInputChange.OnChange(), ), // OnChange for select elements h.Select( selectValue.Bind(), handleSelectChange.OnClick(), h.Option(h.Value("opt1"), h.Text("Option 1")), h.Option(h.Value("opt2"), h.Text("Option 2")), h.Option(h.Value("opt3"), h.Text("Option 3")), ), ) }) }) v.Start(":3000") } ``` -------------------------------- ### Route Registration and Page Handlers in Go Source: https://context7.com/go-via/via/llms.txt Illustrates how to define routes and their corresponding page handlers in a Via application. It covers registering page routes that render HTML using a `View` function and also shows how to register custom HTTP handlers for non-page API endpoints. The handler functions receive a `Context` for managing state and UI. ```go package main import ( "github.com/go-via/via" "github.com/go-via/via/h" ) func main() { v := via.New() // Register a page route with handler function v.Page("/", func(c *via.Context) { c.View(func() h.H { return h.Div( h.H1(h.Text("Welcome to Via")), h.P(h.Text("Build reactive apps in pure Go")), ) }) }) v.Page("/about", func(c *via.Context) { c.View(func() h.H { return h.Div( h.H1(h.Text("About Page")), h.P(h.Text("This is the about page")), ) }) }) // Register custom HTTP handler for non-page routes v.HandleFunc("GET /api/status", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"status":"ok"}`)) }) v.Start(":3000") } ``` -------------------------------- ### HTML Element Composition with via/h DSL (Go) Source: https://context7.com/go-via/via/llms.txt This snippet builds type-safe, reactive HTML structures using the via/h package's DSL for server-side rendering in Go. It creates a complete registration form with inputs, labels, textarea, select, checkbox, button actions, conditional rendering, lists, tables, links, images, and styled elements. Depends on 'github.com/go-via/via' and 'github.com/go-via/via/h'; uses signals for email binding and submit action for state updates, inputting user data and outputting an interactive form view synced via Via. Limitations include static server-side generation, requiring sync calls for dynamic updates post-render. ```go package main import ( "github.com/go-via/via" "github.com/go-via/via/h" ) func main() { v := via.New() v.Page("/", func(c *via.Context) { submitted := false email := c.Signal("") submitForm := c.Action(func() { submitted = true c.Sync() }) c.View(func() h.H { return h.Div( // Headings h.H1(h.Text("Registration Form")), h.H2(h.Text("Sign up for updates")), // Form elements h.Form( h.FieldSet( h.Legend(h.Text("User Information")), // Input with label h.Label( h.Text("Email Address:"), h.Input( h.Type("email"), h.Placeholder("you@example.com"), h.Required(), email.Bind(), ), ), // Textarea h.Label( h.Text("Comments:"), h.Textarea( h.Rows("4"), h.Cols("50"), h.Placeholder("Your feedback..."), ), ), // Select dropdown h.Label( h.Text("Country:"), h.Select( h.Option(h.Value("us"), h.Text("United States")), h.Option(h.Value("uk"), h.Text("United Kingdom")), h.Option(h.Value("ca"), h.Text("Canada")), ), ), // Checkbox h.Label( h.Input(h.Type("checkbox")), h.Text("I agree to terms"), ), // Button with action h.Button( h.Type("button"), h.Text("Submit"), submitForm.OnClick(), ), ), ), // Conditional rendering h.If(submitted, h.Div( h.P(h.Text("Thank you for submitting!")), h.P(h.Text("Email: "), email.Text()), )), // Lists h.Ul( h.Li(h.Text("Feature 1")), h.Li(h.Text("Feature 2")), h.Li(h.Text("Feature 3")), ), // Table h.Table( h.THead( h.Tr( h.Th(h.Text("Name")), h.Th(h.Text("Age")), ), ), h.TBody( h.Tr( h.Td(h.Text("Alice")), h.Td(h.Text("30")), ), h.Tr( h.Td(h.Text("Bob")), h.Td(h.Text("25")), ), ), ), ), // Links and images h.A( h.Href("https://example.com"), h.Target("_blank"), h.Text("Visit Example"), ), h.Img( h.Src("/static/logo.png"), h.Alt("Logo"), h.Width("200"), ), // Styling h.Div( h.Style("color: blue; padding: 10px;"), h.Class("container"), h.ID("main-content"), h.Text("Styled content"), ), ) }) }) v.Start(":3000") } ``` -------------------------------- ### Synchronization Methods over SSE in Via (Go) Source: https://context7.com/go-via/via/llms.txt This code implements various server-to-browser update mechanisms using Server-Sent Events (SSE) in the Via framework. It demonstrates Sync() for full view and signal updates, SyncSignals() for signal-only pushes, SyncElements() for targeted DOM patches, and ExecScript() for client-side JavaScript execution. Depends on 'github.com/go-via/via' and 'github.com/go-via/via/h'; inputs are button clicks triggering actions that modify a counter and signal, outputting reactive UI changes without full page reloads. Limitations include reliance on SSE for real-time features, which may not work in all browsers. ```go package main import ( "fmt" "github.com/go-via/via" "github.com/go-via/via/h" ) func main() { v := via.New() v.Page("/", func(c *via.Context) { counter := 0 message := c.Signal("Initial message") // Action using Sync() - updates both view and signals updateAll := c.Action(func() { counter++ message.SetValue("Counter updated to " + fmt.Sprintf("%d", counter)) // Sync pushes entire view state and all changed signals c.Sync() }) // Action using SyncSignals() - only updates signals updateSignalsOnly := c.Action(func() { message.SetValue("Signal updated without view refresh") // SyncSignals only pushes changed signal values c.SyncSignals() }) // Action using SyncElements() - updates specific DOM elements updatePartial := c.Action(func() { counter++ // SyncElements patches a specific element by ID c.SyncElements( h.Div( h.ID("partial-update"), h.P(h.Textf("Partial update: %d", counter)), ), ) }) // Action using ExecScript() - runs JavaScript in browser runScript := c.Action(func() { c.ExecScript(`alert("Hello from server!");`) }) c.View(func() h.H { return h.Div( h.P(h.Textf("Counter: %d", counter)), h.P(h.Text("Message: "), message.Text()), h.Div( h.ID("partial-update"), h.P(h.Text("Partial update area")), ), h.Button(h.Text("Update All"), updateAll.OnClick()), h.Button(h.Text("Update Signals Only"), updateSignalsOnly.OnClick()), h.Button(h.Text("Partial Update"), updatePartial.OnClick()), h.Button(h.Text("Run Script"), runScript.OnClick()), ) }) }) v.Start(":3000") } ``` -------------------------------- ### Go Signal Type Conversion and Manipulation Source: https://context7.com/go-via/via/llms.txt Demonstrates type-safe signal value conversions including int, int64, uint64, float64, string, bool, and complex types. Shows how to update signal values, check for type mismatches with Err() method, and handle errors during signal manipulation. ```go package main import ( "github.com/go-via/via" "github.com/go-via/via/h" ) func main() { v := via.New() v.Page("/", func(c *via.Context) { numSignal := c.Signal(42) textSignal := c.Signal("hello") boolSignal := c.Signal(true) process := c.Action(func() { // Read signal values with type conversion intVal := numSignal.Int() // Convert to int int64Val := numSignal.Int64() // Convert to int64 uint64Val := numSignal.Uint64() // Convert to uint64 floatVal := numSignal.Float64() // Convert to float64 strVal := textSignal.String() // Convert to string boolVal := boolSignal.Bool() // Convert to bool bytesVal := textSignal.Bytes() // Convert to []byte complexVal := numSignal.Complex128() // Convert to complex128 // Update signal values (must match original type) numSignal.SetValue(intVal * 2) textSignal.SetValue(strVal + " world") boolSignal.SetValue(!boolVal) // Check for errors after SetValue with wrong type if err := numSignal.Err(); err != nil { // Handle type mismatch error c.ExecScript(`console.error("Signal error: " + "` + err.Error() + `"`) return } c.Sync() }) c.View(func() h.H { return h.Div( h.P(h.Text("Number: "), numSignal.Text()), h.P(h.Text("Text: "), textSignal.Text()), h.P(h.Text("Boolean: "), boolSignal.Text()), h.Button(h.Text("Process"), process.OnClick()), ) }) }) v.Start(":3000") } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.