Themes

Last updated: September 3rd, 2017

Introduction

A theme in TriTan consists of routers and layouts (views). Your theme's folder structure should follow the below layout.


themes (where all themes are stored)
    -- Vapor (main theme folder and name of theme)
        -- assets
            -- css
            -- fonts
            -- images
            -- js
        -- functions.php
        -- routers
            -- index.router.php
            -- posts.router.php
        -- views
            -- layout.tpl
            -- posts.tpl
            -- rss.tpl
            -- single.tpl

What you name your routers and views is up to you. Just make sure that your routers end with the extension .router.php.

For views, TriTan uses the Fenom library. It is very easy templating language to learn and you should checkout the documentation. When rendering a view, there are two parameters that can be passed to the display() method.


/**
 * $viewName is the file name minus + extension (i.e. tpl, html, etc)
 * $data is an array that can be passed and then
 * manipulated in the view.
 */
$app->fenom->display($viewName, $data = []);
                                        

Layouts

You can create a parent layout (template) that other templates (children) can extend. You need to utilize the {extends} and {block} tags.


/**
 * This is a sample layout (i.e. layout.tpl).
 */
<html>
<head>
  <title>{$title}</title>
</head>
<body>
  {block 'content-layout'}{/block}
</body>
</html>
                                        

Now, let's say we need to create a post layout which extends the main layout. Our posts.tpl file would look like below:


/**
 * This posts.tpl extends the parent template which is layout.tpl.
 */
{extends "layout.tpl"}

{block 'content-layout'}
    
    {foreach $posts as $post}
        <article class="preview">
            <header>
                <h1 class="post-title"><a href="{$.php.get_permalink($post.post_id)}">{$.php.get_post_title($post.post_id)}</a></h1>
                <div class="post-meta"><time datetime="{$.php.get_post_datetime($post.post_id)}">{$.php.timeAgo($.php.strtotime($.php.get_post_datetime($post.post_id)))}</time></div>
            </header>
            <section class="post-excerpt">
                {$.php.the_content($post.post_id)|truncate:150:" . . ."}
                <p class="readmore"><a href="{$.php.get_permalink($post.post_id)}">Read More <i class="fa fa-chevron-circle-right"></i></a></p>
            </section>
        </article>
    {/foreach}

{/block}
                                        

Route

That's the basic structure of how to make a parent layout with a child layout extending the parent. Now below is an example of how we would construct our route:


/**
 * Example route in router posts.router.php.
 * This route is a base route (http://localhost:8888/) where we get a list
 * of all our posts.
 */
$app->get('/', function() use($app) {
    $posts = $app->db->table('post')
        ->where('post_type.post_posttype', 'post')
        ->get();

    $app->fenom->display('posts.tpl', [
        'title' => 'Posts',
        'posts' => $posts
    ]);
});
                                        

Functions

You are free to write and develop your own functions, but you can also use the default functions that come with TriTan. In order to use those functions or any PHP functions for that matter, you have to let Fenom know that they are a function it should parsed. To let Fenom know that the code is a function, it must be preceeded with $.php.. If you are using the functions in your router or functions.php file, of course you would use them in the normal php way. However, many of the core TriTan CMS functions are now namespaced with the exception of the functions in app/functions.php. Using namespaced functions in your theme will instead start with $.php.. Check out the API Docs for a complete list of functions available for use. Following is a list of TriTan functions that can be used in your templates.

current_user_can


/**
 * Function checks if logged in user
 * has the requested permission.
 * 
 * @file app/functions/auth-function.php
 *
 * @since 0.9
 * @param string $perm Permission to look check for.
 */
$.php.current_user_can($perm);

get_userdata


/**
 * Retrieve info of the logged in user.
 * 
 * @file app/functions/auth-function.php
 *
 * @since 0.9
 * @param mixed $field The field you want returned.
 * @return mixed
 */
$.php.get_userdata($field);

is_user_logged_in


/**
 * Checks if a visitor is logged in or not.
 * 
 * @file app/functions/auth-function.php
 *
 * @since 0.9
 * @return boolean
 */
$.php.is_user_logged_in();

get_user_by


/**
 * Retrieve user info by a given field from the user's table.
 *
 * @file app/functions/auth-function.php
 *
 * @since 0.9
 * @param string $field The field to retrieve the user with.
 * @param int|string $value A value for $field (id, uname or email).
 */
$.php.get_user_by($field, $value);

site_url


/**
 * Returns the url for a given site.
 *
 * Returns 'https' if `is_ssl()` evaluates to true and 'http' otherwise. If `$scheme` is
 * 'http' or 'https', `is_ssl()` is overridden.
 *
 * @file app/functions/link-function.php
 *
 * @since 0.9.9
 * @param string $path    Optional. Route relative to the site url. Default '/'.
 * @param string $scheme  Optional. Scheme to give the site URL context. Accepts
 *                        'http', 'https', 'login', 'admin', or 'relative'.
 *                        Default null.
 * @return string Site url link.
 */
$.php.site_url($path = '', $scheme = null);

admin_url


/**
 * Returns the url to the admin area for a given site.
 *
 * @file app/functions/link-function.php
 *
 * @since 0.9.9
 * @param string $path    Optional. Path relative to the admin url. Default empty.
 * @param string $scheme  Optional. The scheme to use. Accepts 'http' or 'https',
 *                        to force those schemes. Default 'admin'.
 * @return string Admin url link with optional path appended.
 */
$.php.admin_url($path = '', $scheme = 'admin');

home_url


/**
 * Returns the url for a given site where the front end is accessible.
 *
 * The protocol will be 'https' if `is_ssl()` evaluates to true; If `$scheme` is
 * 'http' or 'https', `is_ssl()` is overridden.
 *
 * @file app/functions/link-function.php
 *
 * @since 0.9.9
 * @param string      $path    Optional. Path relative to the home url. Default empty.
 * @param string|null $scheme  Optional. Scheme to give the home URL context. Accepts
 *                              'http', 'https', 'relative', or null. Default null.
 * @return string Home url link with optional path appended.
 */
$.php.home_url($path = '', $scheme = null);

login_url


/**
 * Returns the login url for a given site.
 *
 * @file app/functions/link-function.php
 *
 * @since 0.9
 * @param string $redirect Path to redirect to on log in.
 * @param string $path     Optional. Path relative to the login url. Default empty.
 * @param string|null $scheme  Optional. Scheme to give the home URL context. Accepts
 *                              'http', 'https', 'relative', or null. Default 'login'.
 * @return string Returns the login url.
 */
$.php.login_url($redirect = '', $path = '', $scheme = 'login');

esc_html


/**
 * Escaping for HTML blocks.
 *
 * @file app/functions.php
 *
 * @since 0.9.9
 * @param string $string
 * @return string Escaped HTML block.
 */
$.php.esc_html($string);

ttcms_seconds_to_time


/**
 * Converts seconds to time format.
 * 
 * @file app/functions/core-function.php
 *
 * @since 0.9
 * @param numeric $seconds
 */
$.php.ttcms_seconds_to_time($seconds);

php_like


/**
 * SQL Like operator in PHP.
 * 
 * Returns true if match else false.
 * 
 * Examples:
 *      php_like('%uc%','Lucy'); //true
 *      php_like('%cy', 'Lucy'); //true
 *      php_like('lu%', 'Lucy'); //true
 *      php_like('%lu', 'Lucy'); //false
 *      php_like('cy%', 'Lucy'); //false
 * 
 * @file app/functions/core-function.php
 *
 * @since 0.9
 * @param string $pattern
 * @param string $subject
 * @return bool
 */
$.php.php_like($pattern, $subject);

ttcms_url_shorten


/**
 * Url shortening function.
 * 
 * @file app/functions/core-function.php
 *
 * @since 0.9
 * @param string $url URL
 * @param int $length Characters to check against.
 * @return string
 */
$.php.ttcms_url_shorten($url, $length = 80);

ttcms_redirect


/**
 * Redirects to another page.
 * 
 * @file app/functions/core-function.php
 *
 * @since 0.9
 * @param string $location The path to redirect to
 * @param int $status Status code to use
 * @return bool False if $location is not set
 */
$.php.ttcms_redirect($location, $status = 302);

html_purify


/**
 * Makes content safe to print on screen.
 *
 * This function should only be used on output. With the exception of uploading
 * images, never use this function on input. All inputted data should be
 * accepted and then purified on output for optimal results. For output of images,
 * make sure to escape with esc_url().
 *
 * @file app/functions.php
 *
 * @since 0.9.9
 * @param string $string Text to purify.
 * @param bool $is_image
 * @return string
 */
$.php.html_purify($string, $is_image = false);

_return_post


/**
 * Return posted data if set.
 * 
 * @file app/functions.php
 *
 * @since 0.9
 * @param mixed $post
 * @return mixed
 */
$.php._return_post($post);

ttcms_pluralize


/**
 * Pluralizes a word if quantity is not one.
 *
 * Example Usages:
 *                  ttcms_pluralize(4, 'cat'); // cats
 *                  ttcms_pluralize(3, 'kitty'); // kitties
 *                  ttcms_pluralize(2, 'octopus', 'octopii'); // octopii
 *                  ttcms_pluralize(1, 'mouse', 'mice'); // mouse
 * 
 * @file app/functions/core-function.php
 *
 * @since 0.9
 * @param int $quantity Number of items
 * @param string $singular Singular form of word
 * @param string $plural Plural form of word; function will attempt to deduce plural form from singular if not provided
 * @return string Pluralized word if quantity is not one, otherwise singular
 */
$.php.ttcms_pluralize($quantity, $singular, $plural = null);

app


/**
 * Call the application global scope.
 *
 * A better alternative than using:
 *      global $app;
 * 
 * @file app/functions.php
 *
 * @since 0.9
 * @return object
 */
$.php.app();

has_posts


/**
 * This function checks to see if the current TriTan CMS query has any
 * results to loop over.
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @return int|null
 */
$.php.has_posts();

get_post


/**
 * Retrieves post data given a post ID or post array.
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @param int|Post|null $post Post ID or post object.
 * @param bool $object  If set to true, data will return as an object, else as an array.
 *                      Default: true.
 * @return array|object
 */
$.php.get_post($post, $object = true);

get_post_date


/**
 * A function which retrieves TriTan CMS post date.
 *
 * Uses `call_user_func_array()` function to return appropriate post date function.
 * Dynamic part is the variable $type, which calls the date function you need.
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @param int $post_id The unique id of a post.
 * @param string $type Type of date to return: created, published, modified. Default: published.
 * @return string
 */
$.php.get_post_date($post_id = 0, $type = 'published');

get_post_time


/**
 * A function which retrieves TriTan CMS post time.
 *
 * Uses `call_user_func_array()` function to return appropriate post time function.
 * Dynamic part is the variable $type, which calls the date function you need.
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @param int $post_id The unique id of a post.
 * @param string $type Type of date to return: created, published, modified. Default: published.
 * @return string
 */
$.php.get_post_time($post_id = 0, $type = 'published');

get_post_datetime


/**
 * A function which retrieves TriTan CMS post datetime.
 * 
 * Purpose of this function is for the post_datetime
 * filter.
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @param int $post_id The unique id of a post.
 * @return string
 */
$.php.get_post_datetime($post_id = 0);

get_post_modified


/**
 * A function which retrieves TriTan CMS post modified datetime.
 * 
 * Purpose of this function is for the post_modified
 * filter.
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @param int $post_id The unique id of a post.
 * @return string
 */
$.php.get_post_modified($post_id = 0);

get_post_content


/**
 * A function which retrieves a TriTan CMS post content.
 * 
 * Purpose of this function is for the_post_content
 * filter.
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @param int $post_id The unique id of a post.
 * @return string
 */
$.php.get_post_content($post_id = 0);

get_post_type_name


/**
 * A function which retrieves a TriTan CMS post posttype name.
 * 
 * Purpose of this function is for the post_posttype_name
 * filter.
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @param int $post_id The unique id of a post.
 * @return string
 */
$.php.get_post_type_name($post_id = 0);

get_post_posttype_link


/**
 * A function which retrieves a TriTan CMS post posttype link.
 * 
 * Purpose of this function is for the post_posttype_link
 * filter.
 *
 * Example:
 *      http://localhost:8888/post/
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @param into $post_id The unique id of a post.
 * @return string
 */
$.php.get_post_posttype_link($post_id = 0);

get_post_title


/**
 * A function which retrieves a TriTan CMS post title.
 * 
 * Purpose of this function is for the post_title
 * filter.
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @param int $post_id The unique id of a post.
 * @return string
 */
$.php.get_post_title($post_id = 0);

get_post_slug


/**
 * A function which retrieves a TriTan CMS post slug.
 * 
 * Purpose of this function is for the post_slug
 * filter.
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @param int $post_id The unique id of a post.
 * @return string
 */
$.php.get_post_slug($post_id = 0);

the_content


/**
 * The TriTan CMS post filter.
 *
 * @uses app()->hook->apply_filter('the_content')
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @param int $post_id The unique id of a post.
 * @return string
 */
$.php.the_content($post_id = 0);

the_posts


/**
 * Wrapper function for get_all_posts.
 *
 * @file app/functions/post-function.php
 *
 * @since 0.9
 * @param string $post_type The post type.
 * @param int $limit        Number of posts to show.
 * @param null|int $offset  The offset of the first row to be returned.
 * @param string $status    Should it retrieve all statuses, published, draft, etc.?
 * @return object
 */
$.php.the_posts($post_type = null, $limit = 0, $offset = null, $status = 'all');

get_user_value


/**
 * Retrieve requested field from usermeta document based on user's id.
 *
 * @file app/functions/user-function.php
 *
 * @since 0.9
 * @param string $id    User ID.
 * @param string $field Data requested of particular user.
 * @return mixed
 */
$.php.get_user_value($id, $field);

username_exists


/**
 * Checks whether the given username exists.
 *
 * Uses `username_exists` filter.
 *
 * @file app/functions/user-function.php
 *
 * @since 0.9
 * @param string $username Username to check.
 * @return string|false The user's ID on success, and false on failure.
 */
$.php.username_exists($username)

email_exists


/**
 * Checks whether the given email exists.
 *
 * Uses `email_exists` filter.
 *
 * @file app/functions/user-function.php
 *
 * @since 0.9
 * @param string $email Email to check.
 * @return string|false The user's ID on success, and false on failure.
 */
$.php.email_exists($email);