开发者

xml tag with a dot in haml

I have a tag开发者_开发技巧 that contains a dot (.) that I want haml to preserve:

Haml:

%text
 %text.resource
  ...

I would like Haml to expand to:

<text>
   <text.resource>...
   </text.resource>
<text>

but it keeps doing:

<text>
   <text class="resource">...
   <text>
<text>

Is there any easy way to "escape" "class" expansion in Haml?


HAML is made to generate HTML of various forms, but you can trick it to generate other things by being creative. Putting in what you want to get back out:

<text>
<text.resource>...
</text.resource>
<text>

will work, because if HAML sees a line that doesn't start with one of its reserved characters it'll output it as is. You can't indent though, or it will get mad.

From the docs:

Note that HTML tags are passed through unmodified as well. If you have some HTML you don’t want to convert to Haml, or you’re converting a file line-by-line, you can just include it as-is. For example:

%p
  <div id="blah">Blah!</div>

is compiled to:

<p>
  <div id="blah">Blah!</div>
</p>

You could do:

<text>
= "  <text.resource>..."
= "  </text.resource>"
<text>

if you insist on indentation:

>>    <text>
>>      <text.resource>...
>>      </text.resource>
>>    <text>

EDIT:

The OP says:

the problem I have is that the elypsis (...) means that I have to add more haml code there (a bunch of xml tags that would be "children" of and therefore I need to "indent" the lines after the comments...

XML doesn't care about indentation; Indentation is a for-human-eyes-only aesthetic. I'd worry more about being functionally and syntactically correct. If you absolutely have to have "pretty" XML, then consider running the HAML output through xmllint, or tidy with the xml flags set.

Or, abandon HAML because you're starting to abuse it, and use something like ERB and/or Erubis which is more free form and less caring about syntax, or go old-school and generate the XML via print and puts statements. If you insist on using HAML and having your indentation, then I'd suggest consulting with the HAML developers and see if they have a recommendation. There might be a HAML filter that would be of use, or some other way of forcing the indentation level inline.

My advice, as someone who's been doing this a long time and been there too many times is: We, as software developers, can lose sight of the end-goal of being functional and spin off into some yak-shaving exercise worrying about minutia that don't accomplish anything real. Unless it's a specification that every indenting space is sacred I'd worry more about getting correct XML and move on, then later return to it and see if it can be tweaked to perfection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜