GitHub
(原名PDQuickUI,從0.6.0版本開始更名為QuickUI)
QuickUI是一個源自PDRenderKit的前端渲染框架,專注於增強前端框架功能。
透過整合虛擬DOM,重寫渲染邏輯,提高渲染效率,實現更快的資料觀察和自動更新。
該專案從 PDRenderKit 中刪除了原型擴展,以確保相容性和效能,使其適合複雜的應用程式。
它提供模組和非模組版本,並將 PDRenderKit 中的授權從 GPL-3.0 更改為 MIT。
從 npm 安裝
npm i @pardnchiu/quickui
包含來自 CDN
直接包含QuickUI
<!-- Version 0.6.0 and above --> <script src="https://cdn.jsdelivr.net/npm/@pardnchiu/quickui@[VERSION]/dist/QuickUI.js"></script> <!-- Version 0.5.4 and below --> <script src="https://cdn.jsdelivr.net/npm/pdquickui@[VERSION]/dist/PDQuickUI.js"></script>
模組版本
// Version 0.6.0 and above import { QUI } from "https://cdn.jsdelivr.net/npm/@pardnchiu/quickui@[VERSION]/dist/QuickUI.esm.js"; // Version 0.5.4 and below import { QUI } from "https://cdn.jsdelivr.net/npm/pdquickui@[VERSION]/dist/PDQuickUI.module.js";
初始化 QUI
const app = new QUI({ id: "", // Specify rendering element data: { // Custom DATA }, event: { // Custom EVENT }, when: { before_render: function () { // Stop rendering }, rendered: function () { // Rendered }, before_update: function () { // Stop updating }, updated: function () { // Updated }, before_destroy: function () { // Stop destruction }, destroyed: function () { // Destroyed } } });
自動渲染:偵測到資料變更時自動重新載入。
屬性概述
Attribute | Description |
---|---|
{{value}} | Inserts text into HTML tags and automatically updates with data changes. |
:path | Used with the temp tag to load HTML fragments from external files into the current page. |
:html | Replaces the element's innerHTML with text. |
:for | Supports formats like item in items, (item, index) in items, (key, value) in object. Iterates over data collections to generate corresponding HTML elements. |
:if :else-if :elif :else |
Displays or hides elements based on specified conditions, enabling branching logic. |
:model | Binds data to form elements (e.g., input), updating data automatically when input changes. |
:hide | Hides elements based on specific conditions. |
:animation | Specifies transition effects for elements, such as fade-in or expand, to enhance user experience. |
:mask | Controls block loading animations, supporting `true |
:[attr] | Sets element attributes, such as ID, class, image source, etc. Examples: :id/:class/:src/:alt/:href... |
:[css] | Sets element CSS, such as margin, padding, etc. Examples: :background-color, :opacity, :margin, :top, :position... |
@[event] | Adds event listeners that trigger specified actions upon activation. Examples: @click/@input/@mousedown... |
文字替換
index.html
npm i @pardnchiu/quickui
結果
<!-- Version 0.6.0 and above --> <script src="https://cdn.jsdelivr.net/npm/@pardnchiu/quickui@[VERSION]/dist/QuickUI.js"></script> <!-- Version 0.5.4 and below --> <script src="https://cdn.jsdelivr.net/npm/pdquickui@[VERSION]/dist/PDQuickUI.js"></script>
index.html
// Version 0.6.0 and above import { QUI } from "https://cdn.jsdelivr.net/npm/@pardnchiu/quickui@[VERSION]/dist/QuickUI.esm.js"; // Version 0.5.4 and below import { QUI } from "https://cdn.jsdelivr.net/npm/pdquickui@[VERSION]/dist/PDQuickUI.module.js";
結果
const app = new QUI({ id: "", // Specify rendering element data: { // Custom DATA }, event: { // Custom EVENT }, when: { before_render: function () { // Stop rendering }, rendered: function () { // Rendered }, before_update: function () { // Stop updating }, updated: function () { // Updated }, before_destroy: function () { // Stop destruction }, destroyed: function () { // Destroyed } } });
插入區塊
> [!注意]
>確保在測試時停用瀏覽器中的本機檔案限製或使用即時伺服器。
test.html
<h1>{{ title }}</h1> const app = new QUI({ id: "app", data: { title: "test" } });
index.html
<h1>test</h1>
結果
const app = new QUI({ id: "app", data: { html: "<b>innerHtml</b>" } });
循環渲染
index.html
<b>innerHtml</b>
結果
<h1>path heading</h1> <p>path content</p>
結果
const app = new QUI({ id: "app" });
條件渲染
index.html
<h1>path heading</h1> <p>path content</p>
結果:標題 = 1
<ul> <li>{{ item }} {{ CALC(index + 1) }}</li> </ul> const app = new QUI({ id: "app", data: { ary: ["test1", "test2", "test3"] } });
結果:heading = null && isH2 = true
<li>
index.html
結果:標題 = 3 && isH2 = null
<ul> <li>food: Food <ul> <li>Snacks <ul> <li>1. Potato Chips - </li> <li>2. Chocolate - </li> </ul> </li> <li>Beverages <ul> <li>1. Juice - </li> <li>2. Tea - </li> </ul> </li> </ul> </li> <li>home: Home <ul> <li>Furniture <ul> <li>1. Sofa - 0</li> <li>2. Table - 0</li> </ul> </li> <li>Decorations <ul> <li>1. Picture Frame - </li> <li>2. Vase - </li> </ul> </li> </ul> </li> </ul>
結果:heading = null && isH2 = null
<h1>{{ title }} {{ heading }}</h1> <h2>{{ title }} {{ heading }}</h2> <h3>{{ title }} {{ heading }}</h3> <h4>{{ title }} {{ heading }}</h4> const app = new QUI({ id: "app", data: { heading: [Number|null], isH2: [Boolean|null], title: "test" } });
模板渲染
index.html
<h1>test 1</h1>
結果
<h2>test </h2>
綁定
<h3>test 3</h3>
活動
<h4>test </h4>
CSS
> [!注意]
>支援使用:[CSS屬性]進行簡單設置,直接將資料綁定到樣式屬性。
index.html
const test = new QUI({ id: "app", data: { hint: "hint 123", title: "test 123" }, render: () => { return ` "{{ hint }}", h1 { style: "background: red;", children: [ "{{ title }}" ] }` } })
結果:
hint 123 <h1>test 123</h1>
功能
index.html
test const app = new QUI({ id: "app", data: { password: null, }, event: { show: function(e){ alert("Password:", app.data.password); } } });
結果
test const app = new QUI({ id: "app", event: { test: function(e){ alert(e.target.innerText + " clicked"); } } });
index.html
test const app = new QUI({ id: "app", data: { width: "100px", color: "red" } });
結果
test
index.html
<p>Total: {{ LENGTH(array) }}</p> const app = new QUI({ id: "app", data: { array: [1, 2, 3, 4] } });
結果
<p>Total: 4</p>
index.html
<p>calc: {{ CALC(num * 10) }}</p> const app = new QUI({ id: "app", data: { num: 1 } });
結果
<p>calc: 10</p>
延遲載入
index.html
<p>{{ UPPER(test1) }} {{ LOWER(test2) }}</p> const app = new QUI({ id: "app", data: { test1: "upper", test2: "LOWER" } });
結果
<p>UPPER lower</p>
測試.svg
<p>{{ DATE(now, YYYY-MM-DD hh:mm:ss) }}</p> const app = new QUI({ id: "app", data: { now: Math.floor(Date.now() / 1000) } });
index.html
<p>2024-08-17 03:40:47</p>
結果
<img> const app = new QUI({ id: "app", data: { image: "test.jpg" }, option: { lazyload: true // Enable image lazy loading: true|false (default: true) } });
i18n
> [!注意]
>如果format是對象,則直接配置多語言內容。
>如果格式是字串,則透過 fetch 動態載入語言檔案。
en.json
<img src="test.jpg">
index.html
結果 i18nLang = zh
const app = new QUI({ id: "app", data: { svg: "test.svg", }, option: { svg: true // Enable SVG file transformation: true|false (default: true) } });
結果 i18nLang = en
生命週期掛鉤
{ "greeting": "Hello", "username": "Username" }
資料擷取
npm i @pardnchiu/quickui
此項目已取得專有許可證許可。
您只能根據最終使用者授權協議 (EULA) 中指定的條款使用、安裝和執行此軟體。
©️ 2024 邱敬幃 Pardn Chiu
以上是QuickUI:輕量級前端框架的詳細內容。更多資訊請關注PHP中文網其他相關文章!