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
1 change: 1 addition & 0 deletions src/Providers/ExtensionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class ExtensionServiceProvider extends ServiceProvider
Tags\GetSite::class,
Tags\Glide::class,
Tags\In::class,
Tags\IncludeTag::class,
Tags\Increment::class,
Tags\Installed::class,
Tags\Is::class,
Expand Down
4 changes: 4 additions & 0 deletions src/Providers/ViewServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Statamic\Contracts\View\Antlers\Parser as ParserContract;
use Statamic\Facades\Site;
use Statamic\Statamic;
use Statamic\StaticCaching\NoCache\Region;
use Statamic\Tags\IncludeTag;
use Statamic\View\Antlers\Engine;
use Statamic\View\Antlers\Language\Analyzers\NodeTypeAnalyzer;
use Statamic\View\Antlers\Language\Runtime\Debugging\GlobalDebugManager;
Expand Down Expand Up @@ -425,6 +427,8 @@ public function boot()
{
ViewFactory::addNamespace('compiled__views', storage_path('framework/views'));

Region::preserveContextKeys(IncludeTag::VIEW_DATA_KEYS);

$this->registerBladeDirectives();

Blade::precompiler(function ($content) {
Expand Down
28 changes: 16 additions & 12 deletions src/StaticCaching/NoCache/Region.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ abstract class Region
protected $context = [];
protected $session;

protected static $preservedContextKeys = [];

public static function preserveContextKeys(array $keys): void
{
static::$preservedContextKeys = array_unique(array_merge(static::$preservedContextKeys, $keys));
}

public function setSession(Session $session)
{
$this->session = $session;
Expand All @@ -26,21 +33,18 @@ public function context(): array
return $this->context;
}

protected function filterContext(array $context)
public static function filterCacheable(array $context): array
{
$context = collect($context)
->reject(fn ($value, $key) => str_starts_with((string) $key, '__'))
->reject(fn ($value, $key) => in_array($key, ['app', 'errors', 'resolve', 'resolveComponentsUsing', 'forgetComponentsResolver', 'forgetFactory', 'flushCache', 'constructor']))
->map(function ($value, $key) {
if ($value instanceof InvokableComponentVariable) {
return $value->resolveDisplayableValue();
}

return $value;
})
return collect($context)
->reject(fn ($value, $key) => str_starts_with((string) $key, '__') && ! in_array($key, static::$preservedContextKeys))
->reject(fn ($value, $key) => in_array($key, ['app', 'errors', 'obLevel', 'resolve', 'resolveComponentsUsing', 'forgetComponentsResolver', 'forgetFactory', 'flushCache', 'constructor']))
->map(fn ($value) => $value instanceof InvokableComponentVariable ? $value->resolveDisplayableValue() : $value)
->all();
}

return $this->arrayRecursiveDiff($context, $this->session->cascade());
protected function filterContext(array $context)
{
return $this->arrayRecursiveDiff(static::filterCacheable($context), $this->session->cascade());
}

public function fragmentData(): array
Expand Down
92 changes: 92 additions & 0 deletions src/Tags/Concerns/RendersViews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Statamic\Tags\Concerns;

use Illuminate\Support\HtmlString;

trait RendersViews
{
protected function viewName($partial)
{
$partial = str_replace('/', '.', $partial);

if (view()->exists($underscored = $this->underscoredViewName($partial))) {
return $underscored;
}

if (view()->exists($subdirectoried = 'partials.'.$partial)) {
return $subdirectoried;
}

if (view()->exists($underscored_subdirectoried = 'partials.'.$this->underscoredViewName($partial))) {
return $underscored_subdirectoried;
}

return $partial;
}

protected function underscoredViewName($partial)
{
$bits = collect(explode('.', $partial));

$last = $bits->pull($bits->count() - 1);

return $bits->implode('.').'._'.$last;
}

protected function shouldRender(): bool
{
if ($this->params->has('when')) {
return $this->params->bool('when');
}

if ($this->params->has('unless')) {
return ! $this->params->bool('unless');
}

return true;
}

protected function getSlotContent(): HtmlString|string
{
$content = trim($this->parse());

if ($this->isAntlersBladeComponent()) {
return new HtmlString($content);
}

return $content;
}

/**
* The {{ exists }} tag.
*
* Returns true if the view exists, false otherwise. If the src parameter is
* omitted, it acts like the user is trying to use a view named "exists".
*/
public function exists()
{
if (! $view = $this->params->get('src')) {
return $this->wildcard('exists');
}

return view()->exists($this->viewName($view));
}

/**
* The {{ if_exists }} tag.
*
* Renders the view if it exists, and outputs nothing otherwise. If the src parameter
* is omitted, it acts like the user is trying to use a view named "if_exists".
*/
public function ifExists()
{
if (! $view = $this->params->get('src')) {
return $this->wildcard('if_exists');
}

if (view()->exists($this->viewName($view))) {
return $this->render($view);
}
}
}
Loading
Loading