Skip to content

Commit 1e3392e

Browse files
committed
feat(Functions): Added template functions
1 parent fcd4c1a commit 1e3392e

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

xwp-helper-fns.php

+51
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,54 @@ function xwp_bool_to_str( bool|string $boolean ): string {
195195
return xwp_str_to_bool( $boolean ) ? 'yes' : 'no';
196196
}
197197
endif;
198+
199+
200+
if ( ! function_exists( 'xwp_get_template' ) ) :
201+
202+
/**
203+
* Get a template passing variables and including the file.
204+
*
205+
* @param string $template The template file.
206+
* @param null|array<string,mixed> $params Optional. The variables to pass to the template file.
207+
*
208+
* @since 1.18.0
209+
*/
210+
function xwp_get_template( string $template, ?array $params = null ): void {
211+
if ( ! file_exists( $template ) ) {
212+
_doing_it_wrong(
213+
__FUNCTION__,
214+
sprintf( 'The template file %s does not exist.', esc_html( basename( $template ) ), ),
215+
'1.0.0',
216+
);
217+
return;
218+
}
219+
220+
if ( is_array( $params ) && $params ) {
221+
//phpcs:ignore WordPress.PHP.DontExtract.extract_extract
222+
extract( $params );
223+
}
224+
225+
include $template;
226+
}
227+
228+
endif;
229+
230+
231+
if ( ! function_exists( 'xwp_get_template_html' ) ) :
232+
233+
/**
234+
* Like `xwp_get_template` but returns the HTML instead of outputting it.
235+
*
236+
* @param string $template The template file.
237+
* @param null|array<string,mixed> $params Optional. The variables to pass to the template file.
238+
* @return string
239+
*
240+
* @since 1.18.0
241+
*/
242+
function xwp_get_template_html( string $template, ?array $params = null ): string {
243+
ob_start();
244+
xwp_get_template( $template, $params );
245+
return (string) ob_get_clean();
246+
}
247+
248+
endif;

0 commit comments

Comments
 (0)