Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ An alternative to the native WordPress function `get_template_part()` that can p
- Using composer : `composer require freshflesh/wp-render-template-part`
- Or manually download and include the `src/wp-render-template-part.php` file

##Parameters
## Parameters

* **$slug**: (string) (Required) The slug name for the generic template.
* **$name**: (string) (Optional) The name of the specialised template. Default value: null
Expand Down Expand Up @@ -63,6 +63,12 @@ Passing a WP_Post instance as a `post_object` argument will automatically call `

> Note: `query` and `post_object` are therefore reserved argument names


#### CSS selectors
You can add a CSS selector to the `slug` or `name` arguments to only load the HTML of the corresponding DOM node :
`render_template_part( 'partials/sidebar', 'article #container' );`


#### Extendable

2 hooks are provided to extend its behavior:
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
}
],
"require": {
"php": ">=5.2.0"
"php": ">=5.2.0",
"symfony/dom-crawler": "^3.2",
"symfony/css-selector": "^3.2"
},
"require-dev": {
},
"autoload": {
"files": ["src/wp-render-template-part.php"]
}

}
}
49 changes: 37 additions & 12 deletions src/wp-render-template-part.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* An alternative to the native WP function `get_template_part` that
* can pass arguments to the local scpoe
* can pass arguments to the local scope
*
* @param string $slug The slug name for the generic template.
* @param string $name The name of the specialised template.
Expand All @@ -12,6 +12,9 @@
* @return string The rendered template if $echo is false.
*/

use Symfony\Component\DomCrawler\Crawler;


function render_template_part( $slug, $name = null, $args = array(), $echo = true) {
global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;

Expand All @@ -29,13 +32,25 @@ function render_template_part( $slug, $name = null, $args = array(), $echo = tru
$args = apply_filters( "render_template_part_{$slug}_args", $args, $slug, $name, $echo );

$templates = array();

$name = (string) $name;
if ( '' !== $name )
if ( '' !== $name ) {
if ( strpos($name, ' ') !== false ) {
$cssSelector = substr($name, strrpos($name, ' ') + 1);
$name = substr($name, 0, strrpos($name, ' '));
}
$templates[] = "{$slug}-{$name}.php";

}

if ( strpos($slug, ' ') !== false ) {
$cssSelector = substr($slug, strrpos($slug, ' ') + 1);
$slug = substr($slug, 0, strrpos($slug, ' '));
}
$templates[] = "{$slug}.php";

if ( '' === locate_template($templates) ) return;

if ( '' === locate_template($templates) ) {
return;
}

if ( is_array( $wp_query->query_vars ) ) {
extract( $wp_query->query_vars, EXTR_SKIP );
Expand All @@ -60,13 +75,23 @@ function render_template_part( $slug, $name = null, $args = array(), $echo = tru
setup_postdata( $post );
}

if ( false === $echo ) {
ob_start();
require( locate_template($templates) );
$return = ob_get_clean();
ob_start();
require( locate_template($templates) );
$html = ob_get_clean();

if ( isset($cssSelector) ) {
$crawler = new Crawler();
$crawler->addContent($html);
try {
$html = $crawler->filter($cssSelector)->html();
}
catch (Exception $e) {
$html = '';
}
}
else {
require( locate_template($templates) );

if ( true === $echo ) {
echo $html;
}

if ( isset($query) ) {
Expand All @@ -78,7 +103,7 @@ function render_template_part( $slug, $name = null, $args = array(), $echo = tru
}

if ( false === $echo ) {
return $return;
return $html;
}

}