diff --git a/README.md b/README.md index 4d3d201..1ff3674 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: diff --git a/composer.json b/composer.json index f93eb07..8e9d53b 100644 --- a/composer.json +++ b/composer.json @@ -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"] } - -} \ No newline at end of file +} diff --git a/src/wp-render-template-part.php b/src/wp-render-template-part.php index cea4ed3..89bb446 100644 --- a/src/wp-render-template-part.php +++ b/src/wp-render-template-part.php @@ -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. @@ -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; @@ -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 ); @@ -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) ) { @@ -78,7 +103,7 @@ function render_template_part( $slug, $name = null, $args = array(), $echo = tru } if ( false === $echo ) { - return $return; + return $html; } }