Skip to content

Commit 1d3cf21

Browse files
authored
Fix Antlers Array Access (#2663)
1 parent 130464b commit 1d3cf21

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed

src/Exceptions/ArrayKeyNotFoundException.php renamed to src/View/Antlers/ArrayKeyNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Statamic\Exceptions;
3+
namespace Statamic\View\Antlers;
44

55
class ArrayKeyNotFoundException extends \Exception
66
{

src/View/Antlers/Parser.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use ReflectionProperty;
1515
use Statamic\Contracts\Data\Augmentable;
1616
use Statamic\Contracts\Query\Builder;
17-
use Statamic\Exceptions\ArrayKeyNotFoundException;
1817
use Statamic\Facades\Config;
1918
use Statamic\Fields\LabeledValue;
2019
use Statamic\Fields\Value;
@@ -816,7 +815,7 @@ public function processCondition($condition, $data, $isTagPair = true)
816815
// also pass in the current callback (for later processing callback tags); also setting
817816
// $ref so that we can use it within the anonymous function
818817
$ref = $this;
819-
$condition = $this->preg_replace_callback('/(\b'.$this->variableRegex.'[\b\]])/', function ($match) use ($ref) {
818+
$condition = $this->preg_replace_callback('/\b('.$this->variableRegex.'\b]?)/', function ($match) use ($ref) {
820819
return $ref->processConditionVar($match);
821820
}, $condition);
822821

@@ -980,7 +979,7 @@ public function injectNoparse($text)
980979
*/
981980
public function processConditionVar($match)
982981
{
983-
$var = trim(is_array($match) ? $match[0] : $match);
982+
$var = is_array($match) ? $match[0] : $match;
984983

985984
if (in_array(strtolower($var), ['true', 'false', 'null', 'or', 'and']) or
986985
strpos($var, '__cond_str') === 0 or

tests/View/Antlers/ParserTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,87 @@ public function it_automatically_augments_augmentable_objects_when_looping_with_
18961896
(string) Antlers::parse('{{ augmentables limit="1" }}<{{ one }}><{{ two }}>{{ /augmentables }}', ['augmentables' => $loop])
18971897
);
18981898
}
1899+
1900+
/** @test */
1901+
public function it_uses_tags_with_single_part_in_conditions()
1902+
{
1903+
(new class extends Tags {
1904+
public static $handle = 'truthy';
1905+
1906+
public function index()
1907+
{
1908+
return true;
1909+
}
1910+
})::register();
1911+
1912+
(new class extends Tags {
1913+
public static $handle = 'falsey';
1914+
1915+
public function index()
1916+
{
1917+
return false;
1918+
}
1919+
})::register();
1920+
1921+
$this->assertEquals('yes', $this->parse('{{ if {truthy} }}yes{{ else }}no{{ /if }}'));
1922+
$this->assertEquals('yes', $this->parse('{{ if {truthy} == true }}yes{{ else }}no{{ /if }}'));
1923+
$this->assertEquals('no', $this->parse('{{ if {truthy} == false }}yes{{ else }}no{{ /if }}'));
1924+
$this->assertEquals('no', $this->parse('{{ if {falsey} }}yes{{ else }}no{{ /if }}'));
1925+
$this->assertEquals('no', $this->parse('{{ if {falsey} == true }}yes{{ else }}no{{ /if }}'));
1926+
$this->assertEquals('yes', $this->parse('{{ if {falsey} == false }}yes{{ else }}no{{ /if }}'));
1927+
}
1928+
1929+
/** @test */
1930+
public function it_uses_tags_with_multiple_parts_in_conditions()
1931+
{
1932+
(new class extends Tags {
1933+
public static $handle = 'truthy';
1934+
1935+
public function test()
1936+
{
1937+
return true;
1938+
}
1939+
})::register();
1940+
1941+
(new class extends Tags {
1942+
public static $handle = 'falsey';
1943+
1944+
public function test()
1945+
{
1946+
return false;
1947+
}
1948+
})::register();
1949+
1950+
$this->assertEquals('yes', $this->parse('{{ if {truthy:test} }}yes{{ else }}no{{ /if }}'));
1951+
$this->assertEquals('yes', $this->parse('{{ if {truthy:test} == true }}yes{{ else }}no{{ /if }}'));
1952+
$this->assertEquals('no', $this->parse('{{ if {truthy:test} == false }}yes{{ else }}no{{ /if }}'));
1953+
$this->assertEquals('no', $this->parse('{{ if {falsey:test} }}yes{{ else }}no{{ /if }}'));
1954+
$this->assertEquals('no', $this->parse('{{ if {falsey:test} == true }}yes{{ else }}no{{ /if }}'));
1955+
$this->assertEquals('yes', $this->parse('{{ if {falsey:test} == false }}yes{{ else }}no{{ /if }}'));
1956+
}
1957+
1958+
/** @test */
1959+
public function it_does_stuff_in_issue_2537()
1960+
{
1961+
$template = '{{ if noindex || segment_1 == "mobile" || get:page > 0 }}yes{{ else }}no{{ /if }}';
1962+
1963+
$this->assertEquals('yes', $this->parse($template, ['noindex' => true]));
1964+
}
1965+
1966+
/** @test */
1967+
public function it_does_stuff_in_issue_2456()
1968+
{
1969+
$template = '{{ if publication_venue:publication_venue_types:slug !== "journal" and publication_venue:first_year }}yes{{ else }}no{{ /if }}';
1970+
1971+
$this->assertEquals('yes', $this->parse($template, [
1972+
'publication_venue' => [
1973+
'first_year' => true,
1974+
'publication_venue_types' => [
1975+
'slug' => 'notjournal',
1976+
],
1977+
],
1978+
]));
1979+
}
18991980
}
19001981

19011982
class NonArrayableObject

0 commit comments

Comments
 (0)