Opened 15 years ago
Last modified 6 years ago
#13691 new enhancement
Make get_template_part() accept multiple template names
Reported by: | 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)
Change History (18)
#1
@
15 years ago
forgot to mention:
$names can be pass a string or as an array, so it is backward compatible.
#2
@
15 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.
#3
@
14 years ago
- Component changed from General to Template
- Keywords has-patch added
- Milestone changed from Unassigned to Future Release
#5
@
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:
↓ 10
@
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
@
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.
#8
@
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.
get_template_part() accepting multiple template names