【问题标题】:converting html to haml将html转换为haml
【发布时间】:2013-02-02 15:07:38
【问题描述】:

上面有一个projects.html文件。这个文件成功了,但是当我用http://html2haml.heroku.com/将此html文件转换为haml文件时,haml文件不起作用。

你能看出两个文件之间的区别吗?

projects.html.erb

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<script type="text/javascript">
  $(function () {
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: 'Sectoral Statistics'
            },
            tooltip: {
              pointFormat: '{series.name}: <b>{point.percentage}% - {point.y} proje</b> ',
              percentageDecimals: 1
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        formatter: function() {
                            return '<b>'+ this.point.name +'</b>: '+ Math.round(this.percentage) +' % - '+ this.y +' proje';
                        }
                    }
                }
            },
            series: [{
                type: 'pie',
                name: 'Browser share',
                data: [
                    <% @results.each do |key,value| %>     
                      ['<%= key %>',<%= value %>,<%= value %>],
                    <% end %> 
                  ]
            }]
        });
    });

});
</script>

projects.html.haml

%script{:src => "http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", :type => "text/javascript"}
%script{:src => "http://code.highcharts.com/highcharts.js"}
%script{:src => "http://code.highcharts.com/modules/exporting.js"}
:javascript
  $(function () {
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                renderTo: 'container',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: 'Sectoral Statistics'
            },
            tooltip: {
              pointFormat: '{series.name}: {point.percentage}% - {point.y} proje ',
              percentageDecimals: 1
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        formatter: function() {
                            return ''+ this.point.name +': '+ Math.round(this.percentage) +' % - '+ this.y +' proje';
                        }
                    }
                }
            },
            series: [{
                type: 'pie',
                name: 'Browser share',
                data: [
                     @results.each do |key,value|      
                      [' key ', value , value ],
                  ]
            }]
        });
    });

});

【问题讨论】:

  • 在我看来更像是顶部带有 HTML 标记的 Javascript。你打算在 Rails 项目中使用它吗?
  • 是的,它实际上是关于带有 html 的 javascript 但我将在 rails 项目中使用它
  • 我建议将您的 js 从视图模板移动到单独的文件中,并将这些文件包含在清单文件 application.js 中。在您的情况下,HTML2HAML 无法正确转换。您可以搜索 asset-pipeline in this RailsGuide,您将了解 Rails 组织您的 jscss 的方式。

标签: ruby-on-rails highcharts haml


【解决方案1】:

我认为这是html2haml 中的一个错误。

在您的 HTML 中,您的最后一个 script 标记看起来像这样(为简洁起见,我已将其删减):

<script type="text/javascript">
  $(function () {
  ...
});
</script>

第一行是缩进的,但最后一行不是。 html2haml 在确定如何缩进 javascript 时只查看第一行,这导致 Haml 看起来像:

:javascript
  $(function () {
  ...
});

最后一行 - }); - 没有缩进。

要解决这个问题,您应该在 HTML 或生成的 Haml 中修复缩进。

请注意,您应该注意 another bug:javascript 过滤器。它认为在这种情况下你应该没问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多