Last active
March 22, 2016 06:37
-
-
Save wkirby/c22fc99a0b8e62dd3503 to your computer and use it in GitHub Desktop.
Better component rendering for Wordpress themes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Apsis Template Render | |
Plugin URL: http://apsis.io/ | |
Description: Better component rendering for Wordpress Themes | |
Version: 1.0.0 | |
Author: Apsis Labs | |
Author URI: http://apsis.io/ | |
*/ | |
/** | |
* Get path for templates using same structure as {@link ap_include_template_part}, | |
* returns path for use with include to allow for passing variables | |
* between templates. If a name parameter is passed but not found, will fall | |
* back to using file with just the slug. | |
* | |
* @param string $slug Primary name for template part | |
* @param string $name Secondary name for template part | |
* @return string Fully qualified path for relevant template part | |
*/ | |
if ( !function_exists('ap_find_template_part') ) { | |
function ap_find_template_part($slug, $name = null) { | |
do_action( "find_template_part_{$slug}", $slug, $name ); | |
$templates = array(); | |
$name = (string) $name; | |
if ( '' !== $name ) { | |
$templates[] = "{$slug}-{$name}.php"; | |
} | |
$templates[] = "{$slug}.php"; | |
return locate_template($templates, false, false); | |
} | |
} | |
/** | |
* Return the rendered HTML of a template using {@link ap_find_template_part}. | |
* The params object is extracted into variables for use in the template. | |
* | |
* @param string $slug Primary name for template part | |
* @param object $params Object of variables to be available to template | |
* @param string $name Secondary name for template part | |
* @return string Rendered HTML of template | |
*/ | |
if ( !function_exists('ap_get_render') ) { | |
function ap_get_render($slug, $params = array(), $name = null) { | |
do_action( "get_render_{$slug}", $slug, $params, $name); | |
$template_part = ap_find_template_part($slug, $name); | |
if ( !empty($template_part) ) { | |
ob_start(); | |
extract($params, EXTR_SKIP); | |
include($template_part); | |
return ob_get_clean(); | |
} | |
} | |
} | |
/** | |
* Outputs the rendered HTML as fetched by {@link ap_get_render} | |
* @param string $slug Primary name for template part | |
* @param object $params Object of variables to be available to template | |
* @param string $name Secondary name for template part | |
*/ | |
if ( !function_exists('ap_render') ) { | |
function ap_render($slug, $params = array(), $name = null) { | |
do_action( "render_template_part_{$slug}", $slug, $params, $name); | |
echo ap_get_render($slug, $params, $name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment