如何将一系列键替换为使用 javascript 显示的字符串?
How to substitute a series of keys into a string being displayed using javascript?
我有一组模板,其中包含由 %%key%% 表示的关键短语(如果这些有问题,可以使用不同的分隔符)。在所提供的代码中,模板的名称显示在选择器中,当被选中时,它们的值当前被移动到文本区域中以供显示。在显示它们之前,我希望浏览模板并将每个键替换为与该键关联的值。
我试过用template.gsub'key''value',template.gsub! 'key''value',甚至模板['key']='value',都无济于事。为了消除其他问题,我尝试对 '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
成功了吗?
在: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]);
我有一组模板,其中包含由 %%key%% 表示的关键短语(如果这些有问题,可以使用不同的分隔符)。在所提供的代码中,模板的名称显示在选择器中,当被选中时,它们的值当前被移动到文本区域中以供显示。在显示它们之前,我希望浏览模板并将每个键替换为与该键关联的值。
我试过用template.gsub'key''value',template.gsub! 'key''value',甚至模板['key']='value',都无济于事。为了消除其他问题,我尝试对 '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
成功了吗?
在: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]);