Trongate Website Homepage

How To Create Your Own Website Template

There now follows some instructions on how to create a basic HTML template, within the context of the Trongate framework. The example provided here will be basic but functional.

Where Are The Templates Stored?

The Trongate framework has a module called 'templates'. This module exists at the root directory level. The 'templates' module has the same general structure and features of any ordinary module. So, all the functionality that you'd expect to see in an ordinary Trongate controller file is available from inside the 'Templates.php' controller file. As well as having a 'controllers' directory, the 'templates' module also has a 'views' directory.

locating the 'templates' module

Now you know where the templates are to be stored. So, let's create an HTML template!

Step 1: Come up with a meaningful name

The first part of building your own template is to come up with a meaningful and sensible name for your website template. A meaningful name is a name that tells you something about where the template appears and/or how the template has been built. For example, if the template happens to use a CSS framework like Bootstrap or Tailwind then it may be helpful to include those CSS framework names a part of your template name. Examples of good template names (in snake_case) are:

In case you're wondering, the name of the template for the webpage that you are viewing is 'docs_template'.

Step 2: Create A Method For Your New Template

Let's assume that you have decided to call your template 'public'. With that decision made, you should now go into your 'templates' module and then open up your 'Templates.php' controller file.

Your goal here is to add a new method onto the 'templates' controller. This new method will be used to load your template. Below is an example of the basic syntax required to load a custom HTML template. In this example, the name of the template will be 'public':


function public() { 
  load("public");
}

As you can see, our method is simple and - in this example - it contains only one line of code. The line of code, contained within our method, calls upon Trongate's inbuilt load() method. The load() method initiates a request to serve a view file called 'public.php'. This view file should be created and stored inside the views directory of our 'templates' module.

The "template" controller file (Templates.php) will already contain code for things like an 'admin' template and an 'error_404' template. Whatever the name of your template is going to be, make sure it's unique. Also, don't be intimidated by other code that's already on Template.php. Simply paste your own method into the Templates class and you'll be good to go!

Step 3: Create A View File For Your Template

Now, create a file named public.php and store it inside the "views" directory, within your "templates" module. Your view file should contain the HTML code for generating an HTML webpage.

screenshot showing the location of the new template file (public.php)

The precise code that goes onto your HTML template is a matter for you to decide. Hopefully, you'll be able to design templates that look nice! For the purposes of demonstration, below is very a basic example of a template view file.

example of a template file

In this example, we're saving the view file as public.php. However, if the template had a different name then our view file would be named differently. For example, if our template was called 'members_area' then the corresponding view file, for our template, would be saved as members_area.php.

Now, you've created a template. In the next page you'll learn how to load your template from any existing Trongate module.