是否可以在 php/html 风格的情况下正确完成嵌套?
我怀疑甚至可能有一个数学证明来证明这个问题的答案是“不”,但是,问题是:是否可以发明一种类似 php 的语言(即一些在幕后评估代码的行,以及一些在幕后评估代码的行)评估显示的html)它总是可以正确嵌套的?举一个我正在谈论的例子,在rails/haml中,
%table
%tr
%th Title
%th Content
%th Owner
%th Categories
- @posts.each do |post|
%tr
%td
第二个%tr应该与第一个%tr垂直对齐(因为它们在输出html中是兄弟姐妹),但是开始每个块的行导致它是多缩进一行。是否有人可以开发某种 html 元语言,其中缩进可以反映控制结构和正确的嵌套,而不会相互冲突?如果有的话,这样的事情存在吗?
I suspect that there might even be a mathematical proof that the answer to this is "no", but, question: Could one invent a type of php-like language (ie with some lines that evaluate code behind the scenes, and some lines that evaluate to displayed html) where it could always be properly nested? To give an example of what I'm talking about, in rails/haml
%table
%tr
%th Title
%th Content
%th Owner
%th Categories
- @posts.each do |post|
%tr
%td
the second %tr should be aligned vertically with the first (as they are siblings in the output html), but the line that begins the each block causes it to be indented one additional line. Is it possible that someone could develop some sort of of html meta-language where indentation can reflect both control structures and proper nesting, without each coming into conflict with the other? If so, does such a thing exist?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以想一些办法。编译器/解释器只需要做出一些仔细且明确概述的假设。
例如,假设为了解释页面结构而删除了控制结构的缩进,那么上面的代码会将数据
%tr
置于与%td
相同的缩进级别。请注意,这会使某些页面结构难以阅读。另一个例子,使控制结构语法与给定缩进级别的页面结构共存。控制结构可以单独存在,也可以存在于页结构语句之后。这样做的优点是代码和页面结构都可读。
I can think of some ways. The compiler/interpreter simply needs to make some assumptions that are carefully and explicitly outlined.
For example, assume that indents for control structures are removed for interpreting page structure, then your code above would put the data
%tr
at the same indentation level as the%td
. Note that this would make some of the page structure hard to read.Another example, make the control structure syntax coexist with page structure at a given indentation level. A control structure can exist alone or after a page structure statement. This has the advantage of being readable both in terms of code and page structure.