Show HN:Typeform 替代方案,将 Markdown 转换为表单 Show HN: Typeform alternative, turns Markdown to forms

原始链接: https://blocks.md

使用配对 ::: 将内容包裹在

标记之间。 任何有效的 Markdown 内容(如标题、段落、列表、表单等)都是可接受的。 可以通过[...]添加其他类和属性(上面进一步解释)。 此外,通过将多个字段的名称括在 [...] 内的大括号 ($...$) 中,将多个字段分配给
元素,从而在绑定字段值更改时启用自动更新。 利用 Nunjucks 进行模板化可以支持 if-else 语句、循环、过滤器等结构。 此外,可以使用 {$ field $} 将单个字段分配给 标记。 虽然通用性较差,但这简化了段落、标题、列表项等区域的使用。 以下是使用选择框、数字输入和 span 元素的示例: ``降价 [.col-4] 已选择:{$产品$} ::: [.col-8 .text-end .xs:text-start {$价格数量$}] {% 如果价格和数量 -%} 合计:${{ 价格 }} × {{ 数量 }} = ${{ 价格 * 数量 }} {% 别的 -%} 总计:设定价格和数量 {% 结束 %} ::: ````` 在此示例中,我们创建两个输入字段“产品”和“价格”。 通过将它们绑定到特定部分,更新这些字段将导致相关部分自动刷新。 例如,更改产品选择将更新“所选”部分,而调整“价格”或“数量”输入将重新计算“总计”部分下显示的总成本。 该示例使用以下数据结构: ```javascript SelectBox({问题: '产品', 选项: ['T 恤', '袜子'], 选择: 'T 恤'}, NumberInput({问题:'价格',unitEnd:'$',子字段:true}, NumberInput({问题: '数量', 子字​​段: true}) ) ````` 此外,单个字段可以绑定到 元素,这在整个文档的各种上下文中都很有用。

Wrap your content between

tags using pairing :::. Any valid Markdown content like headers, paragraphs, lists, forms, etc., is acceptable. Additional classes and attributes may be added through [...](further explained above). Furthermore, assign multiple fields to a
element by including their names enclosed in curly braces ($...$) within the [...], enabling automatic updates when bound field values change. Utilizing Nunjucks for templating allows support for constructs like if-else statements, loops, filters, among others. In addition, an individual field can be assigned to a tag with the use of {$ field $}. While less versatile, this simplifies usage in areas like paragraphs, headings, list items, etc. Here's an example using select boxes, number inputs, and span elements: ```markdown [.col-4] Selected: {$ product $} ::: [.col-8 .text-end .xs:text-start {$ price quantity $}] {% if price and quantity -%} Total: ${{ price }} × {{ quantity }} = ${{ price * quantity }} {% else -%} Total: Set price and quantity {% endif %} ::: ``` In this example, we create two input fields "product" and "price". By binding them to specific sections, updating these fields will cause the relevant parts to refresh automatically. For instance, changing the product selection will update the "selected" section, while adjusting either the "price" or "quantity" input will recalculate the total cost displayed under the "total" section. This example uses the following data structure: ```javascript SelectBox({question: 'Product', options: ['T-Shirts', 'Socks'], selected: 'T-Shirts'}, NumberInput({question: 'Price', unitEnd: '$', subfield: true}, NumberInput({question: 'Quantity', subfield: true}) ) ``` Furthermore, a single field can be bound to a element, which can be useful in various contexts throughout the document.


Create wrapping <div> elements by putting content inside pairs of :::. The content inside can be any valid Markdown, such as headings, paragraphs, lists, form fields, etc. Class names and other attributes are supported via [...] (see above). Moreover, you can bind one or more fields to a <div> element by adding the names of the fields inside {$...$} (separated by spaces), and placing this within the [...]. This means that whenever the value of a binded field changes, the content inside the <div> will be automatically re-rendered.

The templating is done using Nunjucks, so its entire list of features such as if-else statements, loops, filters, etc. are fully supported. You can also bind a single field to a <span> element using {$ field $}. This is obviously not as flexible, but it is simple, and can be used inside paragraphs, headings, list items, etc.

Further reading: Data-binding - Docs

Markdown

product* = SelectBox(
	| question = Product | options = T-Shirts, Socks 
	| selected = T-Shirts | subfield
)

price* = NumberInput(
	| question = Price | unitEnd = $ | subfield | min = 1
)

quantity* = NumberInput(
	| question = Quantity | subfield | min = 1
)

[.col-4]
Selected: {$ product $}

::: [.col-8 .text-end .xs:text-start {$ price quantity $}]
{% if price and quantity -%}
	Total: ${{ price }} × {{ quantity }} = ${{ price * quantity }}
{% else -%}
	Total: Set price and quantity
{% endif %}
:::
相关文章
联系我们 contact @ memedata.com