Support » Fixing WordPress » 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?*

    1. Simply start of with a good old loop and inside it render your template:
      print render_template('foo.php');
    2. Create a file called ‘foo.php’ (in this example) put the file inside the tpl directory of your theme directory.
    3. 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') ?>
    4. 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

Viewing 3 replies - 1 through 3 (of 3 total)
  • This is like shortcodes for templates. There are already libraries like Mustache to do this, but this is actually what a theme is already doing, but without a middleman function.
    Also, the post content contains paragraphs, so putting it in a paragraph would produce invalid syntax.

    I’m not sure it’s a good idea to recursively call the function to grab the post data.

    Hello Joy,

    Thank you for your reply.

    There are already libraries like Mustache to do this

    Could you please give a general explanation of what the Mustache library is capable of? I tried to search for it but got irrelevant results I guess.

    this is actually what a theme is already doing, but without a middleman function.

    The function is actually not much like a theme as far as I can tell, you can call it a sort of wrapper function if that is what you mean by “middleman function” however it does also come with it’s own logic for organizing template files and setting up open and close identifiers for the placeholders used in the template. And of course last but not least customizing replacements through hooks.

    the post content contains paragraphs, so putting it in a paragraph would produce invalid syntax.

    This is not completely true, the content initially doesn’t include paragraphs. Of course it does if you add a paragraph to the content through e.g. the content editor. However the code above is just an example, the issue could be solved by writing <div>{post_content}</div> instead.

    I’m not sure it’s a good idea to recursively call the function to grab the post data.

    The post data is cached and thus it wont affect the performance of your site concernedly. Of course you should be careful as you add more items to the replacements array and use the function a lot of times recursively, then this will drag the performance down eventually. To clarify: when executing the preg_replace() function about +1.000.000 with an array of about 100 replacement patterns. It will drag the response time down by 5 seconds. I don’t think any website will use the function in such extend.

    But how about Mustache how do they do it, does it affect the performance drastically?

    Mustache: https://en.wikipedia.org/wiki/Mustache_%28template_system%29

    Here is a comparison: https://developer.ibm.com/node/2014/11/11/compare-javascript-templates-jade-mustache-dust/

    organizing template files and setting up open and close identifiers for the placeholders used in the template. And of course last but not least customizing replacements through hooks.

    That’s what a theme does, but with more readability and child theme compatibility.

    the issue could be solved by writing <div>{post_content}</div> instead.

    This is what the theme is already doing, but better because of classes for styling and structured data (see schema.org)

    The post data is cached and thus it wont affect the performance

    My concern was not performance, but infinite loops by filtering the thing that you are recursively retrieving. Better to do it in a logical flow instead of everything calling the same function which can call itself. It doesn’t even make sense to set up the variables each time through. There is no benefit to this function.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Render a template’ is closed to new replies.