Skip to content

Inconsistent behaviour for variables set in a partial #8175

Description

@hybridvision

Bug description

Hello!

I've been testing out Statamic for a small project and I think I've discovered a bug with variables set inside a partial. Originally I was using the excellent Peak starter kit when I saw this bug but I've since reproduced it on a blank installation.

I watched Jack's informative video, which explains how the new Antlers runtime allows more flexibility with variables thanks to the AST parser. This seems to confirm that what I'm attempting should work by design.

When including a specific partial, I want it to influence the main layout's behaviour (I set a variable in the partial and the layout checks for this variable). This works, but not consistently.

If my included partial just sets the variable and doesn't do anything else, the variable isn't available elsewhere. However, I discovered if I include any of the following snippets in my partial, suddenly my variable is available in the main layout:

  • {{ dump }}
  • {{ partial:whatever }}
  • or even... {{ partial:if_exists src="neverexists" }}

It seems that the inclusion of the dump or partial tags change the way the variable is handled. 🤔

How to reproduce

Minimal setup to reproduce this problem:

  1. Install a blank site from the CLI.
  2. Create a page with the default template (template: default in the front-matter).
  3. Create new partial in resources/views/_test.antlers.html with the following content:
<!-- _test.antlers.html -->
{{ $var_from_partial = "#### VAR SET in _test PARTIAL ####" }}
  1. Update resources/views/default.antlers.html with the following:
<!-- default.antlers.html -->
{{ partial:test }}
<b>default.antlers.html: {{ $var_from_partial ?? '*** NO PARTIAL VAR ***' }}</b><br>

{{ content }}
  1. Add the following line anywhere in resources/views/layout.antlers.html:
<!-- layout.antlers.html -->
<b>layout.antlers.html: {{ $var_from_partial ?? '*** NO PARTIAL VAR ***' }}</b><br>
  1. Visit the page created in Step 2. There will be two instances of the *** NO PARTIAL VAR *** text because the variable doesn't exist in either scope.

  2. Now edit resources/views/_test.antlers.html and add a {{ dump }} tag or a partial (eg. {{ partial:whatever }} or {{ partial:if_exists src="neverexists" }})

  3. Refresh the page and observe that now the main layout is receiving the value of $var_from_partial. Curiously, the default.antlers.html still doesn't see the variable. I can live with this for now since I only really need it in the layout / header partial but from everything I've understood, it should be available in the default template too, right?

Logs

N/A

Environment

Environment
Application Name: Statamic
Laravel Version: 10.11.0
PHP Version: 8.2.5
Composer Version: 2.5.5
Environment: local
Debug Mode: ENABLED
URL: statamic-vanilla.test
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: statamic
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Statamic
Addons: 0
Antlers: runtime
Stache Watcher: Enabled
Static Caching: Disabled
Version: 4.1.3 Solo

Installation

Fresh statamic/statamic site via CLI

Antlers Parser

runtime (new)

Additional details

To give some extra context on what I'm trying to achieve: I want to be able to change the body CSS class in the main layout (or maybe a CSS class in the header partial) when a particular partial is included.

More specifically, I have a hero block that is included via Peak's page builder and when it is present and in the first position, the header should switch to being absolutely positioned, allowing it to overlap the hero block. I don't know if my explanation makes sense but this kind of hero block that sits under a transparent header/nav is quite common. On other pages without the hero block, the header should maintain its place in the flow with the correct spacing and background colour.

Lastly, I did a bunch of searches before posting this and couldn't find anything quite like it. The closest I found was #7587 but it is coming from a different angle and is currently unresolved.

Thanks for reading! 👋

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions