Skip to content

Compile

Tyler Bounty edited this page Jun 23, 2020 · 5 revisions

Cache

In order to increase the rendering speed, eryn uses a cache.

When you render a file, it is first compiled and then placed in the cache. The cache associates the file path with the compiled data, such that if you render the same file in the future, the cache entry will used and the file won't be compiled again.

Compiling Files

As previously stated, the render function automatically compiles files that are not in the cache. However, you can also manually compile a file, or even a whole directory. This is useful, for example, when you want to compile all the files when the server starts, so the render function can just directly render from the cache.

The compile function doesn't return anything, as all it does is to add an entry to the cache (the file path, along with the compiled data).

You can compile a single file like this:

eryn.compile("/path/to/file.eryn");

Note that it is recommended to specify the absolute path to the file:

eryn.compile(path.join(__dirname, "file.eryn"));

This is because the cache associates the file path (that you pass) to the compiled data. So, if you compile a file like this:

eryn.compile("file.eryn");

...and render it like this:

eryn.render(path.join(__dirname, "file.eryn"), { });

The file will be recompiled, because there is no entry in the cache with that exact path. So, you should avoid passing relative paths, and you should make sure that the paths are identical (e.g. no different or additional characters, like \ instead of /, or uppercase instead of lowercase).

This is because the cache uses a hash map behind the scenes, and uses the paths as keys for performance reasons.

Exceptions

If an error occurs while compiling the file, it will be thrown as an exception.

Compiling Directories

You can also compile all files in a directory, that match a specific filter, by using the compileDir function.

The compileDir function also doesn't return anything, as all it does is to add an entry to the cache (the file path, along with the compiled data) for each file it compiles.

eryn.compileDir("/path/to/dir", [ "*.eryn" ]);

The supported filter globs are gitignore-like. This means you can do something like:

eryn.compileDir("/path/to/dir", [ "!*/", "*.eryn" ]);

...which only compiles the files that match *.eryn, and will also not search in the subdirectories.

Please note that the order of the filters matters.

eryn.compileDir("/path/to/dir", [ "!*/", "components/", "*.eryn" ]);

This will compile all files that match *.eryn from the current directory, as well as the components directory, but not any other subdirectories.

eryn.compileDir("/path/to/dir", [ "components/", "!*/", "*.eryn" ]);

...while this will compile all files that match *.eryn only from the current directory. You can see that the !*/ filter will overwrite the components/ filter.

Exceptions

If an error occurs while compiling a file, it will be printed on the screen, but no exception will be thrown.

This behavior can be changed with the throwOnCompileDirError option, which is false by default. since 0.2.0

Compiling Strings since 0.2.0

It is also possible to compile strings. Here's the syntax:

eryn.compileString("alias", "content goes here");

These strings can be rendered using the renderString function. Please note that, unlike normal files, strings must be compiled before being rendered.

eryn.compileString("testString", "Hello, [| context.name |]");

var data = eryn.renderString("testString", { name: "Tyler" });

Directly rendering a string containing eryn templates will not work.

If the string contains component templates, the component path will actually be an alias of a cache entry. If that entry isn't present in the cache, an exception will be thrown.

It's recommended to only use other string aliases as components, because using other files might be more difficult (i.e. you have to provide the exact full path that was used when it was compiled).

Intro

    Home

    Getting Started

Engine Basics

    Context

    Templates

    Local

    Shared

    Modes

Functions

    Compile

    Render

    Options

Other

    Security Concerns

    Known Issues

Clone this wiki locally