Liquid Ruby模板引擎可以处理Rails表单吗? [英] Can the Liquid Ruby template engine deal with Rails forms?

查看:71
本文介绍了Liquid Ruby模板引擎可以处理Rails表单吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找模板引擎,以允许用户轻松地在线创建课程和练习.

类似 Liquid 在Rails中最受欢迎.Liquid用户可以轻松创建Rails表单吗?

I've been searching for a template engine to allow users to create lessons and exercises online easily.

Seems like Liquid is the most popular for use in Rails. Can Liquid users easily create rails forms?

通常我会使用以下方法在ERB中创建表单:

Normally I create forms in ERB with:

<%= form_for(@lesson) do |f| %>
  <% if @lesson.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@lesson.errors.count, "error") %> prohibited this lesson from being saved:</h2>

      <ul>
      <% @lesson.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
  <div>lots of fields</div>
<% end %>

Rails会自动插入CSRF保护内容.我可以用Liquid做同样的事吗?我可以在Liquid中创建过滤器,标签和/或块来模拟Rails表单标签吗?

Rails will automatically insert the CSRF protection stuff among other things. Can I do the same with Liquid? Can I create filters, tags and/or blocks in Liquid to emulate Rails form tags?

推荐答案

您可以在Liquid上注册您自己的标签块,但是它并不是开箱即用的.

You can register your own tag blocks with Liquid, but it doesn't came out of the box.

如果您查看文档,您会发现您可以创建自己的标签块.

If you check the documentation, you will notice that you can create your own tag blocks.

您可以注册自己的标记块

You can register your own tag block

class LiquidForm < Liquid::Block
  def initialize(tag_name, markup, tokens)
     super
  end

  def render(context)
    form_tag("/hello_word") do 
      input_tag "hello"
    end
  end
end

Liquid::Template.register_tag('liquid_form', LiquidForm)

然后用液体解析所需的文本

And then parse the text you want with liquid

text = " {% liquid_form %} Form content {% endliquid_form %} "
@template = Liquid::Template.parse(text)

这篇关于Liquid Ruby模板引擎可以处理Rails表单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆