Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Providers/ViewServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\View\View;
use Statamic\Contracts\View\Antlers\Parser as ParserContract;
use Statamic\Facades\Site;
use Statamic\Fields\Value;
use Statamic\Statamic;
use Statamic\View\Antlers\Engine;
use Statamic\View\Antlers\Language\Analyzers\NodeTypeAnalyzer;
Expand All @@ -22,6 +23,7 @@
use Statamic\View\Antlers\Language\Utilities\StringUtilities;
use Statamic\View\Blade\AntlersBladePrecompiler;
use Statamic\View\Blade\StatamicTagCompiler;
use Statamic\View\Blade\ValueEchoHandler;
use Statamic\View\Cascade;
use Statamic\View\Debugbar\AntlersProfiler\PerformanceCollector;
use Statamic\View\Debugbar\AntlersProfiler\PerformanceTracer;
Expand Down Expand Up @@ -435,6 +437,8 @@ public function boot()
return AntlersBladePrecompiler::compile($content);
});

Blade::stringable(Value::class, [ValueEchoHandler::class, 'handle']);

View::macro('withoutExtractions', function () {
if ($this->getEngine() instanceof Engine) {
$this->getEngine()->withoutExtractions();
Expand Down
31 changes: 31 additions & 0 deletions src/View/Blade/ValueEchoHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Statamic\View\Blade;

use Statamic\Facades\Antlers;
use Statamic\Facades\Cascade;
use Statamic\Fields\Value;
use Statamic\View\Antlers\Language\Runtime\GlobalRuntimeState;

class ValueEchoHandler
{
public static function handle(Value $value): mixed
{
if (! $value->shouldParseAntlers()) {
return $value;
}

$data = Cascade::toArray() ?: Cascade::hydrate()->toArray();

$wasEvaluatingUserData = GlobalRuntimeState::$isEvaluatingUserData;
GlobalRuntimeState::$isEvaluatingUserData = true;
GlobalRuntimeState::$userContentEvalState = [$value, null];

try {
return $value->antlersValue(Antlers::parser(), $data);
} finally {
GlobalRuntimeState::$userContentEvalState = null;
GlobalRuntimeState::$isEvaluatingUserData = $wasEvaluatingUserData;
}
}
}
60 changes: 60 additions & 0 deletions tests/View/Blade/AntlersValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Tests\View\Blade;

use Illuminate\Support\Facades\Blade;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Cascade;
use Statamic\Fields\Field;
use Statamic\Fields\Value;
use Statamic\Fieldtypes\Markdown;
use Statamic\Fieldtypes\Textarea;
use Tests\TestCase;

class AntlersValueTest extends TestCase
{
#[Test]
public function it_parses_antlers_in_a_value_when_the_field_has_antlers_enabled()
{
Cascade::set('name', 'World');

$value = $this->value(new Textarea, 'Hello {{ name }}', ['antlers' => true]);

$this->assertSame('Hello World', Blade::render('{!! $text !!}', ['text' => $value]));
}

#[Test]
public function it_doesnt_parse_antlers_in_a_value_when_the_field_has_antlers_disabled()
{
Cascade::set('name', 'World');

$value = $this->value(new Textarea, 'Hello {{ name }}', ['antlers' => false]);

$this->assertSame('Hello {{ name }}', Blade::render('{!! $text !!}', ['text' => $value]));
}

#[Test]
public function it_parses_antlers_before_augmenting_a_markdown_value()
{
Cascade::set('name', 'World');

$value = $this->value(new Markdown, '# Hello {{ name }}', ['antlers' => true]);

$this->assertSame("<h1>Hello World</h1>\n", Blade::render('{!! $text !!}', ['text' => $value]));
}

#[Test]
public function it_escapes_parsed_antlers_in_an_escaped_echo()
{
Cascade::set('name', '<b>World</b>');

$value = $this->value(new Textarea, 'Hello {{ name }}', ['antlers' => true]);

$this->assertSame('Hello &lt;b&gt;World&lt;/b&gt;', Blade::render('{{ $text }}', ['text' => $value]));
}

private function value($fieldtype, $raw, array $config)
{
return new Value($raw, 'text', $fieldtype->setField(new Field('text', $config)));
}
}
Loading