37 lines
838 B
PHP
37 lines
838 B
PHP
<?php
|
|
|
|
namespace App\View\Traits;
|
|
|
|
use Illuminate\View\ComponentAttributeBag;
|
|
|
|
trait RendersAlpine
|
|
{
|
|
public function asAlpineString(ComponentAttributeBag $attributes, string $tagName): string
|
|
{
|
|
$rawTag = ':' . $tagName;
|
|
|
|
if ($attributes->has($rawTag)) {
|
|
return $attributes->get($rawTag);
|
|
}
|
|
|
|
$output = e($this->{$tagName});
|
|
return str($output)->swap([
|
|
'`' => '\\`',
|
|
'$' => '\\$',
|
|
'{' => '\\{',
|
|
'}' => '\\}',
|
|
])->wrap('`');
|
|
}
|
|
|
|
public function asAlpineBool(ComponentAttributeBag $attributes, string $tagName): string
|
|
{
|
|
$rawTag = ':' . $tagName;
|
|
|
|
if ($attributes->has($rawTag)) {
|
|
return $attributes->get($rawTag);
|
|
}
|
|
|
|
return $this->{$tagName} ? 'true' : 'false';
|
|
}
|
|
}
|