WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 3 months ago

#13691 new enhancement

Make get_template_part() accept multiple template names

Reported by: wjm Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Themes Keywords: needs-patch
Focuses: template Cc:

Description

This enhancement allows get_template_part() allows to pass several template names, to work in the same way as: locate_template()

so, you could call,

get_template_part( 'loop', array('category', 'taxonomy', 'archive') );

$names are passed by order of priority.

Attachments (5)

get_template_part.diff (1.3 KB) - added by wjm 4 years ago.
get_template_part() accepting multiple template names
get_template_part.2.diff (1.1 KB) - added by wjm 4 years ago.
Simplified version
get_template_part.3.diff (3.5 KB) - added by wjm 4 years ago.
implements get_template_part() in get_header(), get_footer() and get_sidebar()
get_template_part.4.diff (6.0 KB) - added by wjm 4 years ago.
Cache system + require_once
get_template_part.5.diff (6.3 KB) - added by wjm 4 years ago.
This removes the need to add an action

Download all attachments as: .zip

Change History (17)

wjm4 years ago

get_template_part() accepting multiple template names

comment:1 wjm4 years ago

forgot to mention:

$names can be pass a string or as an array, so it is backward compatible.

comment:2 nacin4 years ago

Seems sane, though too late for 3.0. It would be cleaner to simply case names as an array and use it in a single foreach.

wjm4 years ago

Simplified version

comment:3 nacin4 years ago

  • Component changed from General to Template
  • Keywords has-patch added
  • Milestone changed from Unassigned to Future Release

comment:4 nacin4 years ago

I like this. Could you provide a basic case?

comment:5 nacin4 years ago

s/basic case/basic use case/

Another thought: If we do this for get_template_part(), we'd want to do it for get_footer(), get_header(), and get_sidebar() as well.

comment:6 follow-up: wjm4 years ago

This is the first time i need to write something like this.
If this is not the case, let me know and i will do my best to write it again.

Allowing get_template_part() to take a second argument, allows theme designers to use "contextual" template parts.
Letting them extend themes based on different situations.
Working with a theme framework such as hybrid, which uses a function to get its context ( hybrid_get_context() will retrieve a list of contexts
example

array (
    [0] => archive
    [1] => taxonomy
    [2] => category
    [3] => category-bananas
)

so by calling ( get_template_part( array_reverse( hybrid_get_context() ) ) )
this way it is possible to call 'loop-category-bananas.php' just by creating the template file, but if that file is not present, then 'loop-category.php' is called, and so on.

Letting theme designers focus on creating the files rather worrying about modifying the code.

this change can also be applied to get_footer(), get_header(), get_sidebar()
letting theme designer create featured footers, headers, sidebars for certain situations.

comment:7 nacin4 years ago

  • Keywords needs-patch added; has-patch removed

I like this. The only thing is that we'd be looking at a good number of file_exists checks there to traverse the entire context tree. I suppose if we can patch all four functions, we can go from there.

wjm4 years ago

implements get_template_part() in get_header(), get_footer() and get_sidebar()

wjm4 years ago

Cache system + require_once

comment:8 wjm4 years ago

Changes made to the last patch.

require_once argument added to get_template_part(),
this is set to true to get_header(), get_footer() and get_sidebar()

A cache listing the template files has been created, which I hope speeds things up avoiding to run file_exists().
It also reads template subdirectories, only on demand (not commonly used buy it was possible to include a template using a / in its slug or name).

If someone can benchmark this, but all the code was made with speed in mind.

comment:9 demetris4 years ago

  • Cc dkikizas@… added

wjm4 years ago

This removes the need to add an action

comment:10 in reply to: ↑ 6 scribu4 years ago

Replying to wjm:

Allowing get_template_part() to take a second argument, allows theme designers to use "contextual" template parts.
Letting them extend themes based on different situations.

I think a better way to go about this would be #12877.

Suggest closing as duplicate.

comment:11 SergeyBiryukov6 months ago

#25558 was marked as a duplicate.

comment:12 nacin3 months ago

  • Component changed from Template to Themes
  • Focuses template added
Note: See TracTickets for help on using tickets.