Helpers
Helpers are utility functions accessible from templates and Go code.
Template Helpers
Section titled “Template Helpers”These helpers are available inside HTML templates via the helper function:
base_url
Section titled “base_url”Returns the base URL of the application:
<link rel="stylesheet" href='{{ helper "base_url" "/static/css/style.css" }}'>site_url
Section titled “site_url”Returns a full URL for a given route:
<a href='{{ helper "site_url" "/products/detail/5" }}'>Detail</a>asset_url
Section titled “asset_url”Returns a URL for static assets:
<img src='{{ helper "asset_url" "/images/logo.png" }}'>is_active
Section titled “is_active”Returns a CSS class if the current route matches:
<a href="/" class='{{ helper "is_active" "/" "active" }}'>Home</a>excerpt
Section titled “excerpt”Truncates text to a specified length with ellipsis:
<p>{{ helper "excerpt" .Description 100 }}</p>Formats a time value:
<p>{{ helper "date" .CreatedAt "2006-01-02" }}</p>Converts newlines to <br> tags:
<p>{{ helper "nl2br" .Description }}</p>Code Helpers
Section titled “Code Helpers”These functions are available in Go code:
Session
Section titled “Session”import "goigniter/system/helpers"
// Set session datahelpers.SessionSet(c.Ctx, "user_id", "1")
// Get session datauserId := helpers.SessionGet(c.Ctx, "user_id")
// Delete session datahelpers.SessionDelete(c.Ctx, "user_id")
// Destroy entire sessionhelpers.SessionDestroy(c.Ctx)Encryption
Section titled “Encryption”// Encrypt datatoken, _ := helpers.Encrypt("sensitive-data")
// Decrypt datadecrypted, _ := helpers.Decrypt(token)
// Hash a passwordhash, _ := helpers.HashPassword("secret123")
// Verify a passwordisValid := helpers.CheckPassword("secret123", hash)Validation
Section titled “Validation”import "goigniter/system/helpers"
// Validate email // email is valid}
// Validate URLif helpers.ValidURL("https://example.com") { // URL is valid}CSRF Protection
Section titled “CSRF Protection”// Generate CSRF tokentoken := helpers.CSRFToken(c.Ctx)
// Get CSRF field namefield := helpers.CSRFFieldName()
// Validate CSRF tokenif helpers.CSRFValid(c.Ctx, token) { // valid token}Next: Upload Library for file uploads and image processing.