New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page variables override parameters passed to partials #1189

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

Comments

Projects
None yet
4 participants
@Nevcairiel

Nevcairiel commented Jan 19, 2017

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

This comment has been minimized.

Nevcairiel commented Feb 3, 2017

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

This comment has been minimized.

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

This comment has been minimized.

Member

jasonvarga commented Feb 15, 2017

Are you using 2.5.3?

@curtisblackwell

This comment has been minimized.

curtisblackwell commented Feb 15, 2017

Yes.

@curtisblackwell

This comment has been minimized.

curtisblackwell commented Feb 15, 2017

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

This comment has been minimized.

Member

jasonvarga commented Feb 15, 2017

Yeah I don't think I saw that.

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

@jasonvarga

This comment has been minimized.

Member

jasonvarga commented Feb 15, 2017

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

This comment has been minimized.

edalzell commented Mar 12, 2017

this can be closed I think, right @jasonvarga?

@jasonvarga jasonvarga closed this Mar 16, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment