Make WordPress Core

Opened 14 years ago

Last modified 5 years ago

#13691 new enhancement

Make get_template_part() accept multiple template names

Reported by: wjm's profile wjm Owned by:
Milestone: 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 14 years ago.
get_template_part() accepting multiple template names
get_template_part.2.diff (1.1 KB) - added by wjm 14 years ago.
Simplified version
get_template_part.3.diff (3.5 KB) - added by wjm 14 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 14 years ago.
Cache system + require_once
get_template_part.5.diff (6.3 KB) - added by wjm 14 years ago.
This removes the need to add an action

Download all attachments as: .zip

Change History (18)

@wjm
14 years ago

get_template_part() accepting multiple template names

#1 @wjm
14 years ago

forgot to mention:

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

#2 @nacin
14 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.

@wjm
14 years ago

Simplified version

#3 @nacin
14 years ago

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

#4 @nacin
14 years ago

I like this. Could you provide a basic case?

#5 @nacin
14 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.

#6 follow-up: @wjm
14 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.

#7 @nacin
14 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.

@wjm
14 years ago

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

@wjm
14 years ago

Cache system + require_once

#8 @wjm
14 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.

#9 @demetris
14 years ago

  • Cc dkikizas@… added

@wjm
14 years ago

This removes the need to add an action

#10 in reply to: ↑ 6 @scribu
14 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.

#11 @SergeyBiryukov
11 years ago

#25558 was marked as a duplicate.

#12 @nacin
10 years ago

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