Version: v0.7.1 - Beta.  We welcome contributors & feedback.

Custom Modules

You may already be familiar with THT's built-in modules, like Math and Input.

You can create your own modules, which let you organize your functions and make them accessible from any page (or another module) in your app.

Defining a Module 

To define a module, create a .tht file in the code/modules folder.

Module names are always UpperCamelCase.

code/modules/MyModule.tht
code/modules/blog/BlogPost.tht

Module Functions 

Functions must be declared public so they can be called outside of the module.

// modules/MyModule.tht
//--------------------------------------

public fn sayHello($name) {
    print('Hello, ' ~ $name ~ '!')
}

To call a module function, use dot . notation.

// pages/myPage.tht
//--------------------------------------

MyModule.sayHello('Tez')

//= 'Hello, Tez!'

Loading Modules 

Auto-Loading

If the module is in the top-level modules directory, it will be automatically loaded when one of its methods is called.

MyModule.hello()  // modules/MyModule.tht

Manual Loading

Use the load function to manually import a module into the current file.

This is required if the module is under a subdirectory.

// pages/blog.tht
//---------------------------------

load('blog/Article')  // modules/blog/Article.tht

Article.addToViewCount()

Relative Paths

Modules can load other modules using relative paths (. and ..).

// modules/blog/Article.tht
//---------------------------------

load('./Comment')  // modules/blog/Comment.tht

Module Fields 

You can assign data fields to a module using the @@ (“this module”) symbol.

These fields can be accessed anywhere within the current module.

// modules/MyApp.tht
//--------------------------------------

@@.numUsersCreated = 0

fn newUser {

    ...

    @@.numUsersCreated += 1
}

Module Constants 

Any module field that begins with a capital letter is a read-only constant.

@@.ContactEmail = 'hello@example.com'

@@.MaxPostsPerDay = 5

@@.Color = {
    Red
    Blue
    Green
}

Outside Access

Module constants can be accessed outside the module.

// pages/home.tht
//--------------------------------------

$contact = 'My email is: ' ~ MyApp.ContactEmail