Make WordPress Core

Opened 11 years ago

Closed 11 years ago

Last modified 10 years ago

#36187 closed enhancement (duplicate)

Pass variables scoped to templates

Reported by: enrico.sorcinelli Owned by:
Priority: normal Milestone:
Component: Themes Version:
Severity: normal Keywords:
Cc: Focuses: template

Description

This patch allows to pass variables scoped to template files.
The patch affects load_template, locate_template and get_* template family functions.
For example:

get_template_part('parts/template', '', array( 'foo' => 'baz' ) );

and in parts/template.php:

echo $params['foo'];

or:

get_header('header', '', array( 'foo' => 'baz') );

and so on.

I know, there are a lot ot threads, proposals, posts in the past that have requested a similar functionality and I think that is the time to examine again this for the 4.5 release.

Moreover, even if there are workarounds for that, like using globals, or include(locate_template()) pattern, the patch would really to clean up those approaches.

In my opinion, abuse of globals is not too good practice and multiple times the people just need to repeat bits of data on partial content (for example loops which have considerable template HTML). And they would like to reuse this template components across the site in more than one place and or in different themes creating a sort of their own templates codebase repository.

---

PS: Since it seems that there is no a clean way to test locate_template() (and consequently get_* template functions) with a different theme other than twentyfifteen even after switching a theme (that is, themes inside tests/phpunit/data directory), the only unit test currently I added is for load_template().

Attachments (2)

36187.patch (7.2 KB ) - added by enrico.sorcinelli 11 years ago.
36187.2.patch (9.6 KB ) - added by enrico.sorcinelli 10 years ago.
Updated to trunk. This patch replaces the previous ones

Download all attachments as: .zip

Change History (6)

#1 @ocean90
11 years ago

  • Component GeneralThemes
  • Milestone Awaiting Review
  • Resolutionduplicate
  • Status newclosed
  • Version trunk

Hello @enrico.sorcinelli, thanks for your ticket and patch, but it's actually a duplicate of #21676.

Last edited 11 years ago by ocean90 (previous) (diff)

@enrico.sorcinelli
10 years ago

Updated to trunk. This patch replaces the previous ones

#2 @enrico.sorcinelli
10 years ago

  • Versiontrunk

I just updated the patch to trunk.
I would to get back on track with this long awaited and requested enhancement.
See comment:58:ticket:21676

#3 @SergeyBiryukov
10 years ago

  • Version trunk

Hi @enrico.sorcinelli, please upload the patch to #21676.

P.S. Version field indicates when the enhancement was initially suggested, not the current version.

#4 @enrico.sorcinelli
10 years ago

Hi @SergeyBiryukov,
I just added the patch also to #21676.

Note: See TracTickets for help on using tickets.