Lesson 24 of 56 · Layout language

Layout language: common for web and native

Salam's layout DSL is a declarative language for building interfaces: the same structure can produce web output (HTML/CSS/JS) or run as a native UI app drawn by Salam. This lesson covers what is common; web-specific details are in Layout on the web and native desktop details in Layout in native apps.

What is shared

In both targets you describe the interface as a tree of elements. A page: block (or صفحه:) is the root, each element opens with its name and a colon and closes with end (پایان), and attributes/styles are written as name = value. Element text is usually its content, elements can nest, and the structure stays readable.

Top-level page settings such as title, lang, and dir belong to the page. English files default to left-to-right; Persian files default to right-to-left. You can set them explicitly when needed.

There is one official spelling for every name in the layout language, and in an English file it has spaces: font size, word break, border radius, hover background. Writing it with an underscore or a hyphen, as in font_size, is the error E235, and the message tells you the spelling to write. The rule covers spellings that write an official spaced name with _ or - instead of the spaces (a spelling with no separator, such as fontsize, is not part of it), for attributes, elements and their state forms, in page and component blocks, in named pages, and in every branch of a style if. Names that officially carry a hyphen, such as data-* and aria-*, keep it. Names you make yourself, such as component names and parameters, are ordinary names and keep their underscores. Persian files keep their own spelling.

layout-page.salam
page:
    title = "My app"
    lang = "english"
    dir = "left to right"
    header:
        background = "#2563eb"
        color = "white"
        padding = 24px
        heading:
            size = 1
            content = "Welcome to Salam"
        end
    end
    box:
        padding = 20px
        paragraph:
            content = "This page was built from Salam code."
        end
    end
end
Result

Simpler value syntax

In layout/style values you can now write many values in a shorter form: percentages like 50% and 100% without quotes, hex colors like "#0f766e" and words like "solid" in quotes, units such as 12px/1rem/200ms in value contexts, and value lists/groups where supported. For example:

salam
box:
    width = 50%
    max width = 480
    padding = 16, 24
    background = "#f8fafc"
    color = "#0f766e"
    border = 1, "solid", "#0f766e"
end

Bare numbers are typically treated as pixels for length-like properties. Strings (such as text content, URLs, or specific textual values) still use quotes.

Elements, attributes, and styles

Element and property names are readable (English and Persian aliases). Common elements include box/div, paragraph, heading, link, image, list/item, form, input, and button. Boolean attributes like required = true are meaningful, while false is usually omitted. CSS properties are written alongside elements as attributes.

layout-form.salam
page:
    title = "Contact us"
    nav:
        link:
            url = "/"
            content = "Home"
        end
        link:
            url = "/blog"
            content = "Blog"
        end
        link:
            url = "https://github.com/SalamLang/Salam"
            target = "new"
            content = "Source"
        end
    end
    image:
        url = "/logo.png"
        alt = "Salam logo"
    end
    form:
        id = "contact"
        label:
            for = "email"
            content = "Your email"
        end
        input:
            id = "email"
            type = "email"
            name = "email"
            placeholder = "you@example.com"
            required = true
        end
        label:
            for = "message"
            content = "Message"
        end
        input:
            id = "message"
            type = "text"
            name = "message"
        end
        button:
            type = "submit"
            content = "Send"
        end
    end
    list:
        item:
            content = "We answer within a day"
        end
        item:
            content = "No account needed"
        end
    end
end
Result

Style properties are plain attributes, and the standard vocabulary is the same on both targets:

layout-style.salam
page:
    title = "Pricing"
    section:
        layout = "row"
        gap = 16
        padding = 32
        box:
            border = 1, "solid", "#dddddd"
            radius = 12
            padding = 16
            heading:
                size = 2
                content = "Free"
            end
            paragraph:
                content = "For trying things out"
            end
        end
        box:
            border = 2, "solid", "#0f766e"
            radius = 12
            padding = 16
            heading:
                size = 2
                content = "Pro"
                color = "#0f766e"
            end
            button:
                content = "Upgrade"
                background = "#0f766e"
                color = "white"
                padding = 8, 16
                radius = 6
                hover background = "#115e59"
                cursor = "pointer"
            end
        end
    end
end
Result

Components and reuse

A component (بخش) is a reusable piece of UI with parameters, used like a custom element. content: end marks the slot where nested children are inserted. To share components across files, include them (e.g. include = "parts.salam", path relative to the including file). A component is private to the file that defines it; write pub component (همگانی بخش) to let other files use it. Using a private component from another file is error E232. The English name of a pub component must be written in PascalCase (MyCard, not my_card); anything else is error E290, and Persian names are exempt. A component with no parameters is written without parentheses, component Footer:. Empty parentheses, component Footer():, are error E270.

layout-component.salam
component Plan(name, price):
    box:
        border = 1px, "solid", "#ddd"
        radius = 12px
        padding = 16px
        heading:
            size = 2
            content = name
        end
        paragraph:
            content = "$" + price + " a month"
        end
        content: end
    end
end

page:
    title = "Plans"
    Plan:
        name = "Basic"
        price = 4
    end
    Plan:
        name = "Pro"
        price = 9
        list:
            item:
                content = "Private projects"
            end
            item:
                content = "Email support"
            end
        end
    end
end
Result

The include = "parts.salam" line goes inside the page: block or a component body. At the top level of a file include is written without = and loads code, not components.

Parameterized pages and dynamic structure

A layout/page can take parameters (e.g. layout Card(name: str, score: int):) and be rendered with runtime values. Inside parameterized layouts you can use conditions (if/else) and loops (each) to build dynamic UI. Parameter text is handled safely when inserted into the page.

Every row that an each renders needs a key: key = i or key = item.id (کلید), on the row element or on the component use, in every if/else branch, and in a nested loop for its own variable. A constant, id or data-key does not count; a missing key is error E292, and salam check --fix adds one.

layout-runtime.salam
page Card(name: str, score: int):
    title = "Card " + name
    heading:
        size = 2
        content = name
    end
    if score >= 50:
        paragraph:
            content = "pass"
        end
    else:
        paragraph:
            content = "fail"
        end
    end
end

func from(html: str, mark: str): str:
    start := html.Find(mark)
    ret html.Substr(start, html.Len() - start)
end

func main:
    println from(Card("Ali", 70), "<body>")
end
Output
<body><h2>Ali</h2><p>pass</p></body></html>
layout-inbox.salam
component Badge(text: str):
    span:
        content = text
        background = "#fde68a"
    end
end

page Inbox(user: str, subjects: Vector<str>):
    title = "Inbox of " + user
    heading:
        size = 1
        content = "Hello, " + user
    end
    if subjects.IsEmpty():
        paragraph:
            content = "No new messages"
        end
    else:
        paragraph:
            content = "Unread: " + subjects.Len()
        end
        list:
            each i, s in subjects:
                item:
                    key = i
                    content = (i + 1) + ". " + s
                end
            end
        end
    end
end

func from(html: str, mark: str): str:
    start := html.Find(mark)
    ret html.Substr(start, html.Len() - start)
end

func main:
    mut subjects := Vector {} as Vector<str>
    defer subjects.Free()
    subjects.Push("Meeting at 10")
    subjects.Push("<b>FREE</b> prize & more")
    println from(Inbox("Sara", subjects), "<body>")
    println from(Badge("new"), "<span")
end
Output
<body><h1>Hello, Sara</h1><p>Unread: 2</p><ul><li data-key="0">1. Meeting at 10</li><li data-key="1">2. &lt;b&gt;FREE&lt;/b&gt; prize &amp; more</li></ul></body></html>
<span class="badge-a">new</span>

Two targets: web and native

The same DSL can target different runtimes, with different outputs and capabilities:

•

Web: build with salam build --platform=web to produce HTML/CSS/JS for browsers, with DOM/events and web/server integration. See Layout on the web.

•

Native (desktop): run/build with salam run --ui=native or salam build --ui=native to open a native window rendered by Salam's UI engine (no browser). See Layout in native apps.

For desktop apps that host web UI inside a native window (WebView, like Tauri/Electron model), see gui.webview; that is a different path from native DSL rendering.

Compile-time checks

The compiler checks element/attribute names and many values and reports helpful errors. This catches mistakes early for both web and native targets. For parameterized pages and untrusted input, follow the safety/validation rules of the chosen runtime (especially web/server paths).

Continue with Layout on the web for build/serve/browser details, or Layout in native apps for --ui=native and desktop behavior. Later lessons cover elements, CSS, events, and complete web projects in depth.