Knowledgebase
In-depth explaination of the templating system.
Posted by Konrad Piwowar on 06 January 2017 02:36 PM

This article is to explain more in depth how the script is put together and how the templating system works.

First of all, the most important template is template.content_item.php. It contains the thumbnail block:

There is a similar file called template.content_item_photos.php, which is the same thing except for photo galleries.

All of the content display pages that show thumbs are put together in the same way. Examples of these pages include:

All of these pages are loading the same template files:

  • template.overall_header.php
  • template.content_item.php (or content_item_photos if it's gallery sorting) repeating over and over
  • template.overall_footer.php

All of these pages are loading exactly the same template files, but the database query is being swapped out based on the URL:

  • User hits /most-viewed/
  • The following rewriting rule is accessed:
    RewriteRule ^(my-uploads|favorites|most-recent|most-discussed|most-viewed|longest|top-rated|photos|random|my-friends|videos|my-rentals)/$ index.php?controller=index&mode=$1 [QSA]
  • We are hitting /most-viewed/, so the actual request being made is index.php?controller=index&mode=most-viewed
  • /index.php is loaded, which loads inc.metatags.php and controllers/control.index_queries.php
  • includes/inc.metatags.php determines the metatags, titles, etc based on mode=most-viewed
  • controllers/control.index_queries.php determines the database query to be used, based on mode=most-viewed
  • Once the above two are loaded and have their data, the template files are loaded (in the way mentioned 1 paragraph higher)

The homepage is loaded in exactly the same way. On the default template with on a clean installation (or our demo site), everything above the "Free Tube" section is overall_header, the section itself is content_item repeating several times based on database query loaded from control.index_queries.php, and everything below it is overall_footer.php. On the homepage, there are additional blocks (such as Videos Being Watched) that are loaded based on "if" statements in the template files so that they only show up in the homepage. The left sidebar on /videos/ is loaded much in the same way - it's only displayed NOT on the homepage.


The media pages (so video and gallery pages) are loaded in a much simpler way:

The video page is made up of:

  • template.video_header.php
  • template.video_content.php
  • template.video_footer.php

The gallery page is made up of:

  • template.video_header.php
  • template.gallery_content.php
  • template.video_footer.php

Similarly to above, these pages are loaded via rewriterules in .htaccess

  • RewriteRule ^galleries/(.*)-([0-9]+).html$ index.php?controller=gallery&id=$2 [QSA]
  • RewriteRule ^video/(.*)-([0-9]+).html$ index.php?controller=video&id=$2 [QSA]

The "video" controller is loaded (so controllers/control.video.php), which grabs all the data and then loads the template files listed above. For galleries, it's the same except it loads the gallery controller as specified in the rewriterule.


Other pages, such as signup or login, are loaded similarly to the video/gallery pages as listed above. For instance, the signup page consists of:

  • template.overall_header.php
  • template.signup.php
  • template.overall_header.php

The signup page is loaded by the signup page rewrite, so:

  • RewriteRule ^signup$ index.php?controller=signup [QSA]

Index.php loads /controllers/control.signup.php (which contains all the logic of the signup process) and includes/inc.metatags.php (which determines the metatags/titles/etc for this page based on "controller=signup"), and then loads the appropriate template files.


A common question that get's asked is how to make something appear only on certain pages such as most-viewed or only on the login page if all of the pages are using the same template files. The answer to this is to use php if statements based on the information you have from the rewriterules.

For instance, if I wanted to show something in overall_header or overall_footer that appears ONLY on the signup page, I would use

  • <? if($_GET['controller'] == 'signup') { ?>MY HTML THAT IS SUPPOSED TO SHOW ONLY ON SIGNUP PAGE GOES HERE<? } ?>

If I wanted to show it only every page EXCEPT for signup, I would do the same but instead of == use != (does not equal):

  • <? if($_GET['controller'] != 'signup') { ?>MY HTML THAT IS SUPPOSED TO SHOW ONLY ON SIGNUP PAGE GOES HERE<? } ?>

We know to use $_GET['controller'] because of the rewriterule:

  • RewriteRule ^signup$ index.php?controller=signup [QSA]

If you wanted to show something only on the most-viewed page, you would have to use $_GET['mode'] because the controller is the same for all the sorting pages (top rated, most viewed, newest, search results, etc), so:

  • <? if($_GET['mode'] == 'most-viewed') { ?>YOUR HTML FOR MOST-VIEWED ONLY PAGE GOES HERE<? } ?>

If you wanted to show something only on page 1 of the most-viewed page, then you would do:

  • <? if($_GET['mode'] == 'most-viewed' && $_GET['page'] == 1) { ?>YOUR HTML FOR MOST-VIEWED PAGE 1 ONLY<? } ?>

or on page 3 of top-rated:

  • <? if($_GET['mode'] == 'top-rated' && $_GET['page'] == 3) { ?>YOUR HTML FOR TOP RATED PAGE 3 ONLY<? } ?>

or only on the homepage:

  • <? if($_GET['controller'] == 'index' && !$_GET['mode']) { ?>SHOWS ONLY ON HOMEPAGE<? } ?>
    (in this above case we are specifying !$_GET['mode'], which means that there is no mode selected.

(4 vote(s))
Helpful
Not helpful

Comments (0)
Post a new comment
 
 
Full Name:
Email:
Comments: