
洪 民憙 (Hong Minhee)
@hongminhee@hollo.social
@FediChatBot What's the most modern way to emit HTML while using a server framework like Hono, Koa, or Express? Oh, I can't use JSX because I can't use any transpilers.
@hongminhee@hollo.social
@FediChatBot What's the most modern way to emit HTML while using a server framework like Hono, Koa, or Express? Oh, I can't use JSX because I can't use any transpilers.
@FediChatBot@fedichatbot.deno.dev · Reply to 洪 民憙 (Hong Minhee)'s post
That's an interesting question! If you can't use JSX or any transpilers, you're looking for a way to generate HTML directly from your server-side code. Here are a few modern approaches you can consider, along with their pros and cons:
1. Template Literals (Tagged Templates)
How it works: You can use JavaScript's template literals (backticks ``) to create multiline strings that include variables. You can even use tagged templates to create custom functions that process the template string before returning the final HTML.
Example:
function html(strings, ...values) {
let result = "";
for (let i = 0; i < strings.length; i++) {
result += strings[i];
if (i < values.length) {
result += String(values[i]); // Basic escaping
}
}
return result;
}
const title = "My Page";
const content = "<p>Hello, world!</p>";
const page = html`
<!DOCTYPE html>
<html>
<head>
<title>${title}</title>
</head>
<body>
${content}
</body>
</html>
`;
Pros:
Cons:
2. String Concatenation
How it works: You can build HTML strings by concatenating regular strings and variables.
Example:
const title = "My Page";
const content = "<p>Hello, world!</p>";
const page =
"<!DOCTYPE html>\n" +
"<html>\n" +
" <head>\n" +
' <title>' + title + '</title>\n' +
" </head>\n" +
" <body>\n" +
content +
" </body>\n" +
"</html>";
Pros:
Cons:
3. HTML-Generating Libraries (Minimalist)
How it works: There are some lightweight libraries that provide a more structured way to generate HTML using JavaScript functions or objects. These libraries typically don't require a transpiler.
Example (using a hypothetical library):
import { html, head, title, body, p } from 'minimal-html-lib';
const page = html(
head(
title("My Page")
),
body(
p("Hello, world!")
)
);
Pros:
Cons:
4. Server-Side Templating Engines (Without Transpilers)
How it works: Some server-side templating engines can be used directly in JavaScript without a transpiler. These engines typically use a special syntax within HTML files to embed dynamic content.
Example (using a hypothetical engine):
<!-- mypage.html -->
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
{{ content }}
</body>
</html>
import { render } from 'simple-template-engine';
import fs from 'node:fs/promises';
const template = await fs.readFile('./mypage.html', 'utf-8');
const page = render(template, { title: "My Page", content: "<p>Hello, world!</p>" });
Pros:
Cons:
Recommendation
For most cases, template literals with a custom tagged template function (like the html
function in the first example) are a good balance between simplicity and flexibility. They're built into JavaScript, easy to use for basic HTML, and can be extended for more complex scenarios.
If you're dealing with very complex HTML structures, you might consider a minimalist HTML-generating library or a server-side templating engine, but be mindful of the added complexity and dependencies.
No matter which approach you choose, always remember to escape HTML entities to prevent XSS vulnerabilities.
I hope this helps! Let me know if you have any other questions.
@thisismissem@hachyderm.io · Reply to 洪 民憙 (Hong Minhee)'s post
@hongminhee maybe edge.js? Or nunjucks?