Using Controllers and Views

In part 1 we set up the most basic implementation of Tidy Templates, we simply moved the template files to a folder called “templates”.

Like Django and Rails, we can separate our logic from presentation too. Add another constant to the wp-config.php file : ‘WP_VIEWS_DIRECTORY’ and set it to “views”:

<?php
define('WP_VIEWS_DIRECTORY', 'views');

While we are here, let’s change WP_TEMPLATE_DIRECTORY to “controllers”

Screen Shot 2016-04-04 at 4.09.57 PM

Don’t forget to create another folder in the theme root called “views”.  Now it gets a little complicated here, the files in the views and controller folders have 1 to 1 relationships. What we will be doing is, collecting the data we want to display in the “controller” files (the old templates) and we will be sending them over to the “views” files (must be php files) to be displayed.

The Controller – Collect your data

So for example. The single.php template (in controllers) collects data about a post such as:

  • Title
  • date
  • author
  • content
  • ect

Use whatever method you want to collect that data and package it up into an key/value (associative) array like so:

<?php
global $post;
 
$title = $post->post_title;
$author_id = $post->post_author;
$author_name = get_the_author_meta('display_name', $author_id);
$date = date("M j, Y", $post->post_date);
$content = $post->post_content;
 
$data = array(
'title' => $title,
'author_name' => $author_name,
'date' => $date,
'content' => $content
);

Now that you have everything packaged up, you can send it to the view with the following function:

<?php
tidyt_render( 'post', $data);

The function tidyt_render() accepts 2 arguments:

  1. view name
    1. a single value (string) of the filename for the view to send the data too
    2. an array of filenames (strings). tidyt_render will look through the list and use the first file it finds
  2. the data
    1. a key/value array (associative array) – tidyt_render will extract the values in the array, so they can be used by their key name. ie – ‘title’ can be used as $title
    2. a single variable (any type) – you’ll have to access this as $data in the view

The View

Now in the view, just echo out the variables. So, using the “title” value in the above example we could add something like this in the post.php

<h1> <?php echo $title; ?></h1>

The view is also here you want to include the header, footer, and sidebars. You can use the tidyt_render() function to include those files. It’s a good practice to package up data for the included files right in the controller. See below for examples.

 

For a more comprehensive example check out some examples on github.

Did you notice the line where we render the comments? We are passing in some data called $comments_data to that view to unpackage and display. A good practice is to package that up in the controller to keep all logic out of the views.

Whats next? New Templates!

Finally, let’s learn about the new template options Tidy Templates gives us. Let’s go.