Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
wp-php-web-components-demo/wp-content/themes/php-components/inc/template-tags.php
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
51 lines (44 sloc)
1.18 KB
This file contains 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 | |
/** | |
* Template tags. | |
*/ | |
/** | |
* Render a component. | |
* | |
* @param string $name Component name. | |
* @param array $attributes Attributes to pass to component. | |
* @param bool $echo Echo the component. | |
* | |
* @return false|string|void | |
*/ | |
function jb_component( $name = '', $attributes = [], $echo = true ) { | |
$path = get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'components' . DIRECTORY_SEPARATOR . $name . DIRECTORY_SEPARATOR . 'index.php'; | |
if ( ! file_exists( $path ) ) { | |
return false; | |
} | |
ob_start(); | |
require $path; | |
$component = ob_get_clean(); | |
if ( false === $echo ) { | |
return $component; | |
} | |
echo $component; // phpcs:ignore -- No need to escape it, since all escaping happens in the component. | |
} | |
/** | |
* Start building template content. | |
*/ | |
function jb_template_header() { | |
ob_start(); | |
} | |
/** | |
* Finish building template content. | |
*/ | |
function jb_template_footer() { | |
// Get built content. | |
$content = ob_get_clean(); | |
// Render built content after header. | |
// This is to trigger all hooks before the header is called. | |
get_header(); | |
echo $content; // phpcs:ignore -- No need to escape it, since all escaping happens in the template / components. | |
get_footer(); | |
} |