Helper Functions Reference
Unfold CMS provides a comprehensive set of PHP helper functions available globally in all template files, controllers, and Blade views.
Template Helpers
template_option($key, $default)
Get a template option value from the active template's options.json.
template_option('hero.enabled', true);
template_option('blog.posts_per_page', 12);
template_option('appearance.default_theme', 'system');
template_view($name)
Resolve the full view path for the active template.
template_view('home'); // → 'templates.default.home'
template_view('blog.index'); // → 'templates.default.blog.index'
template_view('page'); // → 'templates.default.page'
section_items($location)
Get published section items for a location. Results are cached.
$features = section_items('homepage.features');
$testimonials = section_items('homepage.testimonials');
foreach ($features as $item) {
echo $item->title;
echo $item->body;
echo $item->extra_attributes->get('icon');
}
section_setting($section, $key, $default)
Get a setting value for a settings-only section.
$title = section_setting('homepage.hero', 'title', 'Welcome');
$heading = section_setting('homepage.about', 'heading', 'About Us');
$body = section_setting('homepage.cta', 'body');
lucide_icon($name, $classes)
Render a Lucide icon as inline SVG.
{!! lucide_icon('shield', 'h-6 w-6') !!}
{!! lucide_icon('arrow-right', 'h-4 w-4 text-brand') !!}
{!! lucide_icon('check-circle', 'h-5 w-5 text-green-500') !!}
Settings Helpers
setting_get($key, $default)
Get a site setting value from the database, with a fallback default.
setting_get('app.name', 'My Site');
setting_get('social.twitter');
setting_get('comments.enabled', true);
setting_has($key)
Check if a setting exists and is truthy.
if (setting_has('comments.enabled')) {
// Comments are enabled
}
setting_set($key, $value, $type)
Set a setting value in the database.
setting_set('app.name', 'New Name', 'string');
setting_set('comments.enabled', true, 'boolean');
Setting Model Methods
use App\Models\Setting;
Setting::get('key', 'default');
Setting::set('key', 'value', 'type');
Setting::config()->get('key'); // From config cache
User Settings Helpers
user_setting($key, $default)
Get a setting value for the currently authenticated user.
$theme = user_setting('theme', 'light');
$locale = user_setting('locale', 'en');
user_setting_set($key, $value, $type)
Set a setting value for the currently authenticated user.
user_setting_set('theme', 'dark');
user_setting_set('notifications_enabled', true, 'boolean');
user_setting_has($key)
Check if the authenticated user has a specific setting.
if (user_setting_has('theme')) {
// User has a theme preference
}
Menu Helpers
menu_items($location)
Get menu items for a location. Returns a cached collection of menu items with nested children.
$headerItems = menu_items('header');
$footerItems = menu_items('footer');
foreach (menu_items('header') as $item) {
echo $item->label;
echo $item->resolved_url;
echo $item->target;
foreach ($item->allChildren as $child) {
echo $child->label;
echo $child->resolved_url;
}
}
Auth Helpers
authCheck()
Check if the current user is authenticated.
if (authCheck()) {
// User is logged in
}
authUser()
Get the current authenticated user model.
$user = authUser();
echo $user->name;
authId()
Get the current authenticated user's ID.
$userId = authId();
isAdmin()
Check if the current user has the admin or super_admin role.
if (isAdmin()) {
// Show admin features
}
isSuperAdmin()
Check if the current user has the super_admin role.
if (isSuperAdmin()) {
// Show super admin features
}
hasRole($role)
Check if the current user has a specific role.
if (hasRole('editor')) {
// Show editor features
}
canUser($permission)
Check if the current user has a specific permission.
if (canUser('edit-posts')) {
// Show edit button
}
hasAnyRole($roles)
Check if the current user has any of the specified roles.
if (hasAnyRole(['admin', 'editor'])) {
// Show management features
}
canAny($permissions)
Check if the current user has any of the specified permissions.
if (canAny(['edit-posts', 'delete-posts'])) {
// Show post actions
}
Formatting Helpers
format_date($date, $format)
Format a date with locale awareness. Uses Jalali calendar for Persian locale.
format_date($post->created_at, 'Y/m/d');
// English: "2025/03/15"
// Persian: "1403/12/25"
format_datetime($date, $dateFormat, $timeFormat)
Format a date with time.
format_datetime($post->created_at, 'Y/m/d', 'H:i');
// "2025/03/15 14:30"
format_duration($seconds, $short)
Format a duration in seconds to a human-readable string.
format_duration(3600); // "1 hour"
format_duration(3600, short: true); // "1h"
format_duration(90); // "1 minute 30 seconds"
format_bytes($bytes, $precision)
Format bytes into a human-readable string.
format_bytes(1024); // "1 KB"
format_bytes(1048576); // "1 MB"
format_bytes(1536, 1); // "1.5 KB"
str_rlimit($string, $limit, $end)
Limit string length with UTF-8 support, preserving word boundaries.
str_rlimit($post->body, 120); // "First 120 chars at word boundary..."
str_rlimit($post->title, 50, ' →'); // "Truncated title →"
JSON-LD Schema Helpers
schema_organization()
Generate Organization schema for the homepage.
$schema = schema_organization();
// Returns array with @type, name, url, logo, sameAs
schema_website()
Generate WebSite schema with SearchAction for Google sitelinks.
$schema = schema_website();
// Returns array with @type, name, url, potentialAction
schema_article($post)
Generate Article schema for a blog post.
$schema = schema_article($post);
// Returns array with @type, headline, description, image, datePublished, author, publisher
schema_webpage($page)
Generate WebPage schema for a static page.
$schema = schema_webpage($page);
// Returns array with @type, name, description, url
schema_breadcrumb_list($items)
Generate BreadcrumbList schema from an array of breadcrumb items.
$schema = schema_breadcrumb_list([
['name' => 'Home', 'url' => url('/')],
['name' => 'Blog', 'url' => route('blog.index')],
['name' => $post->title, 'url' => url()->current()],
]);
Respond Helper
The Respond class provides standardized JSON responses for API endpoints.
Success Responses
Respond::success($data, 'Operation successful');
Respond::created($data, 'Resource created');
Respond::noContent('Resource deleted');
Error Responses
Respond::error('Something went wrong', 500);
Respond::validationError($errors, 'Validation failed');
Respond::notFound('Resource not found');
Respond::unauthorized('Please log in');
Respond::forbidden('Access denied');
Respond::rateLimited('Too many requests');
Response Structure
All JSON responses follow this structure:
{
"success": true,
"message": "Operation successful",
"data": { ... },
"errors": null
}
Laravel Helpers
In addition to the CMS helpers above, all Laravel built-in helpers are available — including asset(), url(), route(), config(), e(), Str::limit(), collect(), and more.
Related
- Blade Directives — Complete Blade directive reference
- Template Development — Full template development guide
- Configuration Files — JSON schema reference