Haml - 如何强制标签属性仅使用双引号以及如何按照我想要的方式排序标签属性?

发布于 2024-07-23 06:22:02 字数 1665 浏览 6 评论 0原文

我对稍后在 PHP 中使用的模板使用 staticmatic。 有一种奇怪的情况,有些标签属性有单引号,而有些标签属性有双引号。 我希望它们全部都有双引号(我猜这并不重要,但我希望它们像那样!)

例如,haml代码:

!!! XML
%html{html_attrs('hr-HR')}
  %head
    %title Some title
    %meta{'http-equiv' => 'Content-Type', :content => 'text/html; charset=utf-8'}/
    %meta{'name' => "description", :content => 'Some title - YO!'}/
    = stylesheets
    = javascripts('test', :other)
  %body
    = yield

产生以下内容:

<?xml version='1.0' encoding='utf-8' ?>
<html lang='hr-HR' xml:lang='hr-HR' xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title>Some title</title>
    <meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
    <meta content='Some title - YO!' name='description' />
    <link href="stylesheets/application.css" media="all" rel="stylesheet" type="text/css"/><link href="stylesheets/grid.css" media="all" rel="stylesheet" type="text/css"/><link href="stylesheets/text.css" media="all" rel="stylesheet" type="text/css"/>
    <script language="javascript" src="javascripts/test.js" type="text/javascript"></script><script language="javascript" src="javascripts/other.js" type="text/javascript"></script>

  </head>
  <body>
    <h1>some body stuff!</h1>
    utf test šđčćž ŠĐČĆŽ
  </body>
</html>

请注意,如果我使用单引号或双引号,这并不重要Haml 代码中的引号,我总是得到相同的输出!

另外,似乎 haml->html 输出按字母顺序对标签属性进行排序,而不是我在 haml 中对它们进行排序的方式。 我怀疑这与 ruby​​ 数组有关,但我不确定,因为除了 staticmatic 中的 haml 之外,我不/不能使用 Ruby。 我怎样才能让它们的顺序与我在 haml 代码中的 ruby​​ 数组中的顺序相同?

I use staticmatic for templates I use later with PHP. There is an odd situation where some tag attributes have single quotes, while some have double quotation marks. I would like all of them to have double quotes exclusively (not that it matters I guess, but I want them like that!)

For example, haml code:

!!! XML
%html{html_attrs('hr-HR')}
  %head
    %title Some title
    %meta{'http-equiv' => 'Content-Type', :content => 'text/html; charset=utf-8'}/
    %meta{'name' => "description", :content => 'Some title - YO!'}/
    = stylesheets
    = javascripts('test', :other)
  %body
    = yield

produces following:

<?xml version='1.0' encoding='utf-8' ?>
<html lang='hr-HR' xml:lang='hr-HR' xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title>Some title</title>
    <meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
    <meta content='Some title - YO!' name='description' />
    <link href="stylesheets/application.css" media="all" rel="stylesheet" type="text/css"/><link href="stylesheets/grid.css" media="all" rel="stylesheet" type="text/css"/><link href="stylesheets/text.css" media="all" rel="stylesheet" type="text/css"/>
    <script language="javascript" src="javascripts/test.js" type="text/javascript"></script><script language="javascript" src="javascripts/other.js" type="text/javascript"></script>

  </head>
  <body>
    <h1>some body stuff!</h1>
    utf test šđčćž ŠĐČĆŽ
  </body>
</html>

note that it doesn't matter if I use single quotes or double quotes in haml code, I always get the same output!

Also, it seems that haml->html output sorts tag attributes alphabetically, not the way I have ordered them in haml. I suspect this has something to do with ruby arrays, but I'm not sure since I don't/can't use Ruby apart from haml in staticmatic. How could I have them ordered the same as I have ordered them in ruby array in haml code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山田美奈子 2024-07-30 06:22:02

请尝试以下操作:

Haml::Template.options[:attr_wrapper] = '"'

Try the following:

Haml::Template.options[:attr_wrapper] = '"'
终陌 2024-07-30 06:22:02

Haml 确实按字母顺序对属性进行排序,这确实是 Ruby 解析器的结果。 将来,属性可能会尽可能按文档顺序排序,但这在 Haml 2.2 或更高版本之前不太可能发生。

Haml does indeed order attributes alphabetically, and this is indeed a consequence of Ruby's parser. In the future, attributes may be ordered in document order as much as possible, but that's not likely to happen until Haml 2.2 or later.

铜锣湾横着走 2024-07-30 06:22:02

引用自:http://haml.info/docs/yardoc/file.REFERENCE。 html#选项

Haml 了解影响其的各种配置选项
性能和输出。

在 Rails 中,可以通过设置 Haml::Template.options 来设置选项
初始化器中的哈希值:

# config/initializers/haml.rb

Haml::Template.options[:format] = :html5 
  

在 Rails 之外,您可以通过在 Haml::Options.defaults 中全局配置它们来设置它们:

Haml::Options.defaults[:format] = :html5 
  

最后,您还可以通过将选项哈希传递给 [Haml::Engine#initialize][1] 来设置它们。 为了
可用选项的完整列表,请参阅 [Haml::Options][2]。

[1]:
http://haml.info/docs/yardoc/Haml/Engine.html #initialize-instance_method

[2]:http://haml.info/docs/yardoc/Haml/选项.html

Quote from: http://haml.info/docs/yardoc/file.REFERENCE.html#options

Haml understands various configuration options that affect its
performance and output.

In Rails, options can be set by setting the Haml::Template.options
hash in an initializer:

# config/initializers/haml.rb

Haml::Template.options[:format] = :html5

Outside Rails, you can set them by configuring them globally in Haml::Options.defaults:

Haml::Options.defaults[:format] = :html5

Finally, you can also set them by passing an options hash to [Haml::Engine#initialize][1]. For
the complete list of available options, please see [Haml::Options][2].

[1]:
http://haml.info/docs/yardoc/Haml/Engine.html#initialize-instance_method

[2]: http://haml.info/docs/yardoc/Haml/Options.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文