HMPL integration with JSON5
The new version of HMPL has integration with the JSON5 module, which will qualitatively improve the work several times! In this article, I would like to describe in more detail why this was done and why it is necessary.
About JSON5
First of all, it is worth noting the long-standing problem with JSON, which exists in JavaScript, and indeed in almost any other programming language that works with this format.
When working with objects, it seems that everything is convenient and practical. JSON is very convenient and why do we need additional packages at all if there is JSON.parse and JSON.stringify, which will be useful in almost all cases of work? But, let's take this code:
const user = { id: 0, name: "Tony", age: 43, hobbies: ["Building anthill"], }; const jsonString = JSON.stringify(user);
Now, let's output to the console what we got:
console.log(jsonString); // output - {"id":0,"name":"Tony","age":43,"hobbies":["Building anthill"]}
We got a seemingly ordinary string, which can easily be translated back with parsing and everything is cool, only this string takes up "a lot" of space on the disk, and also imagine if you write all this manually, and not in JavaScript through a convenient object? Yes, this is the main problem with this functionality.
If we take the same JavaScript object and write it normally in a string, then JSON.Parse will not parse it, giving an error:
const userString = `{ id: 0, name: "Tony", age: 43, hobbies: ["Building anthill"], }` JSON.parse(userString); // Uncaught SyntaxError: Expected property name or '}' in JSON at position 6 (line 2 column 5) // at JSON.parse (<anonymous>)
To fix this, we will have to adjust the string to the format. To do this, we will have to manually constantly write double quotes near the object properties. We will always have to not put a comma at the end, not write comments in the string like in JS, etc. We kind of get convenient functionality, but if we consider it as something that we will write manually, then it is simply incredibly inconvenient, because everyone is used to writing a JS object manually, and not JSON.
So, the JSON5 module allows you to write strings almost like in JS and not have the problems described above:
import JSON5 from "json5"; const userString = `{ id: 0, name: "Tony", age: 43, hobbies: ["Building anthill"], }` JSON5.parse(userString); /* { id: 0, name: "Tony", age: 43, hobbies: ["Building anthill"], }; */
Even the slogan of the module itself says: "JSON5 – JSON for Humans".
Of course, the problem may seem insignificant at first glance, but only until it becomes a daily issue. It's okay to correct quotes once, remove a comma, but doing it manually every day is incredibly tedious. Therefore, as one of the modules that solves this problem, it is the best fit for all of this.
You can even remember not abstract examples, but specific ones that are used in work. Have you ever configured a configuration file when working with some code assemblers or something else? The same linters, prefixers, module builders, various text editors - all this works, including through JSON.
And, usually, such files are filled manually by users. And, now there is a need to parse such moments, for example, here is an example of a config:
const user = { id: 0, name: "Tony", age: 43, hobbies: ["Building anthill"], }; const jsonString = JSON.stringify(user);
And, such an object also needs to be parsed on the application side. These parameters can be any and in any format, as well as with comments and other jokes. Manually writing such a parser is not cost-effective, and simply unnecessary.
About the benefits for HMPL
The HMPL module is based on extended HTML markup, to which we pass objects, and at the output we receive a ready component from the server. Let's take an example of the code:
console.log(jsonString); // output - {"id":0,"name":"Tony","age":43,"hobbies":["Building anthill"]}
Before version 2.2.0, the module was based on JSON.parse, so this whole thing was completely inconvenient. The stringify function was introduced, which somehow bypassed this point, but it's clear that it's still in separate ones .hmpl files js code will be problematic to write. Here is an example of stringify and a file:
const userString = `{ id: 0, name: "Tony", age: 43, hobbies: ["Building anthill"], }` JSON.parse(userString); // Uncaught SyntaxError: Expected property name or '}' in JSON at position 6 (line 2 column 5) // at JSON.parse (<anonymous>)
mail.hmpl:
import JSON5 from "json5"; const userString = `{ id: 0, name: "Tony", age: 43, hobbies: ["Building anthill"], }` JSON5.parse(userString); /* { id: 0, name: "Tony", age: 43, hobbies: ["Building anthill"], }; */
Therefore, I think that integration with JSON5 is the best way to make sites even faster and smaller in size. Now, it's generally super convenient, because you can just copy an object from JavaScript and paste it into an HTML file.
By the way, the module repository itself can be found here. It will be cool if you rate it with a star ☆ if you think this module is useful :)
hmpl-language
/
hmpl
? Server-oriented customizable templating for JavaScript

Server-oriented customizable templating for JavaScript
Usage
import hmpl from "hmpl-js"; const templateFn = hmpl.compile( `<div> <button data-action="increment"> <div> <div> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24"><title>Enter fullscreen mode</title> <path d="M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z"></path> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24"><title>Exit fullscreen mode</title> <path d="M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z"></path> </svg> </div> </div>
Why HMPL?
Using…
The above is the detailed content of HMPL integration with JSON5. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.
