【问题标题】:How to add variables in a form textarea如何在表单文本区域中添加变量
【发布时间】:2021-11-27 06:08:08
【问题描述】:

我想创建可由管理员轻松更新的自定义确认消息。

喜欢

 <textarea>You can contact {contact_name} located at {contact_place}…</textarea>

contact_name 和 contact_place 是需要填写在同一个表格中的字段,并且都是必填字段。

容易管理吗?

【问题讨论】:

  • 不要认为您可以使用 PHP 实现这一目标。如果理解正确,您希望在表格中填写相应字段后即时填写contact_name 和contact_place。对吗?
  • 不完全是。例如,在我的 view() 中,我想识别 {contact_name} 和 {contact_place} 并用适当的字段动态替换它们,例如 event->name 和 event->place。
  • 如果在生成表单时变量中有数据,您可以将其粘贴到 HTML 中,如下所示:&lt;textarea&gt;You can contact &lt;?= $event-&gt;name?&gt; located at &lt;?= $event-&gt;place ?&gt;&lt;/textarea&gt;。还是使用&lt;?= $this-&gt;Form-&gt;control('textarea_field_name') ?&gt; 生成表单?

标签: php cakephp cakephp-4.x


【解决方案1】:

将带有占位符的文本字符串保存在 db 中,然后在 custom Helper 或实体 virtual fields

中使用 str_replace
   return str_replace(
      [
         '{{contact_name}}', // placeholders
         '{{contact_place}}', 
         '{{email}}',
      ],
      [
         $contact->name, // inputs
         $contact->place,
         $contact->email,
      ],
       // load template string from db
      // You can contact {{contact_name}} located at {{contact_place}}…
      $admin->template
   );

   

【讨论】:

    猜你喜欢
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 2011-12-07
    • 2012-03-16
    • 2012-12-20
    • 1970-01-01
    相关资源
    最近更新 更多