Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
Automattic\Jetpack\Jetpack_Mu_Wpcom\A8C_Posts_List\render_template
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * Template functions.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom\A8C_Posts_List;
9
10/**
11 * OUTPUT BUFFERED LOAD TEMPLATE PART.
12 *
13 * Loads a given template using output buffering.
14 * Optionally including $data to be passed into template.
15 *
16 * @param string $template_name Name of the template to be located.
17 * @param array  $data          Optional. Associative array of data to be passed into the template. Default empty array.
18 * @return string
19 */
20function render_template( $template_name, $data = array() ) {
21    if ( ! strpos( $template_name, '.php' ) ) {
22        $template_name .= '.php';
23    }
24
25    $template_file = __DIR__ . '/templates/' . $template_name;
26
27    if ( ! file_exists( $template_file ) ) {
28        return '';
29    }
30
31    // Optionally provided an assoc array of data to pass to template
32    // and it will be extracted into variables.
33    if ( is_array( $data ) ) {
34        foreach ( $data as $name => $value ) {
35            $GLOBALS[ $name ] = $value;
36        }
37    }
38
39    ob_start();
40    require_once $template_file;
41    $content = ob_get_contents();
42    ob_end_clean();
43
44    return $content;
45}