2017 © Pedro Peláez
 

library wp-render-template-part

An alternative to the native WP function `get_template_part` that can pass arguments to the local scope

image

freshflesh/wp-render-template-part

An alternative to the native WP function `get_template_part` that can pass arguments to the local scope

  • Thursday, January 5, 2017
  • by FreshFlesh
  • Repository
  • 4 Watchers
  • 4 Stars
  • 1,188 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 5 Versions
  • 6 % Grown

The README.md

render_template_part()

An alternative to the native WordPress function get_template_part() that can pass arguments to the local scope., (*1)

Installation

  • Using composer : composer require freshflesh/wp-render-template-part
  • Or manually download and include the src/wp-render-template-part.php file

Parameters

  • $slug: (string) (Required) The slug name for the generic template.
  • $name: (string) (Optional) The name of the specialised template. Default value: null
  • $args: (array) (Optional) An associative array containing arguments to pass to the template. Default empty array.
  • $echo: (boolean) (Optional) Wether to echo or return the rendered template. Default is true.

Usage

To pass arguments to a template partial, use the render_template_part() function., (*2)

Simple example

Parent template:, (*3)

<?php
    $args =  array(
                'title'   => 'Hello there.',
                'content' => 'Lorem ipsum sid amet'
             );

    render_template_part( 'block', 'widget', $args );

Child template:, (*4)

<div class="widget">
    <h2><?php echo $title; // will output 'Hello there.' ?></h2>
    <p><?php echo $content;  // will output 'Lorem ipsum sid amet' ?></p>
</div>

Returning value example

Useful when returning templates through AJAX for instance:, (*5)

<?php
    // get rendered template
    $html = render_template_part( 'partials/sidebar', 'article', array(
                 'post_id' => $post->ID
            ) );
    // send in json response 
    wp_send_json_success( array( $html );

Features

WP_Query autoloading

Passing a WP_Query instance as a query argument will automatically set it as the current loop., (*6)

WP_Post autoloading

Passing a WP_Post instance as a post_object argument will automatically call setup_post_data() so you can start using the_title(), the_content()., (*7)

Note: query and post_object are therefore reserved argument names, (*8)

Extendable

2 hooks are provided to extend its behavior:, (*9)

Action: do_action( "render_template_part_{$slug}", $slug, $name, $args, $echo );, (*10)

Filter: $args = apply_filters( "render_template_part_{$slug}_args", $args, $slug, $name, $echo );, (*11)

The Versions

05/01 2017

dev-master

9999999-dev https://github.com/thomascharbit/wp-render-template-part

An alternative to the native WP function `get_template_part` that can pass arguments to the local scope

  Sources   Download

MIT

The Requires

  • php >=5.2.0

 

by Thomas Charbit

wordpress template render part get_template_part

05/01 2017

1.1.2

1.1.2.0 https://github.com/thomascharbit/wp-render-template-part

An alternative to the native WP function `get_template_part` that can pass arguments to the local scope

  Sources   Download

MIT

The Requires

  • php >=5.2.0

 

by Thomas Charbit

wordpress template render part get_template_part

01/12 2016

1.1.1

1.1.1.0 https://github.com/FreshFlesh/wp-render-template-part

An alternative to the native WP function `get_template_part` that can pass arguments to the local scope

  Sources   Download

MIT

The Requires

  • php >=5.2.0

 

by Thomas Charbit

wordpress template render part get_template_part

31/08 2016

1.1.0

1.1.0.0 https://github.com/FreshFlesh/wp-render-template-part

An alternative to the native WP function `get_template_part` that can pass arguments to the local scope

  Sources   Download

MIT

The Requires

  • php >=5.2.0

 

by Thomas Charbit

wordpress template render part get_template_part

03/08 2016

1.0.0

1.0.0.0 https://github.com/FreshFlesh/wp-render-template-part

An alternative to the native WP function `get_template_part` that can pass arguments to the local scope

  Sources   Download

MIT

The Requires

  • php >=5.2.0

 

by Thomas Charbit

wordpress template render part get_template_part