首页 > 解决方案 > 如何将一系列键替换为使用 javascript 显示的字符串?

问题描述

我有一组模板,其中包含由 %%key%% 表示的关键短语(如果有问题,可以使用不同的分隔符)。In the code presented, the names of the templates are shown in a selector, and when selected, their values are presently being moved into a textarea for display. 在它们显示之前,我希望浏览模板并将每个键替换为与该键关联的值。

我试过使用 template.gsub 'key' 'value',template.gsub!'key' 'value',甚至template['key'] = 'value',都无济于事。为了消除其他问题,我尝试对“值”使用简单的值,然后在警报中显示结果。如果我不尝试替换,警报会显示模板。如果我尝试这些尝试中的任何一种,我想我不会显示警报,表明某种 javascript 错误。我无法弄清楚错误是什么。

这是 application_helper.rb 的一部分:

  #----------------------------------------------------------------------------
  def render_haml(haml, locals = {})
    Haml::Engine.new(haml.strip_heredoc, format: :html5).render(self, locals)
  end

  #----------------------------------------------------------------------------
  def create_template_selector
    get_templates()
    render_haml <<-HAML
    %select{{name: "msg", id: "template_selector"}}
      - @t_hash.each do |name,message|
        %option{ :value => message }= name
    HAML
  end

  #----------------------------------------------------------------------------
  def get_templates()
    templates = Template.all
    @t_hash = Hash.new
    templates.each do |t|
      @t_hash[t.name] = t.message
    end
  end

这是视图部分 _text.html.haml,其中嵌入了选择器,并在更改时显示其选择:

    = form_for(@comment, remote: true, html: {id: "#{id_prefix}_new_comment"}) do |f|
      = hidden_field_tag "comment[commentable_id]", commentable.id, id: "#{id_prefix}_comment_commentable_id"
      = hidden_field_tag "comment[commentable_type]", class_name.classify, id: "#{id_prefix}_comment_commentable_type"
      %div
        %h3 Select a Template
        = create_template_selector
        %div
          = f.text_area :comment, id: "#{id_prefix}_comment_comment", name:"text_msg"
          .buttons
          = image_tag("loading.gif", size: :thumb, class: "spinner", style: "display: none;")
          = f.submit t(:add_note), id: "#{id_prefix}_comment_submit"
          #{t :or}
          = link_to(t(:cancel), '#', class: 'cancel')
    :javascript
      $(document).ready( function() {
      $('#template_selector').change(function() {
        var data= $('select').find('option:selected').val();
        //
        // Here is where I put alert(data) and it works
        // but,
        // var filled = data.gsub '%%first_name%%' 'Ralph'
        // followed by alert(filled), shows no alert panel and no error
        //
        $("##{id_prefix}_comment_comment").val(data);
        });
      }  );
 How can I create a set of fill_ins like:
     def fill_ins()
        @fillins = Hash.new
        @fillins['%%first_name%%'] = 'Ralph'
        @fillins['%%last_name%%'] = 'Jones'
     ...
     end

并创建一个函数,如:

     def fill_in(template)
        @fi = fill_ins()
        @fi.each do 'fkey, fval'
           template.gsub! fkey fval
        end
      end

并让它工作?

标签: javascriptrubyhaml

解决方案


:javascript块中,您应该编写 JavaScript,而不是 Ruby。

String#gsub是一种 Ruby 方法。

String.prototype.replace是一种 JavaScript 方法。

var filled = data.replace(/%%first_name%%/g, 'Ralph');

编辑:忘记了,replace字符串只替换一次。改为使用带有全局标志的正则表达式。

另外:要将数据从 Ruby 传递到 JavaScript,请在​​模板中使用此模式:

<script>
  const fillIns = <%= fill_ins.to_json %>;
</script>

然后,您可以循环该数组并replace使用每一对运行该方法(不是最佳的) - 或者您可以使用一个正则表达式来获取变量的一般模式:

var filled = data.replace(/%%([^%]+)%%/g, (_, name) => fillIns[name]);

推荐阅读