javascript - 如何将一系列键替换为使用 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
并让它工作?
解决方案
在: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]);
推荐阅读
- java - For 循环在我的 Query Firestore 上不起作用
- c# - Azure DocumentDB - ReplaceDocumentCollectionAsync
- java - Java:生成 JSON:尝试命名对象和数组时出现异常
- php - 教义选择查询日期sqlite
- c# - 将复选框的 ID 从另一个 Web 表单获取到当前 Web 表单以进行配置
- regex - 实体内部的正则表达式 (IBM-WATSON)
- c - c语言中的“#@”有什么用?
- unity3d - 如何在 ARcore 中统一停止渲染特征点
- spring-boot - Spring Boot 加载多个属性文件作为属性对象列表
- javascript - 我想在 Loopback 中为这个 JSON 数据创建一个模型