Version: v0.8.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 fun 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

Loading Multiple Modules

You can use a wildcard * to load all modules in a given directory.

load('subdir/*')

It returns a map of the loaded modules that can be used directly.

For example:

$modules = load('colors/*')
foreach $modules as $modName/$mod {
    print($modName ~ ': ' ~ $mod.getRgb())
}
// RedColor: #ff0000
// GreenColor: #00ff00
// BlueColor: #0000ff

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

fun 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