Skip to content

Commit b2539e9

Browse files
committed
Update README.md
1 parent 083affc commit b2539e9

File tree

1 file changed

+62
-61
lines changed

1 file changed

+62
-61
lines changed

README.md

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,68 @@ This will return the string “handlebars”, which is the corresponding engine
471471
472472
Gaining an understanding of this class will provide you with more options and possibilities when using Ecto.
473473
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.
481+
482+
| Name | Type | Description |
483+
| ------ | ------ | ------------------------------------------------- |
484+
| 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+
const ecto = new Ecto();
492+
493+
// Detect EJS templates
494+
const ejsEngine = ecto.detectEngine('<%= name %>');
495+
console.log(ejsEngine); // 'ejs'
496+
497+
// Detect Handlebars templates
498+
const hbsEngine = ecto.detectEngine('{{name}}');
499+
console.log(hbsEngine); // 'handlebars'
500+
501+
// Detect Markdown
502+
const mdEngine = ecto.detectEngine('# Hello World\n\nThis is markdown');
503+
console.log(mdEngine); // 'markdown'
504+
505+
// Detect Pug templates
506+
const pugEngine = ecto.detectEngine('div.container\n h1 Hello');
507+
console.log(pugEngine); // 'pug'
508+
509+
// Detect Nunjucks templates
510+
const njkEngine = ecto.detectEngine('{% block content %}Hello{% endblock %}');
511+
console.log(njkEngine); // 'nunjucks'
512+
513+
// Detect Liquid templates
514+
const liquidEngine = ecto.detectEngine('{% assign name = "John" %}{{ name | upcase }}');
515+
console.log(liquidEngine); // 'liquid'
516+
517+
// Returns default engine for plain text
518+
const defaultEngine = ecto.detectEngine('Plain text without template syntax');
519+
console.log(defaultEngine); // 'ejs' (or whatever is set as defaultEngine)
520+
```
521+
522+
### Detection Patterns
523+
524+
The `detectEngine` method recognizes the following syntax patterns:
525+
526+
- **EJS**: `<% %>`, `<%= %>`, `<%- %>`
527+
- **Handlebars/Mustache**: `{{ }}`, `{{# }}`, `{{> }}`
528+
- **Pug**: Indentation-based syntax without angle brackets
529+
- **Nunjucks**: `{% block %}`, `{% extends %}`, `{% include %}`
530+
- **Liquid**: `{% assign %}`, `{% capture %}`, pipe filters `{{ var | filter }}`
531+
- **Markdown**: Headers `#`, lists, code blocks, links, tables
532+
533+
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+
474536
# The Template Engines We Support
475537
476538
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
566628
* `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.
567629
* `.removeFrontMatter(source: string): string` - This function removes the frontmatter from the markdown file. It takes in a string and returns a string.
568630
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.
576-
577-
| Name | Type | Description |
578-
| ------ | ------ | ------------------------------------------------- |
579-
| 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-
const ecto = new Ecto();
587-
588-
// Detect EJS templates
589-
const ejsEngine = ecto.detectEngine('<%= name %>');
590-
console.log(ejsEngine); // 'ejs'
591-
592-
// Detect Handlebars templates
593-
const hbsEngine = ecto.detectEngine('{{name}}');
594-
console.log(hbsEngine); // 'handlebars'
595-
596-
// Detect Markdown
597-
const mdEngine = ecto.detectEngine('# Hello World\n\nThis is markdown');
598-
console.log(mdEngine); // 'markdown'
599-
600-
// Detect Pug templates
601-
const pugEngine = ecto.detectEngine('div.container\n h1 Hello');
602-
console.log(pugEngine); // 'pug'
603-
604-
// Detect Nunjucks templates
605-
const njkEngine = ecto.detectEngine('{% block content %}Hello{% endblock %}');
606-
console.log(njkEngine); // 'nunjucks'
607-
608-
// Detect Liquid templates
609-
const liquidEngine = ecto.detectEngine('{% assign name = "John" %}{{ name | upcase }}');
610-
console.log(liquidEngine); // 'liquid'
611-
612-
// Returns default engine for plain text
613-
const defaultEngine = ecto.detectEngine('Plain text without template syntax');
614-
console.log(defaultEngine); // 'ejs' (or whatever is set as defaultEngine)
615-
```
616-
617-
### Detection Patterns
618-
619-
The `detectEngine` method recognizes the following syntax patterns:
620-
621-
- **EJS**: `<% %>`, `<%= %>`, `<%- %>`
622-
- **Handlebars/Mustache**: `{{ }}`, `{{# }}`, `{{> }}`
623-
- **Pug**: Indentation-based syntax without angle brackets
624-
- **Nunjucks**: `{% block %}`, `{% extends %}`, `{% include %}`
625-
- **Liquid**: `{% assign %}`, `{% capture %}`, pipe filters `{{ var | filter }}`
626-
- **Markdown**: Headers `#`, lists, code blocks, links, tables
627-
628-
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-
630631
# Caching on Rendering
631632
632633
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

Comments
 (0)