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>) </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>) </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> </button> </div> <div> <h2 id="Why-HMPL">Why HMPL?</h2> </div> <p dir="auto">Using…</p>
The above is the detailed content of HMPL integration with JSON5. For more information, please follow other related articles on the PHP Chinese website!

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Enhance Your Code Presentation: 10 Syntax Highlighters for Developers Sharing code snippets on your website or blog is a common practice for developers. Choosing the right syntax highlighter can significantly improve readability and visual appeal. T

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

This article presents a curated selection of over 10 tutorials on JavaScript and jQuery Model-View-Controller (MVC) frameworks, perfect for boosting your web development skills in the new year. These tutorials cover a range of topics, from foundatio

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
