[wp-trac] [WordPress Trac] #21673: Passing scoped variables and doing partial page caching with get_template_part

WordPress Trac wp-trac at lists.automattic.com
Thu Aug 23 19:09:35 UTC 2012


#21673: Passing scoped variables and doing partial page caching with
get_template_part
-----------------------------+-----------------------------
 Reporter:  sc0ttkclark      |      Owner:
     Type:  feature request  |     Status:  new
 Priority:  normal           |  Milestone:  Awaiting Review
Component:  Template         |    Version:  3.4.1
 Severity:  normal           |   Keywords:
-----------------------------+-----------------------------
 I've recently developed an interesting class that provides an advanced
 get_template_part() experience.

 This class has some stuff baked in for the project it was for, but it
 really provides a great proof of concept for a big addition to
 get_template_part().

 I am suggesting we add three new arguments to get_template_part():

 {{{
 function get_template_part ( $slug, $name = null, $data = null, $expires =
 null, $cache_mode = null ) {
 }}}

 $slug and $name are great and all, but they lack the ability to reference
 currently scoped variables like a normal include/require does. One way
 around this is to pass $data into get_template_part(), like so:

 {{{
 <?php get_template_part( 'twitter', 'feed', compact( array_keys(
 get_defined_vars() ) ) ); ?>
 }}}

 This at least gives get_template_part() access to the currently scoped
 variables or any others you would like to pass in for context of a
 specific get_template_part() call. $data would then be passed to
 load_template() just above the $wp_query extract:

 {{{
 if ( !empty( $_data ) && is_array( $_data ) )
         extract( $_data, EXTR_SKIP );
 }}}

 The last two variables are centered around caching. $expires functions as
 you'd expect, it's a integer of seconds, where 0 is to not expire.
 $cache_mode would accept one of three values: cache | transient | site-
 transient

 Caching get_template_part() can be very handy, especially for content that
 won't change often, and especially for cases where you have logged in
 users but no ability to offer full page caching (not recommended, as
 you've come to know).

 '''Partial page caching is where it's at''', this would be a big step up
 for theming within WordPress. I'm also not opposed to splitting this
 ticket up into two, one to pass $data into get_template_part(), and one to
 create a new function that hooks into get_template_part() using output
 buffering, something to be named like get_template_cached().

 You can see my proof of concept as it currently exists in Pods 2.0 core,
 just ignore the parts that aren't exactly like get_template_part() :)

 https://github.com/pods-framework/pods/blob/2.0/classes/PodsView.php

 {{{
 pods_view( 'location/of/file.php', compact( array_keys( get_defined_vars()
 ) ), ( 60 * 60 * 24 ), 'site-transient' );
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/21673>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list