如何在javascript定义的haml中运行ruby? [英] how to run ruby in haml in javascript definition?

查看:94
本文介绍了如何在javascript定义的haml中运行ruby?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 如何在haml的javascript中运行ruby代码?
  • 如果我在示例中使用var = #{message},则会得到undefined local variable or method message
  • 当我将- message = 'it works'移到:javascript上方时,一切正常
  • how can I run ruby code inside javascript in haml?
  • if I use var = #{message} in my example I get undefined local variable or method message
  • when I move - message = 'it works' above :javascript everything works fine

我想在:javascript内部运行迭代 .each.有关我在最终javascript代码中所需的信息,请参见最后的代码示例.我需要循环几个红宝石变量(或哈希的哈希的一个哈希?)来得到这个.数据(='basics')几乎没有元素.它可以包含很少元素的孩子等.

I want to run iteration .each inside :javascript. See the last code sample for what I need in final javascript code. Where I need to loop few ruby variables (or one hash of hashes of hashes?) to get this. Data (='basics') can have few elemenets. It can have children with few elements etc.

这样的haml代码

%html
  %head
    :javascript
      $(document).ready(function() {
        - message = 'it works'
            var = message

        });
%body
    - message2 = 'hi'
    = message2
    %div{:id =>"jstree"}

给我这个html代码

<html>
  <head>
    <script type='text/javascript'>
      //<![CDATA[
        $(document).ready(function() {
            - message = 'hi'
            var = message

        });
      //]]>
    </script>
  </head>
  <body>
    hi
    <div id='jstree'></div>
  </body>
</html>

我要使用haml生成的最终javascript代码是javascript变量

The final javascript code I want to produce using haml is the javascript variable

var data = [{
       data: "basics",
       attr: {},
        children: [
         {data: "login", attr: {run: "run"},
           children: [                   
           {data: "login", attr: {}}
          ]
         } ,
         {data: "Academic Year", attr: {run: "run"},
          children: [                   
           {data: "login", attr: {}},
           {data: "Academic Year", attr: {filter: "mini", SOF: "yes"}}
          ]

         }
        ]
      }];

推荐答案

首先,让我们回顾一下您似乎了解的内容:

First, let's review what you seem to know:

  1. Ruby要求您先定义局部变量,然后再使用它们.
  2. 您可以使用- ...在过滤器外部的行上运行Ruby代码.
  3. 您可以使用#{...}标记在过滤器中插入Ruby代码.
  1. Ruby requires you to define local variables before you use them.
  2. You can run Ruby code on lines outside of a filter using - ....
  3. You use #{...} markup to interpolate Ruby code inside a filter.

您说要运行each,但是想得到它的输出;由于#{...}的结果被转换为字符串并放入您的代码中,因此您真正想要的(可能)是map:

You say you want to run each, but presumably you want output from this; since the result of #{...} is turned into a string and put in your code, what you really want (probably) is map:

%html
  %head
    :javascript
      var foo = [];
      #{
        limit = rand(4)+3
        array = (0..limit).to_a
        array.map{ |i| "foo[#{i}] = #{rand(12)};" }.join ' '
      }
      console.log(foo.length);
    %body

运行上面的代码将给出以下输出:

Running the above code gives this output:

<html>
  <head>
    <script type='text/javascript'>
      //<![CDATA[
        var foo = [];
        foo[0] = 2; foo[1] = 0; foo[2] = 11; foo[3] = 8; foo[4] = 0; foo[5] = 1;
      //]]>
    </script>
    <body></body>
  </head>
</html>

如您所见,大的#{...}块(可能跨越多行)运行任意的Ruby代码.最后一个表达式的结果(在本例中为map{...}.join)将转换为字符串并放置在输出中.

As you can see, the big #{...} block (which may span multiple lines) runs arbitrary Ruby code. The result of the last expression (in this case the map{...}.join) is converted to a string and placed in the output.

这篇关于如何在javascript定义的haml中运行ruby?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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