From e02f7d95dbecb28584ee8d3c78b2b6c26539b6e3 Mon Sep 17 00:00:00 2001 From: goldnead Date: Fri, 21 Nov 2025 11:56:18 +0100 Subject: [PATCH 1/2] Add 'when' param support to Toc tag and tests The Toc tag now respects a 'when' parameter, returning an empty array if set to false. Added a unit test to verify correct behavior when 'when' is true or false. --- src/Tags/Toc.php | 6 +++++ tests/Unit/RecursionTest.php | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 tests/Unit/RecursionTest.php diff --git a/src/Tags/Toc.php b/src/Tags/Toc.php index 4468931..ab77445 100644 --- a/src/Tags/Toc.php +++ b/src/Tags/Toc.php @@ -26,6 +26,12 @@ class Toc extends Tags */ public function index() { + if ($this->params->get('when', true) === false) { + return []; + } + + // get the supported header-levels + // get the supported header-levels $depth = $this->params->int("depth", 3); $start = $this->params->get('from', "h1"); diff --git a/tests/Unit/RecursionTest.php b/tests/Unit/RecursionTest.php new file mode 100644 index 0000000..d0aa89b --- /dev/null +++ b/tests/Unit/RecursionTest.php @@ -0,0 +1,50 @@ +put('toc', \Goldnead\StatamicToc\Tags\Toc::class); + } + + /** @test */ + public function it_renders_toc_with_when_param() + { + $content = '

Heading 1

Heading 2

'; + + $template = <<<'EOT' +
    + {{ toc field="content" :when="show_toc" }} +
  1. + {{ toc_title }} + {{ if children }} +
      + {{ *recursive children* }} +
    + {{ /if }} +
  2. + {{ /toc }} +
+EOT; + + $data = [ + 'content' => $content, + 'show_toc' => true, + ]; + + $output = (string) Antlers::parse($template, $data); + + $this->assertStringContainsString('Heading 1', $output); + + // Test false + $data['show_toc'] = false; + $output = (string) Antlers::parse($template, $data); + $this->assertStringNotContainsString('Heading 1', $output); + } +} From d5ee0ccf1c21a58a4874d38eb473b26296b5ab57 Mon Sep 17 00:00:00 2001 From: goldnead Date: Sat, 22 Nov 2025 17:13:42 +0100 Subject: [PATCH 2/2] Update tests.yaml --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 63d6044..e7a5032 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -12,7 +12,7 @@ jobs: fail-fast: false matrix: php: [8.1] - statamic: [4.0.*, 5.0.*] + statamic: [4.6.*, 5.*] os: [ubuntu-latest] laravel: [10.*] testbench: [8.*]