getActiveTheme();
// Use our new theme asset route
return route('theme.asset', [
'theme' => $activeTheme,
'path' => $path,
]);
}
}
if (! function_exists('css')) {
function css(string $path, array $attributes = []): HtmlString
{
$push = $attributes['push'] ?? false;
unset($attributes['push']);
$url = theme_asset($path);
$attrString = '';
foreach ($attributes as $key => $value) {
$attrString .= is_bool($value) ? ($value ? " {$key}" : '') : " {$key}=\"$value\"";
}
$tag = "";
if ($push) {
view()->startPush('styles', $tag);
return new HtmlString('');
}
return new HtmlString($tag);
}
}
if (! function_exists('js')) {
function js(string $path, array $attributes = []): HtmlString
{
$push = $attributes['push'] ?? false;
unset($attributes['push']);
$url = theme_asset($path);
$attrString = '';
foreach ($attributes as $key => $value) {
$attrString .= is_bool($value) ? ($value ? " {$key}" : '') : " {$key}=\"$value\"";
}
$tag = "";
if ($push) {
view()->startPush('scripts', $tag);
return new HtmlString('');
}
return new HtmlString($tag);
}
}
if (! function_exists('sw_media')) {
/**
* Helper to render a file (image, audio, video, or link).
*/
function sw_media(string|int|Media $file, array $attributes = []): HtmlString
{
return sw_file($file, $attributes);
}
}
if (! function_exists('sw_file')) {
function sw_file(string|int|Media $file, array $attributes = []): HtmlString
{
$path = '';
$url = '';
if ($file instanceof Media) {
$path = $file->filename;
$url = route('media.jit', ['path' => $file->filename]);
} elseif (is_int($file) || (is_string($file) && is_numeric($file))) {
$media = Media::find($file);
if ($media) {
$path = $media->filename;
$url = route('media.jit', ['path' => $media->filename]);
} else {
return new HtmlString('');
}
} else {
$path = $file;
// Detect if the path is a theme asset or a media file
if (str_starts_with($path, 'media/')) {
$mediaPath = str_replace('media/', '', $path);
$url = route('media.jit', ['path' => $mediaPath]);
} else {
$url = theme_asset($path);
}
}
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
// Handle Image sizing via JIT if it's an image
if (in_array($extension, ['jpg', 'jpeg', 'png', 'gif', 'webp', 'avif'])) {
$jitParams = ['w', 'h', 'fit', 'q', 'fm', 'fp-x', 'fp-y'];
$query = [];
foreach ($jitParams as $param) {
if (isset($attributes[$param])) {
$query[$param] = $attributes[$param];
unset($attributes[$param]);
}
}
if (! empty($query)) {
$url .= (str_contains($url, '?') ? '&' : '?') . http_build_query($query);
}
$attrString = '';
foreach ($attributes as $key => $value) {
$attrString .= " {$key}=\"$value\"";
}
return new HtmlString("
");
}
// Audio
if (in_array($extension, ['mp3', 'wav', 'ogg'])) {
$attrString = '';
foreach ($attributes as $key => $value) {
$attrString .= is_bool($value) ? ($value ? " {$key}" : '') : " {$key}=\"$value\"";
}
return new HtmlString("");
}
// Video
if (in_array($extension, ['mp4', 'webm', 'ogv'])) {
$attrString = '';
foreach ($attributes as $key => $value) {
$attrString .= is_bool($value) ? ($value ? " {$key}" : '') : " {$key}=\"$value\"";
}
return new HtmlString("");
}
// Default (Link)
$attrString = '';
$text = $attributes['text'] ?? basename($path);
unset($attributes['text']);
foreach ($attributes as $key => $value) {
$attrString .= " {$key}=\"$value\"";
}
return new HtmlString("$text");
}
}