? Hey all, welcome back this week! Each tutorial is self-contained, so feel free to start at the beginning, or just plunge in here.
Note: Due to a bug with Dev.To, I'm unable to publish this tutorial normally. The fenced code blocks do not support template syntax, causing the markdown parser to crash.
In Part 6, we'll be creating a custom, interactive sign-up form that only shows the newsletters to subscribe when the user chooses. This will give us practice with some very useful techniques, while building up our knowledge of template-tags, a core concept in templating.
In this tutorial, we will start with Props, just like with the PictureFrame component in Part 2.
As with previous tutorials, you can simply copy and paste this code into any HTML file to start:
<template Modulo> <Component name="NewsletterSubscribe"> <Template> <form method="POST"> <h2>Learn Modulo.js - Sign-Up Component</h2> <label><strong>Email:</strong><input [state.bind] name="email" /></label> <label> <input [state.bind] name="subscribe" type="checkbox" /> I want to subscribe to receive news and special offers </label> <label> <input [state.bind] name="newsletters.kombucha" type="checkbox" /> Kombucha Enthusiast Newsletter </label> <button>Submit</button> </form> </Template> <State email="" subscribe:=false newsletters:={} newsletters.kombucha:=true newsletters.soda:=false newsletters.wine:=true newsletters.beer:=false ></State> </Component> </template> <script src="https://unpkg.com/mdu.js"></script> <x-NewsletterSubscribe></x-NewsletterSubscribe>
Important things to note: Look at how "State" is set-up. Do you see how newsletters:={} assigns to a JavaScript Object (designated with {}), and how then kombucha, soda, wine, and beer are all assigned with the syntax newsletters.kombucha, etc? This creates an Object that ends up looking like: { "kombucha": true, "soda": false ... }. This is a short-hand in Modulo for creating such objects. Also, note how name="newsletters.kombucha" must specify the full name, including the dot (.), to access that true/false value.
In addition to filters, the Modulo templating language also support powerful "template tags", which allow for more complicated custom behavior. This includes the next two topics in this tutorial: "if" template-tag, which allows for conditional rendering (e.g. "only show the submit button if a form is filled correctly", or "only show the modal if the user has clicked the button"), and the "for" template-tag, which allows for HTML to be repeated for each item of some given data (e.g. "every blog post gets it's own
Unlike template variables or filters, they use {% and %} (instead of {{ and }}) to designate where they are in the Template code. Template tags are in the format of {% tag %}. They allow for more complicated transformations to the HTML code generated. For example, here are a few: {% include other_template %} {% comment %} ... {% endcomment %}
One of the most useful template tags you will use is the if tag, written like this: {% if %}. The if-tag allows for conditional rendering of HTML code based on the condition supplied.
See below for two examples of using the "if" tag:
{% if state.expanded %}
Details: {{ props.details }}
请注意,其中还有另外两个标签。这些与 if 标签相关,并且只会显示在 if 标签之后。第一个是 {% endif %} 标签。这是必需的,因为它显示了您希望 if 标记有条件包含的内容的模数。第二个是 {% else %} 标签。 {% else %} 是可选的,如第一个示例所示:您不需要在 if 标签的所有用途中使用它。
让我们从基于 state.subscribe 的 if 语句开始。逻辑是,如果用户根本不想订阅,我们甚至不应该显示额外的新闻通讯选项。我们可以通过以下方式实现这一点:
{% if state.subscribe %}
<输入 [state.bind] name="newsletters.kombucha" type="checkbox" />
康普茶爱好者通讯
标签>
{% endif %}
现在仅在您单击“订阅”时显示选择哪个新闻通讯的选项。
现在,让我们练习使用更复杂的 if 语句来显示提示,而不是“订阅”表单按钮,直到他们开始输入几个字符。电子邮件(可能)永远不应短于 4 个字符,因此我们将其用作阈值。我们可以使用 lt(小于,又名
{% if state.email|长度 lt 4 %}
提示:首先输入您的电子邮件
让我们从一个简单的 for 循环“测试”开始,然后我们将在下一步中对其进行扩展:
{% for key, value in state.newsletters %}
重要提示:始终将模板标签放在 HTML 值中!不要执行类似 {% if value %}style="color: blue"{% endif %} 的操作,而是执行 style="{% if value %}color: blue{% endif %}" 之类的操作。否则,{% 等可能会被解释为无值属性,您的代码中会充斥着 {%=""。
这将循环遍历每个新闻通讯,如果“value”为 true,则将其呈现为绿色。但是,它不会创建切换输入。我们需要在 name= 属性中使用 {{ key }} 属性。检查对输入的以下调整:
将其放入 for 循环中,无论您有多少新闻通讯,它都会自动生成复选框!
最后,我们将所有内容放在一起,添加 |capfirst 进行格式化,并添加 Style 部分以及一些非常基本的样式,以使表单看起来更好一点,最终我们得到了最终结果。请随意复制并粘贴以下内容来尝试一下:
由于 Dev.To 的错误,我无法包含源代码。不过,您可以在以下要点中看到它:
https://gist.github.com/michaelpb/9bac99ba0985859742961c3d1e1f310a
在本节中,我们练习使用模板标签来制作更加用户友好的表单。在下一个教程中,我们将开始深入研究 Modulo 的一些优势,包括数据类型、StaticData 和 API 集成,因此请务必跟随学习如何编写声明性代码以在一个简单的示例中连接 API。几行!
以上是在 Web 组件中创建交互式表单时学习模板标签(学习 Modulo.js - 第 f 部分的详细内容。更多信息请关注PHP中文网其他相关文章!