[wp-trac] [WordPress Trac] #21676: Pass a variable to get_template_part()

WordPress Trac noreply at wordpress.org
Mon Apr 9 14:08:29 UTC 2018


#21676: Pass a variable to get_template_part()
-------------------------------------------+------------------------------
 Reporter:  sc0ttkclark                    |       Owner:  SergeyBiryukov
     Type:  enhancement                    |      Status:  reviewing
 Priority:  normal                         |   Milestone:  Awaiting Review
Component:  Template                       |     Version:  3.4.1
 Severity:  normal                         |  Resolution:
 Keywords:  has-patch close needs-testing  |     Focuses:
-------------------------------------------+------------------------------

Comment (by mboynes):

 I'm personally not a fan of using `extract()` as part of the solution to
 this problem. I think it will ultimately lead to confusing templates and
 undefined variables. Particularly for newer developers, I believe the
 ambiguity will quickly become frustration, especially given the existing
 state of affairs where some core variables are already loaded into the
 template part scope. Imagine someone pulling a template part out of a
 future `twentyeighteen` or `twentynineteen` theme and wondering why
 they're getting errors on one variable, e.g. `$page_title` but not
 another, `$paged`.

 About 5 years ago my company tried out several approaches, this one
 included, and eventually landed on a solution where we created a template
 tag to retrieve the variables passed to the template part. That template
 tag takes the variable name (array key for what gets passed to the
 template part, same as in the patches in this ticket). It also takes an
 optional default value, if the variable wasn't set. In core, this might
 look something like:

 {{{#!php
 <?php
 // single.php
 get_template_part( 'content', get_post_type(), array( 'image_size' =>
 'thumbnail' ) );
 }}}

 {{{#!php
 <?php
 // content-post.php
 the_post_thumbnail( get_template_var( 'image_size', 'small' ) );
 }}}

 If others have interest in considering this pattern, I could put together
 a patch.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/21676#comment:66>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list