You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+62-61Lines changed: 62 additions & 61 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -471,6 +471,68 @@ This will return the string “handlebars”, which is the corresponding engine
471
471
472
472
Gaining an understanding of this class will provide you with more options and possibilities when using Ecto.
473
473
474
+
## Detect Template Engine
475
+
476
+
Ecto provides a `detectEngine` method that can automatically detect the template engine from a template string by analyzing its syntax patterns. This is useful when you receive template content but don't know which engine it uses.
477
+
478
+
## detectEngine(source: string): string
479
+
480
+
The `detectEngine` method analyzes the template syntax and returns the detected engine name. If no specific template syntax is found, it returns the configured default engine.
| source | string | The template source string to analyze |
485
+
486
+
**Returns:** The detected engine name ('ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid') or the default engine if no specific syntax is detected.
487
+
488
+
### Basic Usage
489
+
490
+
```javascript
491
+
constecto=newEcto();
492
+
493
+
// Detect EJS templates
494
+
constejsEngine=ecto.detectEngine('<%= name %>');
495
+
console.log(ejsEngine); // 'ejs'
496
+
497
+
// Detect Handlebars templates
498
+
consthbsEngine=ecto.detectEngine('{{name}}');
499
+
console.log(hbsEngine); // 'handlebars'
500
+
501
+
// Detect Markdown
502
+
constmdEngine=ecto.detectEngine('# Hello World\n\nThis is markdown');
Note: For ambiguous syntax (like simple `{{ }}` which could be Handlebars, Mustache, or Liquid), the method makes intelligent decisions based on additional context clues in the template.
534
+
535
+
474
536
# The Template Engines We Support
475
537
476
538
A template engine is a tool that allows developers to write HTML markup that contains the template engine’s defined tags and syntax. These tags are used to insert variables into the final output of the template, or run some programming logic at run-time before sending the final HTML to the browser for display.
@@ -566,67 +628,6 @@ Ecto has added in some helper functions for frontmatter in markdown files. Front
566
628
* `setFrontMatter(source:string, data: Record<string, unknown>)` - This function sets the front matter even if it already exists and returns the full source with the new front matter.
567
629
* `.removeFrontMatter(source: string): string` - This function removes the frontmatter from the markdown file. It takes in a string and returns a string.
568
630
569
-
# Detect Template Engine
570
-
571
-
Ecto provides a `detectEngine` method that can automatically detect the template engine from a template string by analyzing its syntax patterns. This is useful when you receive template content but don't know which engine it uses.
572
-
573
-
## detectEngine(source: string): string
574
-
575
-
The `detectEngine` method analyzes the template syntax and returns the detected engine name. If no specific template syntax is found, it returns the configured default engine.
| source | string | The template source string to analyze |
580
-
581
-
**Returns:** The detected engine name ('ejs', 'markdown', 'pug', 'nunjucks', 'handlebars', 'liquid') or the default engine if no specific syntax is detected.
582
-
583
-
### Basic Usage
584
-
585
-
```javascript
586
-
constecto=newEcto();
587
-
588
-
// Detect EJS templates
589
-
constejsEngine=ecto.detectEngine('<%= name %>');
590
-
console.log(ejsEngine); // 'ejs'
591
-
592
-
// Detect Handlebars templates
593
-
consthbsEngine=ecto.detectEngine('{{name}}');
594
-
console.log(hbsEngine); // 'handlebars'
595
-
596
-
// Detect Markdown
597
-
constmdEngine=ecto.detectEngine('# Hello World\n\nThis is markdown');
Note: For ambiguous syntax (like simple `{{ }}` which could be Handlebars, Mustache, or Liquid), the method makes intelligent decisions based on additional context clues in the template.
629
-
630
631
# Caching on Rendering
631
632
632
633
Ecto has a built-in caching mechanism that is `disabled by default` that allows you to cache the rendered output of templates. This is useful for improving performance and reducing the number of times a template needs to be rendered. There are currently two caching engines available: `MemoryCache` and `FileCache`.
0 commit comments