![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
diet-ect-fix
Advanced tools
ECT based diet plugin for rendering Dynamic HTML files. With some fixes.
HTML template engine module for diet based on ect. The Fastest JavaScript template engine with embedded CoffeeScript syntax.
Adam appears to have abandoned Diet, and I'm sick of having to manually fix this every time I start a new Diet project.
This fork fixes 2 problems:
{{ if this.page is 'home' :}}
<h1>Users:</h1>
<ul>
{{for user in this.users :}}
<li>{{-user.name}} is cool.</li>
{{end}}
</ul>
{{ else :}}
<h1>404 Page not Found</h1>
{{ end }}
npm install diet-ect-fix
Please note the new v1.0.x
only works with diet >=v0.9.17
You can learn to use ECT quickly with the Quick Syntax Guide
below or in more detail on ECT's websites. Please note diet-ect
by default uses differnt open and curly braces {{
and }}
, while ECT by default uses uses <%
and %>
.
~/yourApp/index.js
// Initialize Server
var server = require('diet') // Require Diet
var app = server() // Create App
app.listen(8000) // Configure Domain
// Require ECT
var ect = require('diet-ect')({ path: app.path+'/static' })
// Load ECT as a global header
app.header(ect)
// Listen on GET /
// ECT is a global header so you can access it
// from every route with the `$.html()` method
app.get('/', function($){
// Set a template variable
$.data.myVar = 'ect'
// Now serve the html file
// by default: /your_app/static/index.html
// use the `root` config to change this
$.html()
})
~/yourApp/static/index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World at {{-this.myVar}}</h1>
</body>
</html>
You can access anything defined in the $
signal from your templates including:
$.query // in template {{ this.query }}
$.body // in template {{ this.body }}
$.params // in template {{ this.params }}
$.url // in template {{ this.query}}
$.data // in template {{ this }} data is a special variable
$.data
object has a priority and will overwrite the variables from the signal.$.data
variable is directly accessible like {{-this.myVar}}
instead of {{-this.data.myVar}}
.$.data
object is also used by the $.json()
response making APIs easier to build.By default $.html()
will serve an index.html
file relativ to the root
config.
$.html('yourFile.html') // will serve /your_app/static/yourFile.html
You can change the file when you call $.html()
by passing in an argument with a different path.
You can use any config that ECT already has. These are the defaults with diet-ect
:
require('diet-ect')({
root : '/',
ext: '.html',
open: '{{', close: '}}',
cache: true,
watch: true,
gzip: true
})
<h1>{{- this.title }}</h1>
<code>{{= this.source }}</code>
{{if this.is :}}
then show this
{{end}}
{{ if this.is :}}
then show this
{{ else :}}
otherwise show this
{{ end }}
{{ if this.is :}}
then show this
{{ else if this.otherwise :}}
otherwise show this
{{ else :}}
every other way show this
{{ end }}
Let's say this is our project structure:
/views
....../html
............/header.html
............/home.html
............/about.html
And we would like to resue header.html
in both home.html
and about.html
.
This is header.html
:
<section>
<a id="logo">A Wonderful Service</a>
</section>
And this is home.html
:
{{ include 'header' }}
<h1>Welcome to my homepage</h1>
...
And this is about.html
:
{{ include 'header' }}
<h1>About Me</h1>
...
Voala! You can use .html
in the include string but it is not necessary. Please note if you define a new variable inside a template it will only be passed to included files if it was defined within the this
variable.
The data:
$.data.letters = ['A', 'B', 'C']
The template:
{{for letter in this.letters :}}
{{-letter}} <br>
{{end}}
The output:
A
B
C
The data:
$.data.capitals = { a: 'A', b: 'B', c: 'C' }
The template:
{{for key, value of this.capitals :}}
{{-key}}: {{-value}} <br>
{{end}}
The output:
a: A
b: B
c: C
Please note if you would like to iterate over just the keys that are defined on the object itself, by adding a hasOwnProperty
check to avoid properties that may be inherited from the prototype, use the own directive in the looping syntax like:
{{for own key, value of object :}}
{{-key}}: {{-value}} <br>
{{end}}
The data:
$.data.grades = [
{ student: 'Arnold', value: 'A+' },
{ student: 'Billy', value: 'B+' },
{ student: 'Carol', value: 'C-' }
];
The template:
{{for grade in this.grades :}}
{{-grade.student}} received {{-grade.value}} <br>
{{end}}
The output:
Arnold received A+
Billy received B+
Carol received C-
The data:
$.data.letters = ['A', 'B', 'C']
The template:
{{for value, index in this.letters :}}
{{-index}}. {{-value}} <br>
{{end}}
The output:
1. A
2. B
3. C
{{ link = (url, text) -> }}
<li><a href="{{- url }}">{{- text }}</a></li>
{{ end }}
<ul>
{{-link '/', 'home'}}
{{-link '/about', 'about'}}
{{-link '/contact', 'contact'}}
</ul>
Please note you cannot access template variables that are created locally within the template unless it is defined as a property of the global context object this
.
This IS working:
<!-- define GLOBAL template variable -->
{{this.preText = 'Learn more about '}}
{{ myFunction = (text) -> }}
<li>{{- this.preText }} {{- text }}</li>
{{ end }}
<!-- call the function -->
{{-text 'football'}}
{{-text 'hockey'}}
This IS NOT working:
<!-- define LOCAL template variable -->
{{preText = 'Learn more about: '}}
<!-- create function -->
{{ myFunction = (text) -> }}
<li>{{- preText }} {{- text }}</li>
{{ end }}
<!-- call the function -->
{{-text 'football'}}
{{-text 'hockey'}}
:
As you can see the colon punctuation :
is sometimes used and sometimes not. It can be confusing at the beginning but hopefully this will save you some time:
The :
is only needed for if
else-if
else
and for
directives - for everything else you don't need it.
To learn more about the syntax check out ECT.js and CoffeeScript.
(The MIT License)
Copyright (c) 2014 Halász Ádám mail@adamhalasz.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
ECT based diet plugin for rendering Dynamic HTML files. With some fixes.
The npm package diet-ect-fix receives a total of 3 weekly downloads. As such, diet-ect-fix popularity was classified as not popular.
We found that diet-ect-fix demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.