Write a BSX view
Let’s make a small badge we can use anywhere in the Journal. We’ll give it a label and use it in another view just as we would call a Bend function.
Where the markup goes
Create your view in a .bsx file. You can write HTML tags in the body of a Bend function here. When you build the app, Bwf translates the tags into Bend expressions and checks the whole function.
Give the view a signature
Take a look at the call to badge(label) in the second function below. It goes right inside the heading. The first function takes a string and returns Html.Html, so Bend can check that call. In your BSX file, import the HTML module as Html and the HTTP text module as Text.
def badge(label: String) -> Html.Html:
<span class="tag">{label}</span>
def heading(label: String) -> Html.Html:
<header><h1>Items</h1>{badge(label)}</header>Insert text or a child element
What happens if the label itself contains a tag? Try a string containing angle brackets. BSX escapes strings in both text and attributes. An Html.Html value already represents markup and goes into the tree as a child. You can spell this explicitly as {@expression}.
Pass <Bend> to the heading. The browser shows the brackets. Validate any URL scheme you accept and sanitize raw markup before inserting it.
Use the label below to see how the brackets look in the rendered HTML.
def title(label: String) -> Html.Html:
<h1>{label}</h1>Return siblings without a wrapper
Use <>…</> when the result contains several siblings. The layout owns the document head and its assets. A response for one region can call the same view as a full page.
Put the component below inside a ul. Its list items will be direct children.
def list_pair(first: String, second: String) -> Html.Html:
<><li>{first}</li><li>{second}</li></>Check a view on its own
Run scripts/bwf check --entry path/to/View.bsx from the project root. Then open the page and inspect the resulting elements. The build catches argument types. The browser shows the actual layout and controls. See HTML forms and CSRF for form checks.
Replace the path in this command with the view you want to check.
scripts/bwf check --entry examples/canonical/View/Account.bsx