HTML Parser для MetaFor - v2.3.2
    Preparing search index...

    Interface NodeCondition

    Узел условного оператора. Представляет тернарный оператор с ветками true и false.

    <div>
    ${context.isLoggedIn ? html`<span>Добро пожаловать, ${context.name}!</span>` : html`<a href="/login">Войти</a>`}
    </div>

    Результат:

    {
    "tag": "div",
    "type": "el",
    "child": [
    {
    "type": "cond",
    "data": "/context/isLoggedIn",
    "child": [
    {
    "tag": "span",
    "type": "el",
    "child": [
    {
    "type": "text",
    "data": "/context/name",
    "expr": "Добро пожаловать, ${[0]}!"
    }
    ]
    },
    {
    "tag": "a",
    "type": "el",
    "string": {
    "href": "/login"
    },
    "child": [
    {
    "type": "text",
    "value": "Войти"
    }
    ]
    }
    ]
    }
    ]
    }
    <div>
    ${core.role === 'admin' && core.permissions.includes('write') ?
    html`<button>Редактировать</button>` :
    html`<span>Нет прав</span>`
    }
    </div>

    Результат:

    {
    "tag": "div",
    "type": "el",
    "child": [
    {
    "type": "cond",
    "data": ["user.role", "user.permissions"],
    "expr": "${[0]} === 'admin' && ${[1]}.includes('write')",
    "child": [
    {
    "tag": "button",
    "type": "el",
    "child": [
    {
    "type": "text",
    "value": "Редактировать"
    }
    ]
    },
    {
    "tag": "span",
    "type": "el",
    "child": [
    {
    "type": "text",
    "value": "Нет прав"
    }
    ]
    }
    ]
    }
    ]
    }
    <div>
    ${core.posts.length > 0 ?
    html`<ul>${core.posts.map(post => html`<li>${post.title}</li>`)}</ul>` :
    html`<p>Постов пока нет</p>`
    }
    </div>

    Результат:

    {
    "tag": "div",
    "type": "el",
    "child": [
    {
    "type": "cond",
    "data": "/core/posts.length",
    "expr": "${[0]} > 0",
    "child": [
    {
    "tag": "ul",
    "type": "el",
    "child": [
    {
    "type": "map",
    "data": "/core/posts",
    "child": [
    {
    "tag": "li",
    "type": "el",
    "child": [
    {
    "type": "text",
    "data": "[item]/title"
    }
    ]
    }
    ]
    }
    ]
    },
    {
    "tag": "p",
    "type": "el",
    "child": [
    {
    "type": "text",
    "value": "Постов пока нет"
    }
    ]
    }
    ]
    }
    ]
    }

    Структура узла:

    • type - всегда "cond" для условных операторов
    • data - путь(и) к данным для условия
    • expr - выражение с индексами (если условие сложное)
    • child - массив из двух элементов: [true-ветка, false-ветка]
    interface NodeCondition {
        type: "cond";
        data: string | string[];
        expr?: string;
        child: Node[];
    }
    Index

    Properties

    Properties

    type: "cond"

    Тип узла - всегда "cond" для условных операторов

    data: string | string[]

    Путь(и) к данным для условия

    data: "/context/isLoggedIn"
    

    data: ["/context/isAdmin", "/core/role"]
    
    expr?: string

    Выражение с индексами (если условие сложное)

    expr: "${[0]} === 'admin' && ${[1]}.length > 0"
    
    child: Node[]

    Узлы для случая когда условие истинно и ложно

    • true: первый элемент массива (child[0])
    • false: второй элемент массива (child[1])