Home > Article > Web Front-end > Zen Coding Implementation of quickly writing HTML/CSS code_Experience exchange
In this article we will demonstrate a new way to quickly develop HTML and CSS using syntax that mimics CSS selectors. It was developed by Sergey Chikuyonok.
How much time do you spend writing HTML code (including all tags, attributes, quotes, braces, etc.)? If your editor has code hints, it will be easier for you to write, but even then you will still have to type a lot of code manually.
In terms of JavaScript, when we want to get a specific element on a page, we will encounter the same problem, we have to write a lot of code, which becomes difficult to maintain and reuse. JavaScript frameworks came into being, and they also introduced CSS selector engines. Now, you can use simple CSS expressions to get DOM elements, which is pretty cool.
But what if you could not only layout and position elements using CSS selectors, but also generate code? For example, if you write:
注意:该视频原版位于Vimeo,但是要看的话需要翻[和谐]墙先,地址在这里:http://vimeo.com/7405114,上面的视频是我费尽周折从Vimeo上下载下来上传到优酷的,上传后质量竟被大打折扣了,囧。youtube上也有一份视频,是基于Aptana的演示,一样很精彩:http://www.youtube.com/watch?v=ug84Ypwqfzk。PS:貌似youtube要比Vimeo翻[和谐]墙容易些,不过如何翻[和谐]墙不在本站讨论范围。
如果你想跳转到详细介绍和使用指南,请看一下演示页面并立刻下载你适用的插件:
Now let's take a look at how these tools work.
expand abbreviation feature converts CSS-like selectors into XHTML code. The term "abbreviation" can be a bit confusing. Why not just call it a "CSS selector"? Well, the first reason is semantics: "selector" means selecting something, but here we are actually generating something, means writing a long A shorter alternative to the code. Second, it just uses a small subset of the real CSS selector syntax and adds some new operators.
Here is a list of supported properties and operators:
As you can see, you already know how to use Zen Coding: just a simple faux CSS selector (er, sorry for the "abbreviation"), like this...
Zen Coding有两个主要的元素类型:“片段(snippets)” 和 “缩写(abbreviations)”。片段就是随意的代码碎片,而缩写是标签定义。通过片段,你可以写出你想要的任何代码,它也会照你写的格式输出;但是你必须手动的格式化它(使用\n 和 实现换行和缩进) 并将${child}变量放到你想要输出子元素的地方,就像这样:cc:ie6>style。如果你不使用${child}变量,子元素将会输出于代码片段的后面。
有了缩写,您必须编写标记定义,而且语法是非常重要的。通常,你必须写一个简单的带有所有默认的属性的标签,比如: 。当Zen Coding被加载后,它会解析一个标签定义到一个描述该标签的名字、属性(包括它们的顺序)以及该标签是否为空的特定的对象中。所以,如果你写,你会告诉Zen Coding这个标签必须是空的,然后“扩展缩写”行为就会在输出之前为它使用特定的规则。
对于片段和缩写,你可以添加一个管道符号,它告诉Zen Coding当缩写被展开的时候光标会被定位到哪里。默认的,Zen Coding 将光标放在空属性的引号中间以及开始和关闭标签的中间。
那么,这里解释一下当你写了一个缩写并召唤“展开缩写”行动时发生的事情。首先,它将一个完整的缩写分开为独立的元素:这样div>a 会被分成div 和a 元素,当然也会维持他们的关系。然后,每个元素,解析器先在代码片段内而后在缩写中寻找定义。如果它找不到,将会使用元素的名字作为新的标签,并为其添加缩写中定义的id和class。比如,如果你写mytag#example,解析器在片段或缩写中找不到mytag定义,它就会输出
我们制作了很多默认的CSS和HTML缩写和片段。你会发现学习使用Zen Coding可以增加你的生产力。
对于HTML编码者的另一个非常常见的任务是寻找一个元素的标签对。例如你想选择整个
Unfortunately, many modern development tools lack this functionality. Then I decided to write my own tag pair matcher as part of Zen Coding. It's still in beta and has some issues, but it works pretty well and fast. Rather than scanning the entire document (as a normal HTML tag pair matcher does), it looks for relevant tags starting from the current position of the cursor. This makes it very fast and context-free: it even works with this JavaScript snippet:
' j ' | ';
This is a really cool feature that combines the functionality of abbreviation and tag pair matchers into one. How often do you realize you need to add a wrapping element to fix a browser bug? Or do you need to add a decoration, such as a background image or border to a block-level content? You have to write the opening tag, temporarily interrupt your code, find the relevant point and then close the tag. This is where "wrapping with abbreviations" can help you.
The feature is fairly simple: it asks you to enter an abbreviation, then performs the appropriate "expand abbreviation" action and places your desired text inside the last element of your abbreviation. If you don't select any text, it starts the tag pair matcher and uses the results. It also knows where your cursor is: inside the content of the tag or between the opening and closing tags. Depending on its position, it wraps the content of the tag or the tag itself.
Abbreviation wrapping introduces a specific abbreviation syntax for wrapping individual lines. Simply jump to the number after the multiplication operator, for example: ul#nav>li*>a. When Zen Coding finds an element that uses an undefined multiplier, it treats it as a repeating element: it prints it as many times as there are lines in your chapter, and adds the The content is placed in the last child element of the repeated element.
If you wrap this abbreviation div#header>ul#navigation>li.item$*>a>span around this text:
你可以看到,Zen Coding是一个强大的文本处理工具。
这些快捷键是可以自定义的。
你已经学到很多关于Zen Coding如何工作以及它是如何使你的编码更容易了。现在为什么不自己尝试一下呢?因为Zen Coding是用纯JavaScript开发并迁移到Python,它甚至可以用于浏览器内部,这令它成为引入到CMS的首选。
Zen Coding并不依赖某个特定的编辑器。它是一个只处理文本的出色的组件:它获取文本、做一些处理并放回新的文本(或索引,用于标签匹配)。Zen Coding由JavaScript和Python编写,所以它实际上可以运行于任何平台。在Windows,你可以运行JavaScript版本,而Mac和Linux 分支可以使用Python版。
如果让你的编辑器支持Zen Coding,你需要写一个特定的可以在你的编辑器和Zen Coding之间转换数据的插件。问题是一个编辑器可能不会完整的支持Zen Coding因为它本身的插件系统。比如,TextMate通过使用脚本输出替换当前行很容的就支持了“展开缩写”功能,但是它不能处理标签对匹配因为没有标准的方法请求TextMate来选择内容。
Aptana是我主要的开发环境,它使用一个JavaScript版本的Zen Coding。它也包含很多其它的我用于日常工作的工具,所以任何一个新的Zen Coding版本都将会首先对Aptana可用,然后部署到Python并兼容其它的编辑器。
Coda和Espresso 插件被杰出的Text Editor Actions (TEA) 平台支持,由Ian Beck开发。原始的源代码在GitHub上,但我还是制作了我自己的分支以整合Zen Coding的特性。
很多尝试过Zen Coding的人都说它改变了他们写页面的方式。当然还有很多事情要做,还有很多的编辑器需要被支持以及一些文档要写。请浏览现在的文档 以及源代码以寻找你的问题的答案。希望你喜欢Zen Coding!
遗憾的是, 本文原作者并没有说明zen coding的具体用法,神飞认为有必要做以下简要的说明。这里就以Aptana/Eclipse和Dreamweaver为例,其它编辑器平台暂不描述,如有疑问可以在评论中与前端观察的网友交流。
由于Aptana本身就是基于Eclipse的,所以,Zen Coding也是支持Eclipse的,只是需要一个EclipseMonkey插件的支持,Aptana已经封装了这个插件,所以如果你使用Aptana,下面的第一步可以跳过。
Note:
The good news is that there is now a Zen coding for DreamWeaver plug-in. The bad news is that the plug-in supports very few functions and only supports the expansion of abbreviations. And the default shortcut keys are invalid. Actions can only be clicked in the Command menu. In addition, I have not tested whether the plug-in only supports the CS4 version. But what’s better is that the author has also released the source code of this plug-in, and you can customize a Dreamweaver plug-in.
In fact, the official DW plug-in has a bit of a bug under Windows, that is, blank lines will appear. I simply corrected it and recompiled the package. It is no problem when tested on this machine. If you are interested Children's shoes can be downloaded and tried: http://www.boxcn.net/shared/c71z7x7sfe
PS: The official DW plug-in has been updated, recommend to the official Go to download. New plugins add more feature support. UPDATE @ 12-23-2009
Special recommendation: Student Haoqing put abbreviations into practice and summarized many great use cases. I recommend everyone Go learn.
Sergey Chikuyonok is a Russian front-end development engineer and author with a passion for optimization: from images and JavaScript effects to workflow and time-saving coding. Visit his homepage and his Twitter.
Original text from: http://www.qianduan.net/zen-coding-a-new-way-to-write-html-code.html