占位符表单

:discourse2: Summary Placeholder Forms will let you build dynamic documentation, by creating a form that replaces any occurrence of a =PATTERN= in your post with the value from a text or selectable input field.
:eyeglasses: Preview Preview on Discourse Theme Creator
:hammer_and_wrench: Repository Link https://github.com/discourse/discourse-placeholder-theme-component
:open_book: New to Discourse Themes? Beginner’s guide to using Discourse Themes

Install this theme component

Features

Add placeholders to posts in this format:

[wrap=placeholder key=NAME description="Your name"][/wrap]
[wrap=placeholder key=COUNTRY default=US defaults=FR,DE,US,CN,AU,CA][/wrap]

Your email: =NAME=-=COUNTRY=@example.com

And this will result in:

This is used in mailing list

Your email: =ZNAME=-=ZCOUNTRY=@example.com

As seen above, text inside the wrapper will be used as a long description.

[wrap=placeholder key=NAME description="Your name"]
This is used in mailing list
[/wrap]

Available keys

  • key: The key that should be replaced in your post
  • default/defaults: default value(s)
  • description: a placeholder text for the input

Examples

# create a new plugin
rails g plugin =PLUGIN_NAME=
cd plugins/=PLUGIN_NAME=

We use it to create runbooks, howtos and reusable templates (it will work in code blocks). Please share your use cases!

Settings

Translation Default
toolbar.builder Add Placeholder
builder.errors.no_key A key is required.
builder.title Add Placeholder
builder.insert Insert
builder.key.label Key
builder.key.description The =Key= to be replaced in the post.
builder.description.label Description
builder.description.description Description displayed on input with no value set.
builder.values.label Default value(s)
builder.values.description Optional value(s) for your placeholder, if multiple values are defined, a select will be used.

:discourse2: Hosted by us? Theme components are available to use on our Standard, Business, and Enterprise plans.

Last edited by @JammyDodger 2024-06-13T22:28:45Z

Check documentPerform check on document:
65 个赞

The big issue with math handling like this, is that you need to eval() and eval is unsafe, so you need to use some lib which provide a safe eval with a limited subset of the real eval. I’m unsure we want all this complexity for such a rare case.

6 个赞

I tried ! and that would be a neat composer-preview-redux-in-OP kind of thing but… :pensive: I must be missing something, if I understand correctly the Reply Template component takes the raw and looks for “replacers”, but the Placeholder Forms component decorates the cooked?

What I’m trying to do is ease my users into adding some quizz Q&As in a topic, with minimal technical friction, clicks or even key strokes, :heavy-sigh: :roll_eyes:

[wrap=placeholder key="question" description="question"][/wrap]
[wrap=placeholder key="réponse" description="réponse"][/wrap]

[wrap=template key="template-spoiler" action="reply" tagsList="#qui, #quand, #où, #comment"]

## Question
=question=
### Réponse
[spoiler]
=réponse=
[/spoiler]
---
*Étiquette(s) ?*
[/wrap]

which gives

so far so good, but unfortunately, the placeholders =question= and =réponse= are kept in the composer when hitting the template button

Yes I’m not sure what I was thinking when I wrote this… I probably had a different use case in mind, but can’t remember.

It can’t work this way for sure. I will think a little bit about this… Both theme components were experiments at the time and might deserve better treatment as people have been using it quite frequently.

7 个赞

Thank you !

happy lets go GIF by Shalita Grant

3 个赞

:grin: I don’t know what’s cookin’ in the chef kitchen, but as I usually stuff myself with pistachios or whatnot while waiting, I managed to make it work (for my specific use of course as I’ve almost no idea of what I’m doing, but hey… :sweat_smile: )

4 个赞

Ideally I would like to be able to let admin define their own replacers, but eval is risky (and not even possible with a secure csp) so I’m not sure how I could achieve this…

3 个赞

First of all - great and really helpful theme component.

But I think I found a bug. If I use a placeholder in a code snippet, that contains backticks (`), it is not working. An example is a SQL code snippet:

DELETE FROM `=TABLENAME=`
WHERE id NOT IN(
        SELECT
            id FROM (
                SELECT
                    id FROM `=TABLENAME=`
                ORDER BY
                    id DESC
                LIMIT 20
) subquery);

TABLENAME gets never replaced (if there is a TABLENAME placeholder).

如果这也包含一些类似 Jinja2 的条件就好了。这样,例如,有人可以创建一个名为 red 的键,并将其映射到图标 :red_circle:orange 将映射到 :orange_circle:。当然,没有这些我也可以,但拥有这些测试条件会很棒。

我真的很喜欢这个组件。 :star_struck:


“你真的靠着=NOUN_1=生活吗?真是个=ADJECTIVE_1=的生活!”

“靠着它,和它一起,在它上面,在它里面,”=PERSON_1=说。“它是我的兄弟姐妹,是我的阿姨,是我的=NOUN_2=,是我的食物和饮料,是(=ADJECTIVE_2=)=VERBING_1=。它是我的=NOUN_3=,我不需要别的。它没有的东西不值得=VERBING_2=,它不知道的东西不值得=VERBING_3=。天哪!我们一起经历了多少=NOUNS_1=!无论是冬天还是夏天,春天还是秋天,它总是有它的=NOUN_4=和它的=NOUNS_2=。当二月=NOUNS_3=的时候,我的=NOUNS_4=和地下室里堆满了对我没用的=NOUN_5=,=COLOR_1=的=NOUN_6=从我最好的=NOUN_7=窗户边流过;或者当一切=ADVERB_1=地落下,露出像=FOOD_1=一样气味的=NOUN_8=斑块,=NOUNS_5=和=NOUNS_6=堵塞了水道,我可以=VERB_1=着=MOISTURE=的=NOUN_9=在大部分=NOUN_10=上,找到新鲜的=FOOD_2=吃,还有那些=ADJECTIVE_3=的人从=NOUNS_7=里掉出来的东西!”


原文来自 Standard Ebooks 版本《柳林风声》在线阅读(搜索 By it and with it)。

10 个赞

你好 :wave:

感谢这个组件 :heart: 它完美地契合了我现在想要实现的目标。实际上,我最初的想法是,如果能与 回复模板 组件结合使用就太棒了。但遗憾的是,正如上面的帖子所解释的,这是不可能的。:confused:


我找到了一个变通方法,可以用作模板,它与 链接到新主题链接到新个人消息 一起使用。

这是我的做法:

  1. 创建一个占位符表单(为简单起见,我使用了 OP 中的示例)

Your email: =NAME=-=COUNTRY=@example.com


  1. 创建一个新主题的链接:
    我在链接中设置了:
  2. 类别
  3. 标题
  4. 正文

如果你想添加空行,请使用 \,这样它就不会破坏链接,或者为了让它不可见,你可以使用  ,它会在新行中添加一个空格。它将成为模板的一部分,并创建空行,

<a href="https://meta.discourse.org/new-topic?category==CATEGORY=&title=This is a topic created by =NAME=&body=Hello :wave:
&nbsp;
This is a test topic... Let's see the placeholder form.
\
Your email: =NAME=-=COUNTRY=@example.com">Use template as new topic</a>

  1. 创建一个主题预览

Selected category

=CATEGORY=

Topic title

This is a topic created by =NAME=

Topic body

Hello :wave:

This is a test topic… Let’s see the placeholder form.

Your email: =NAME=-=COUNTRY=@example.com


  1. 创建“用作新主题的模板”按钮(在上面的示例中,这是一个链接,你可以将其制作为按钮)

用这个文本创建一个 [wrap] 元素。这使得它可以通过 CSS 进行定位和样式设置。我将其添加到下面。

<a href="https://meta.discourse.org/new-topic?category==CATEGORY=&title=This is a topic created by =NAME=&body=Hello :wave:
&nbsp;
This is a test topic... Let's see the placeholder form.
\
Your email: =NAME=-=COUNTRY=@example.com">[wrap=template-button]Use template as new topic[/wrap]</a>

样式化 [wrap=template-button]
类似这样 :arrow_down_small:

Common / CSS

[data-wrap="template-button"] {
  background: var(--tertiary);
  color: var(--secondary);
  border-radius: var(--d-button-border-radius);
  padding: 0.5em 0.65em;
  transition: background 0.25s;
  &:focus,
  &:hover {
    background: var(--tertiary-hover);
    color: var(--secondary);
  }
}

模板按钮看起来会像这样 :arrow_down_small:

template-button


演示

Use template as new topic


希望这个小教程能帮助到需要类似功能的人 :slightly_smiling_face:

编辑:我设置了一个类别选择器,它增加了在不同类别中创建主题的功能。


查看此帖子的原始版本…

8 个赞

太棒了,Don,谢谢你。这实际上非常符合我的一个需求 :slight_smile:

2 个赞

需要注意的是,代码块有时会引起问题:

usermod -aG sudo =USERNAME=

在这种情况下,highlight.js 会将其假定为 Java 并将代码块转换为:

<code class="hljs language-java" data-highlighted="yes">
  usermod -
  <span class="hljs-type">aG</span>
  <span class="hljs-variable">sudo</span>
  <span class="hljs-operator">=</span>
  USERNAME=
</code>

我可以通过将其标记为实际上是 shell 命令来修复它:

    ```shell
    usermod -aG sudo =USERNAME=
    ```

结果是:

usermod -aG sudo =USERNAME=

如果所有其他方法都失败了,text 可以阻止 highlight.js 干扰占位符。

一个更通用的解决方案(Meta Discourse 似乎正在使用)是将 default code lang 设置从 auto 更改为 plaintext 或其他类似选项。 :wink:


虽然这关系不大,但使用 java 是避免在你想写关于占位符时占位符展开的一种方法。

1 个赞

预览链接好像坏了,你能检查一下吗?

1 个赞

这是一个很棒的功能,我可以看到它的许多用途。
但是,在我们的回复中,是否有可能在用户开始在文本框中键入之前隐藏=(name)=?我认为用户(尤其是新用户)可能不理解它的用途,或者它看起来格式不正确。

我想我有一个又能用了 (:crossed_fingers: :slight_smile:)

https://discourse.theme-creator.io/theme/Discourse/placeholder-forms

我也把它添加到了 try.discourse.org,这样你就可以更深入地体验它了。:+1:

1 个赞

也许您可以输入一个将显示为默认值的条目。

1 个赞

那奏效了!谢谢!

2 个赞

此变通办法似乎不再有效。

是否有其他选项可以将 reply-templateplaceholder-forms 结合起来?

4 个帖子已拆分为新主题:链接在占位符表单中不起作用