Skip to content
A simple WordPress plugin that works like get_template_part but allows you to pass variables.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
template-part-with-vars-classes
LICENSE
README.md
jpr-template-part-with-vars.php

README.md

Get Template Part with Variables

A simple WordPress plugin that works like get_template_part() but allows you to pass variables to the rendered template.

The variables must be passed as an associative array where the keys would be valid variable names.

You should be careful to NOT use any variable names that WordPress includes automatically. The plugin will bring all of those defaults in first, and if the variable you pass to this function would overwrite an existing variable, an exception will be thrown.

Example:

$variables = [
    'name' => 'John',
    'class' => 'featuredAuthor',
];

jpr_get_template_part_with_vars('author', 'info', $variables);


// In author-info.php:
echo "
<div class='$class'>
    <span>$name</span>
</div>
";

// Would output:
<div class='featuredAuthor'>
    <span>John</span>
</div>
You can’t perform that action at this time.