Skip to content

Helpers

Helpers are utility functions accessible from templates and Go code.

These helpers are available inside HTML templates via the helper function:

Returns the base URL of the application:

<link rel="stylesheet" href='{{ helper "base_url" "/static/css/style.css" }}'>

Returns a full URL for a given route:

<a href='{{ helper "site_url" "/products/detail/5" }}'>Detail</a>

Returns a URL for static assets:

<img src='{{ helper "asset_url" "/images/logo.png" }}'>

Returns a CSS class if the current route matches:

<a href="/" class='{{ helper "is_active" "/" "active" }}'>Home</a>

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>

These functions are available in Go code:

import "goigniter/system/helpers"
// Set session data
helpers.SessionSet(c.Ctx, "user_id", "1")
// Get session data
userId := helpers.SessionGet(c.Ctx, "user_id")
// Delete session data
helpers.SessionDelete(c.Ctx, "user_id")
// Destroy entire session
helpers.SessionDestroy(c.Ctx)
// Encrypt data
token, _ := helpers.Encrypt("sensitive-data")
// Decrypt data
decrypted, _ := helpers.Decrypt(token)
// Hash a password
hash, _ := helpers.HashPassword("secret123")
// Verify a password
isValid := helpers.CheckPassword("secret123", hash)
import "goigniter/system/helpers"
// Validate email
if helpers.ValidEmail("user@example.com") {
// email is valid
}
// Validate URL
if helpers.ValidURL("https://example.com") {
// URL is valid
}
// Generate CSRF token
token := helpers.CSRFToken(c.Ctx)
// Get CSRF field name
field := helpers.CSRFFieldName()
// Validate CSRF token
if helpers.CSRFValid(c.Ctx, token) {
// valid token
}

Next: Upload Library for file uploads and image processing.