Skip to content

Commit

Permalink
Merge branch '4.3' into feature/twig-paths
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG-WIP.md

[ci skip]
  • Loading branch information
brandonkelly committed Aug 29, 2022
2 parents 079073e + 99b4401 commit a4cc8c2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
- Twig templates now have `today`, `tomorrow`, and `yesterday` global variables available to them.
- Element query date params now support passing `today`, `tomorrow`, and `yesterday`. ([#10485](https://github.com/craftcms/cms/issues/10485))
- Element queries now support passing ambiguous column names (e.g. `dateCreated`) and field handles into `select()`. ([#11790](https://github.com/craftcms/cms/pull/11790), [#11800](https://github.com/craftcms/cms/pull/11800))
- `{% cache %}` tags now store any HTML registered with `{% html %}` tags. ([#11811](https://github.com/craftcms/cms/discussions/11811))
- Control panel `.twig` templates are now prioritized over `.html`. ([#11809](https://github.com/craftcms/cms/discussions/11809), [#11840](https://github.com/craftcms/cms/pull/11840))
- `craft\helpers\Component::iconSvg()` now namespaces the SVG contents, and adds `aria-hidden="true"`. ([#11703](https://github.com/craftcms/cms/pull/11703))
- `craft\services\Search::EVENT_BEFORE_INDEX_KEYWORDS` is now cancellable by setting `$event->isValid` to `false`. ([#11705](https://github.com/craftcms/cms/discussions/11705))
Expand Down
26 changes: 18 additions & 8 deletions src/services/TemplateCaches.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function getTemplateCache(string $key, bool $global, bool $registerResour
return null;
}

[$body, $tags, $bufferedJs, $bufferedScripts, $bufferedCss, $bufferedJsFiles, $bufferedCssFiles] = array_pad($data, 7, null);
[$body, $tags, $bufferedJs, $bufferedScripts, $bufferedCss, $bufferedJsFiles, $bufferedCssFiles, $bufferedHtml] = array_pad($data, 8, null);

// If we're actively collecting element cache tags, add this cache's tags to the collection
Craft::$app->getElements()->collectCacheTags($tags);
Expand All @@ -75,6 +75,7 @@ public function getTemplateCache(string $key, bool $global, bool $registerResour
$bufferedCss ?? [],
$bufferedJsFiles ?? [],
$bufferedCssFiles ?? [],
$bufferedHtml ?? []
);
}

Expand All @@ -86,9 +87,9 @@ public function getTemplateCache(string $key, bool $global, bool $registerResour
*
* @param bool $withResources Whether JS and CSS code registered with [[\craft\web\View::registerJs()]],
* [[\craft\web\View::registerScript()]], [[\craft\web\View::registerCss()]],
* [[\craft\web\View::registerJsFile()]], and [[\craft\web\View::registerCssFile()]] should be captured and
* included in the cache. If this is `true`, be sure to pass `$withResources = true` to [[endTemplateCache()]]
* as well.
* [[\craft\web\View::registerJsFile()]], [[\craft\web\View::registerCssFile()]], and [[\craft\web\View::registerHtml()]]
* should be captured and included in the cache. If this is `true`, be sure to pass `$withResources = true`
* to [[endTemplateCache()]] as well.
* @param bool $global Whether the cache should be stored globally.
*/
public function startTemplateCache(bool $withResources = false, bool $global = false): void
Expand All @@ -107,6 +108,7 @@ public function startTemplateCache(bool $withResources = false, bool $global = f
$view->startCssBuffer();
$view->startJsFileBuffer();
$view->startCssFileBuffer();
$view->startHtmlBuffer();
}
}

Expand All @@ -120,8 +122,8 @@ public function startTemplateCache(bool $withResources = false, bool $global = f
* @param string $body The contents of the cache.
* @param bool $withResources Whether JS and CSS code registered with [[\craft\web\View::registerJs()]],
* [[\craft\web\View::registerScript()]], [[\craft\web\View::registerCss()]],
* [[\craft\web\View::registerJsFile()]], and [[\craft\web\View::registerCssFile()]] should be captured
* and included in the cache.
* [[\craft\web\View::registerJsFile()]], [[\craft\web\View::registerCssFile()]], and [[\craft\web\View::registerHtml()]]
* should be captured and included in the cache.
* @throws Exception if this is a console request and `false` is passed to `$global`
* @throws Throwable
*/
Expand All @@ -141,6 +143,7 @@ public function endTemplateCache(string $key, bool $global, ?string $duration, m
$bufferedCss = $view->clearCssBuffer();
$bufferedJsFiles = $view->clearJsFileBuffer();
$bufferedCssFiles = $view->clearCssFileBuffer();
$bufferedHtml = $view->clearHtmlBuffer();
}

// If there are any transform generation URLs in the body, don't cache it.
Expand All @@ -162,11 +165,11 @@ public function endTemplateCache(string $key, bool $global, ?string $duration, m
$bufferedCssFiles = $this->_parseExternalResourceTags($bufferedCssFiles, 'href');

if ($saveCache) {
array_push($cacheValue, $bufferedJs, $bufferedScripts, $bufferedCss, $bufferedJsFiles, $bufferedCssFiles);
array_push($cacheValue, $bufferedJs, $bufferedScripts, $bufferedCss, $bufferedJsFiles, $bufferedCssFiles, $bufferedHtml);
}

// Re-register the JS and CSS
$this->_registerResources($bufferedJs, $bufferedScripts, $bufferedCss, $bufferedJsFiles, $bufferedCssFiles);
$this->_registerResources($bufferedJs, $bufferedScripts, $bufferedCss, $bufferedJsFiles, $bufferedCssFiles, $bufferedHtml);
}

if (!$saveCache) {
Expand Down Expand Up @@ -219,6 +222,7 @@ private function _registerResources(
array $bufferedCss,
array $bufferedJsFiles,
array $bufferedCssFiles,
array $bufferedHtml,
): void {
$view = Craft::$app->getView();

Expand Down Expand Up @@ -248,6 +252,12 @@ private function _registerResources(
foreach ($bufferedCssFiles as $key => [$url, $options]) {
$view->registerCssFile($url, $options, $key);
}

foreach ($bufferedHtml as $pos => $tags) {
foreach ($tags as $key => $html) {
$view->registerHtml($html, $pos, $key);
}
}
}

/**
Expand Down
38 changes: 37 additions & 1 deletion src/web/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ class View extends \yii\web\View
*/
private array $_jsFileBuffers = [];

/**
* @var array
* @see startHtmlBuffer()
* @see clearHtmlBuffer()
*/
private array $_htmlBuffers = [];

/**
* @var array|null the registered generic `<script>` code blocks
* @see registerScript()
Expand All @@ -265,7 +272,7 @@ class View extends \yii\web\View
* @var array the registered generic HTML code blocks
* @see registerHtml()
*/
private array $_html;
private array $_html = [];

/**
* @var callable[][]
Expand Down Expand Up @@ -1152,6 +1159,35 @@ public function clearJsFileBuffer(): array|false
return $bufferedJsFiles;
}

/**
* Starts a buffer for any html tags registered with [[registerHtml()]].
*
* @since 4.3.0
*/
public function startHtmlBuffer(): void
{
$this->_htmlBuffers[] = $this->_html;
$this->_html = [];
}

/**
* Clears and ends a buffer started via [[startHtmlBuffer()]], returning any html tags that were registered
* while the buffer was active.
*
* @return array|false The html that was registered while the buffer was active or `false` if there wasn't an active buffer.
* @since 4.3.0
*/
public function clearHtmlBuffer(): array|false
{
if (empty($this->_htmlBuffers)) {
return false;
}

$bufferedHtml = $this->_html;
$this->_html = array_pop($this->_htmlBuffers);
return $bufferedHtml;
}

/**
* @inheritdoc
*/
Expand Down

0 comments on commit a4cc8c2

Please sign in to comment.