Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Page variables override parameters passed to partials #1189

Closed
Nevcairiel opened this issue Jan 19, 2017 · 8 comments
Closed

Page variables override parameters passed to partials #1189

Nevcairiel opened this issue Jan 19, 2017 · 8 comments

Comments

@Nevcairiel
Copy link

Expected behaviour

Passing explicit parameters to partials should override any page variables of the same name.
This would also be consistent with the general idea of the cascade, to have more specific data override the "top-level" data.

Actual behaviour

Passing variables to a partial that have a page-level equivalent is ineffective, and the top level page variable is always sourced.

Steps to reproduce

  1. Create a simple partial with a parameter with the same name as a page-level variable, ie.
    partials/header.html: <h1>{{ title }}</h1>
  2. Try to pass a custom string to it from a template, eg.
    {{ partial:header title="Foo Bar" }}

Server configuration

Operating system:
Windows 10

Web server:
Apache 2.4

PHP version:
5.6.29

Statamic version:
2.1.19

Updated from an older Statamic or fresh install:
Fresh install

List of installed addons:
None

Solution

This behavior can be mitigated in PartialTags::__call by swapping the operands to the array_merge call in line 32, so that the original contents of the $variables array is preserved over the context content.

@Nevcairiel
Copy link
Author

The issue persist in 2.5.2, and the same work-around can still be applied in the code. Favoring the parameters array over the context array to make sure any explicitly listed parameters do get used.

@curtisblackwell
Copy link

curtisblackwell commented Feb 15, 2017

Should front matter in partials override passed params?

This is not the behavior in previous releases, but it is now with 2.5.3. I've been using front matter to set defaults that I can override when I call the partial.

Given this partial:

---
p_cta_class: button
---

<a href="{{ p_url }}" class="{{ p_cta_class }}">{{ p_cta_text }}</a>

This template:

{{ partial:cta p_cta_class="button v-white" }}

The output is:

<a href="/support-us/corporate-sponsor" class="button">In reprehenderit</a>

@jasonvarga
Copy link
Member

Are you using 2.5.3?

@curtisblackwell
Copy link

Yes.

@curtisblackwell
Copy link

I added this (probably after you read your notification):

This is not the behavior in previous releases, but it is now with 2.5.3. I've been using front matter to set defaults that I can override when I call the partial.

@jasonvarga
Copy link
Member

Yeah I don't think I saw that.

Anyway, it should work as you expect. Just tested here and works fine. Hmm. 🤔

@jasonvarga
Copy link
Member

I misread that comment, and implemented it incorrectly. One too few cups of coffee.

Fixed for 2.5.4

To clarify the cascade:

  1. Page variables
  2. Partial front matter. (In Curtis' example, setting "defaults" is a perfect use case)
  3. Partial tag parameters. (Again, overriding the defaults from the front matter, or just overriding page vars)

In 2.5.3, number 2 and 3 were mixed around.

@jasonvarga jasonvarga reopened this Feb 15, 2017
@edalzell
Copy link

this can be closed I think, right @jasonvarga?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants