Welcome to Vixeny templates! Here are some basics on how the template engine works. It’s recommended to learn about the dynamic path system via the link below.
We will be using a second server hosted on Deno Deploy
for the examples. You can ping the server here:
Disclaimers
Cold starting
might happen, but this is normal in the cloud environment; the average ping is 55ms.Let’s dive into the structure briefly to see how everything interacts.
The structure of a typical Vixeny project after installation and template selection looks like this:
./
├── main.ts
├── package.json
├── src/
│ ├── globalOptions.ts
│ ├── paths/
│ │ └── root.ts
│ └── plugins/
│ ├── tsx.ts
│ ├── typebox.ts
│ └── typescript.ts
├── tsconfig.json
├── views/
│ ├── public/
│ │ ├── $main.tsx
│ │ ├── css/
│ │ │ ├── ...
│ │ └── mjs/
│ │ └── main.ts
│ └── scripts/
│ └── addFooter.ts
└── watcher.mjs
wrap
and the handler
.ts
files, which are then imported into public
to convert to mjs
.Templates work by pointing at a directory and hosting it to the handler. It’s important to note that:
The next example assumes that you only installed pug
.
const fileServer = plugins.fileServer({
type: "fileServer",
// Hosted on
name: "/",
// Takes files from
path: "./views/public/",
// Removes extensions of
removeExtensionOf: [".html"],
// If a file starts with it, it will become the `/` of the directory
slashIs: "$main",
// Current templates in use:
template: [pugP, typescriptP],
});
You can find this file at src/globalOptions.ts
.
As we know from the core introduction, plugins only exist if they are included in the options
. Here is a basic implementation, but by default all files are set up in create-vixeny
.
You can find this file at src/plugins
.
import { wrap } from "vixeny";
import { options, request } from "./setup.ts";
const serve = wrap(options)()
.get({
path: "/hi",
method: "POST",
f: ({ typebox }) => typebox?.user ? typebox.user.userId : null,
})
.compose();
const response = await serveMock(request);
const body = await response.text();
console.log(
// 50
body,
);
Most of the templates have a:
default
: Provides an object to all the templates for compilation.preserveExtension
: Removes the extension of the file.petition
: Enables a semi-static route using a petition for all the routes in the wildcard.In the next example, we will use Pug. So, let’s create a file called hello.pug
.
Create a hello.pug
file:
p #{name}'s Pug source code!
Install the pug
package and set up src/plugins/pug.ts
:
// Getting dependencies
import * as pugModule from "pug";
import { composeResponse, plugins } from "vixeny";
import { pugStaticServerPlugin } from "vixeny-perspective";
const serve = composeResponse()([
{
type: "fileServer",
name: "/",
path: "./public/",
template: [
// Plugin
pugStaticServerPlugin(
// File Compiler
pugModule.compileFile,
)({
// Removes the `.pug` extension from the `path`
preserveExtension: false,
// Default case, this value will be passed to all the files
default: {
name: "avant",
},
}),
],
},
]);
Fetch the rendered page:
As we saw in the last example, you can have a default case, but Vixeny’s structure also allows you to have a petition for the whole staticFileServe
.
composer.objectNullRequest
To do this, we need composer.objectNullRequest
, where if this petition returns null, it will return the default case. Otherwise, it will pass the object to the template.
import { composer } from "vixeny";
const petition = composer.objectNullRequest()({
// If query.name exists, it returns the object query, otherwise it returns null
f: ({ query }) => query?.name ? query : null,
});
Modify pugStaticServerPlugin
to use the petition:
import { pugStaticServerPlugin } from "vixeny-perspective";
import { composer } from "vixeny";
import * as pugModule from "pug";
export default pugStaticServerPlugin(pugModule.compileFile)({
preserveExtension: false,
default: { name: "avant" },
// Adding a composed petition
petition: composer.objectNullRequest()({
f: ({ query }) => query?.name ? query : null,
}),
});
param in CTX would not work.
You can find this file at src/plugins
.
Thank you for your time.