javascript 块中的 ruby [slim 模板]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17794403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ruby inside javascript block [slim template]
提问by norman784
There is a way to put ruby conditions inside javascript block? i.e.
有没有办法将 ruby 条件放在 javascript 块中?IE
javascript:
var config = {
common_value_1 : 1,
common_value_2 : 2
};
- if my_value === true # this must be a ruby condition
config.custom_true_value_1 = "1" ;
config.custom_true_value_2 = "#{my_value}" ;
- else
config.custom_false_value_1 = "1" ;
config.custom_false_value_2 = "#{my_value}" ;
Or is there another workaround at this problem? Because the ugly way that I can use its:
或者这个问题还有其他解决方法吗?因为我可以使用它的丑陋方式:
javascript:
var config = {
common_value_1 : 1,
common_value_2 : 2
};
- if my_value === true # this must be a ruby condition
javascript:
config.custom_true_value_1 = "1" ;
config.custom_true_value_2 = "#{my_value}" ;
- else
javascript:
config.custom_false_value_1 = "1" ;
config.custom_false_value_2 = "#{my_value}" ;
But I don't like it because if config has common values between if and else then I would duplicate my code and would be much larger and hard to maintain.
但我不喜欢它,因为如果 config 在 if 和 else 之间有共同的值,那么我会复制我的代码并且会更大且难以维护。
Updated with better examples
更新了更好的例子
采纳答案by aross
You can use a style similar to string interpolation. See example below.
您可以使用类似于字符串插值的样式。请参阅下面的示例。
javascript:
var config = {
custom: "#{my_value ? 'truthy' : 'falsy'}",
current_user: #{raw current_user.to_json}
};
** Update below **
** 下面更新**
If you want more advanced configuration I would recommend to create a class, for example
如果您想要更高级的配置,我建议您创建一个类,例如
class ClientConfig
attr_accessor :foo, :bar
# .. code
def to_json
{ foo: foo, bar: bar }.to_json
end
end
# in view file
javascript:
var config = ClientConfig.new.to_json
Else you also have the opportunity to create a ruby partial I've created an example below who may not be so beautiful but I works.
否则你也有机会创建一个 ruby 部分我在下面创建了一个例子,他可能不那么漂亮,但我工作。
# template_path/_config.html.ruby
def configuration
{ foo: "Hello", bar: "World" }
end
def july_special
{ june_key: "It's June" }
end
def month_name
Date.today.strftime("%B")
end
config = month_name == 'July' ? configuration.merge(july_special) : configuration
content_tag :script, config.to_json.html_safe
# viewfile
= render 'template_path/config'
So my point is that there are multiple ways of doing this and you should try to find the way the one that suits you and your application the most. In my case, I would use my first example (before the update) if I just need one or two values else I would go for the class ClientConfig.
所以我的观点是有多种方法可以做到这一点,您应该尝试找到最适合您和您的应用程序的方法。就我而言,如果我只需要一个或两个值,我将使用我的第一个示例(在更新之前),否则我将使用 class ClientConfig。
回答by fphilipe
回答by Rael Gugelmin Cunha
You have 2 options:
您有 2 个选择:
1. Use a rubysection
1. 使用ruby节
This scenario is better for complex code.
这种场景更适合复杂的代码。
I have a ruby object that I want to make a JSON. So, inside my slim file I'll create a rubysection:
我有一个要制作 JSON 的 ruby 对象。因此,在我的超薄文件中,我将创建一个ruby部分:
ruby:
myObject = @object.to_json.html_safe
Pay attention to html_safe: it's important to not escape double quotes.
注意html_safe:不要转义双引号很重要。
Then you can use myObjectinside javascriptsection:
然后你可以使用myObject内部javascript部分:
javascript:
var data = #{myObject};
2. Use double curly braces
2. 使用双花括号
For simple cases, use the double curly braces inside the javascriptsection, like stated in @fphilipe answer:
对于简单的情况,请在该javascript部分内使用双花括号,如@fphilipe 回答中所述:
javascript:
var data = #{{@object.to_json}};

