Render a template
-
Suggesting the implementation of the following function.
render_template($filename)
*Functionality*
This function will be able to render or convert a template file what consists of client side code (e.g. HTML, CSS or JavaScript) to domain specific code. The template may include PHP and client side code with placeholders what should be replaced for domain specific data.*Why do WordPress developers need this?*
This function helps developers to separate the view from the logic. Often I see theme developers mix up their logic with HTML. With this functionality you will be able to minimize the amount of logic in your templates however you will still be able to create complex templates. Work with hierarchical component like structures to avoid duplicated code and improve maintainability of your software.*How does it work?*
- Simply start of with a good old loop and inside it render your template:
print render_template('foo.php');
- Create a file called ‘foo.php’ (in this example) put the file inside the
tpl
directory of your theme directory. - Add the following content to the file to display the post title and content:
<h1>{post_title}</h1>
<p>{post_content}</p>
<?php print render_template('bar.php') ?>
- To extend the foo.php template you can now also add another template called bar.php what will be rendered inside the foo.php template.
*Have a look at the code I’ve written in this gist*
https://gist.github.com/Fleuv/fbdbcdc822be428486a349036efc51dd - Simply start of with a good old loop and inside it render your template:
- The topic ‘Render a template’ is closed to new replies.