Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #298: Views Namespace #300

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Eta is a lightweight and blazing fast embedded JS templating engine that works i
- Great error reporting
- ⚡️ Exports ES Modules as well as UMD
- 📝 Easy template syntax

- Namespaces for easy tempalte include
## Get Started

_For more thorough documentation, visit [https://eta.js.org](https://eta.js.org)_
Expand Down Expand Up @@ -84,8 +84,33 @@ const eta = new Eta({ views: path.join(__dirname, "templates") });

const res = eta.render("./simple", { name: "Ben" });
console.log(res); // Hi Ben!



```
Using namespaces
```ts
import { Eta } from "eta";
const eta = new Eta({ namespaces: {
'@myroot' : path.join(__dirname, "templates"),
'@includes' : path.join(__dirname, "templates",'includes'),
'@components' : path.join(__dirname, "templates",'components')
} });

const res = eta.render("@myroot/sample", { name: "Ben" });
console.log(res); // Hi Ben!

```
Namespace usage within template

```html
<% include('@includes/header') %>
<div>
<h1>Welcome to the Index Page</h1>
<p>Here's a widget:</p>
<% include('@components/widget') %>
</div>
```
## FAQs

<details>
Expand Down
23 changes: 23 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ export interface EtaConfig {

/** Control template file extension defaults. Default `.eta` */
defaultExtension?: string;


/**
* Give namespaces to view paths with `@prefix`.
*
* Example:
* ```typescript
* const eta = new Eta({
* namespaces: {
* '@includes': path.join(__dirname, 'src', 'views', 'includes')
* }
* });
*
* // Usage in code:
* eta.render('@includes/header', { title: 'Home' });
* ```
*
* Within a template, you can use the namespace like this:
* ```html
* <% include('@includes/header') %>
* ```
*/
namespaces?: { [key: string]: string };
}

/* END TYPES */
Expand Down
46 changes: 42 additions & 4 deletions src/file-handling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import type { Options } from "./config.ts";

export function readFile(this: EtaCore, path: string): string {
let res = "";
if(path.startsWith('@') && this.config.namespaces){
path = resolveNamespace.call(this,path) ?? path;
}

try {
res = fs.readFileSync(path, "utf8");
Expand All @@ -26,23 +29,58 @@ export function readFile(this: EtaCore, path: string): string {
return res;
}

/**
* Funtion resolves namespace path provided on namespaces in config
*/
function resolveNamespace(this: EtaCore,templatePath : string) : string | null{
const defaultExtension = this.config.defaultExtension === undefined
? ".eta"
: this.config.defaultExtension;
if(this.config.namespaces){
const entries = Object.keys(this.config.namespaces);
const namespace = entries.find(namespaceKey => {
return templatePath.startsWith(namespaceKey);
});
if(namespace){
templatePath = templatePath.replace(namespace,this.config.namespaces[namespace]);
templatePath += path.extname(templatePath) ? "" : defaultExtension;
return templatePath;
}
}
return null;
}





export function resolvePath(
this: EtaCore,
templatePath: string,
options?: Partial<Options>,
): string {
let resolvedFilePath = "";
const defaultExtension = this.config.defaultExtension === undefined
? ".eta"
: this.config.defaultExtension;
const baseFilePath = options && options.filepath;


// Check if tempalte path has a namespace constant at the begining
if(templatePath.startsWith('@') && this.config.namespaces){
resolvedFilePath = resolveNamespace.call(this,templatePath) ?? "";
return resolvedFilePath;
}


const views = this.config.views;


if (!views) {
throw new EtaFileResolutionError("Views directory is not defined");
}

const baseFilePath = options && options.filepath;
const defaultExtension = this.config.defaultExtension === undefined
? ".eta"
: this.config.defaultExtension;


// how we index cached template paths
const cacheIndex = JSON.stringify({
Expand Down
7 changes: 4 additions & 3 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function handleCache(
? this.templatesAsync
: this.templatesSync;

if (this.resolvePath && this.readFile && !template.startsWith("@")) {
if (this.resolvePath && this.readFile) {
const templatePath = options.filepath as string;

const cachedTemplate = templateStore.get(templatePath);
Expand All @@ -25,6 +25,7 @@ function handleCache(
} else {
const templateString = this.readFile(templatePath);


const templateFn = this.compile(templateString, options);

if (this.config.cache) templateStore.define(templatePath, templateFn);
Expand Down Expand Up @@ -54,7 +55,7 @@ export function render<T extends object>(
const options = { ...meta, async: false };

if (typeof template === "string") {
if (this.resolvePath && this.readFile && !template.startsWith("@")) {
if (this.resolvePath && this.readFile) {
options.filepath = this.resolvePath(template, options);
}

Expand All @@ -78,7 +79,7 @@ export function renderAsync<T extends object>(
const options = { ...meta, async: true };

if (typeof template === "string") {
if (this.resolvePath && this.readFile && !template.startsWith("@")) {
if (this.resolvePath && this.readFile) {
options.filepath = this.resolvePath(template, options);
}

Expand Down