http://www.johnpapa.net/zen-coding-in-visual-studio-2012

Zen Coding is a faster way to write HTML using a CSS style selector syntax, and you can now use Zen Coding in Visual Studio via the Web Essentials 2012 plug in (v1.7).  Zen Coding was introduced by Sergey Chikuyonok in 2009 (according to Smashing Magazine) and has been updated over time to become a great way to write monotonous HTML much more efficiently.
 
Special thanks  to Mads Kristensen and his team at Microsoft for adding Zen Coding to Visual Studio 2012 via Web Essentials 2012 (along with many other great features).

Quick Reference

Here is a quick list of the Zen Coding features that are now supported in Visual Studio 2012 via the Web Essentials 2012 plug in

  • # creates an id attribute
  • . creates a class attribute
  • [ ] creates a custom attribute
  • > creates a child element
  • + creates a sibling element
  • ^ climbs up
  • * is element multiplication. This creates the same thing n number of times
  • $ is replaced with an incremental number
  • $$ is used for numbers with padding
  • { } creates text in an element
What can you do? Here is an example:
  1. <!-- Type this -->
  2. ul[data-bind="foreach:customers"]>li*4>span{Caption $$}+input[type=text data-bind="value:$$"]
  3. <!-- Creates this -->
  4. <uldata-bind="foreach:customers">
  5. <li><span>Caption 01</span><inputtype="text"value=""data-bind="value:01"/></li>
  6. <li><span>Caption 02</span><inputtype="text"value=""data-bind="value:02"/></li>
  7. <li><span>Caption 03</span><inputtype="text"value=""data-bind="value:03"/></li>
  8. <li><span>Caption 04</span><inputtype="text"value=""data-bind="value:04"/></li>
  9. </ul>
Let’s take a closer look at the different symbols used

ID and Class Attributes: # and .

You can create an element and assign it an id or class attribute using CSS style syntax.
  1. <!-- Type this -->
  2. div#contentRegion.address
  3. <!-- Creates this -->
  4. <divid="contentRegion"class="address"></div>

Custom Attributes: [ ]

You can create any attribute the square bracket syntax.
  1. <!-- Type this -->
  2. div[title]
  3. <!-- Creates this -->
  4. <divtitle=""></div>
Or create multiple attributes and fill in values
  1. <!-- Type this -->
  2. input[placeholder="Name" type="text"]
  3. <!-- Creates this -->
  4. <inputtype="text"value=""placeholder="Name"/>

Child Elements: >

Create an element and then a child element inside of it. In this example I create a div with the id=menu that contains a span with a class="item" and a blank title attribute
  1. <!-- Type this -->
  2. div#menu>span.item[title]
  3. <!-- Creates this -->
  4. <divid="menu">
  5. <spanclass="item"title=""></span>
  6. </div>

Sibling Elements: +

You can create a sibling element easily too.
  1. <!-- Type this -->
  2. footer>div>a+input
  3. <!-- Creates this -->
  4. <footer>
  5. <div>
  6. <ahref=""></a>
  7. <inputtypevalue=""/>
  8. </div>
  9. </footer>

Climbing Elements: ^

The > operator descends into element hierarchy while the ^ climbs up the hierarchy. You can also climb multiple levels. For example: use 1 ^ to climb 1 level or use 4 ^ to climb 4 levels.
  1. <!-- Type this -->
  2. footer>div>a+input^^p
  3. <!-- Creates this -->
  4. <footer>
  5. <div>
  6. <ahref=""></a>
  7. <inputtypevalue=""/>
  8. </div>
  9. <p></p>
  10. </footer>

Multiplication: *

Create n number of elements
  1. <!-- Type this -->
  2. ul>li*4>span
  3. <!-- Creates this -->
  4. <ul>
  5. <li><span></span></li>
  6. <li><span></span></li>
  7. <li><span></span></li>
  8. <li><span></span></li>
  9. </ul>

Item Numbering: $

When using the multiplication to create n number of elements, you can add an incremental number to them using the $. Notice that using multiple $ operators (ex: $$) creates pads the numbers with 0′s.
  1. <!-- Type this -->
  2. section>article.item$$*4
  3. <!-- Creates this -->
  4. <section>
  5. <articleclass="item01"></article>
  6. <articleclass="item02"></article>
  7. <articleclass="item03"></article>
  8. <articleclass="item04"></article>
  9. </section>

Text: } {

You can enter text values inside of elements, without changing the parent context.
  1. <!-- Type this -->
  2. ul>li*4>span{Caption $$}
  3. <!-- Creates this -->
  4. <ul>
  5. <li><span>Caption 01</span></li>
  6. <li><span>Caption 02</span></li>
  7. <li><span>Caption 03</span></li>
  8. <li><span>Caption 04</span></li>
  9. </ul>
This does not change the parent context, so when specifying the sibling to follow the text, the sibling element will actually follow the element prior to the text. That’s why the example below creates an anchor tag next to the span tag.
  1. <!-- Type this -->
  2. ul>li*4>span{Caption $$}+a{click me}
  3. <!-- Creates this -->
  4. <ul>
  5. <li><span>Caption 01</span><ahref="">click me</a></li>
  6. <li><span>Caption 02</span><ahref="">click me</a></li>
  7. <li><span>Caption 03</span><ahref="">click me</a></li>
  8. <li><span>Caption 04</span><ahref="">click me</a></li>
  9. </ul>

Combining Them all

You can combine multiple features together which allows you to write some pretty cool HTML much faster. You can even use this to create some Knockout.js bindings for templates, and then just change the property names.
  1. <!-- Type this -->
  2. section[data-bind="foreach:customers"]>div*4>input[type="text" data-bind="text:$$"]
  3. <!-- Creates this -->
  4. <sectiondata-bind="foreach:customers">
  5. <div>
  6. <inputtype="text"value=""data-bind="text:01"/>
  7. </div>
  8. <div>
  9. <inputtype="text"value=""data-bind="text:02"/>
  10. </div>
  11. <div>
  12. <inputtype="text"value=""data-bind="text:03"/>
  13. </div>
  14. <div>
  15. <inputtype="text"value=""data-bind="text:04"/>
  16. </div>
  17. </section>

Grouping: ( )

Grouping is a powerful feature of Zen Coding that allows you to create complex expressions. It is not yet in Web Essentials 2012, but I assume it will come in the near future. If it does arrive, you would be able to create entire sections of a DOM very easily.
  1. <!-- Type this -->
  2. div>(header>div)+section>(ul>li*2>a)+footer>(div>span)
  3. <!-- WOULD create this (not yet supported in Web Essentials 2012)-->
  4. <div>
  5. <header>
  6. <div></div>
  7. </header>
  8. <section>
  9. <ul>
  10. <li><ahref=""></a></li>
  11. <li><ahref=""></a></li>
  12. </ul>
  13. </section>
  14. <footer>
  15. <div>
  16. <span></span>
  17. </div>
  18. </footer>
  19. </div>
As you can see, this would make it quite simple to create large sections of HTML with just a few keystrokes.

Lorem Ipsum Generator

(Added Dec 5, 2012)

You can now generate Lorem Ipsum directly in the HTML editor. Type “lorem” and hit TAB and a 30 word Lorem Ipsum text is inserted. Type “lorem10″ and a 10 word Lorem Ipsum text is inserted.

ul>li*5>lorem3

References

Zen Coding in Visual Studio 2012的更多相关文章

  1. 如何在Visual Studio 2012中发布Web应用程序时自动混淆Javascript

    同Java..NET实现的应用程序类似,Javascript编写的应用程序也面临一个同样的问题:源代码的保护.尽管对大多数Javascript应用公开源代码不算是很严重的问题,但是对于某些开发者来说, ...

  2. 在Visual Studio 2012中使用VMSDK开发领域特定语言(二)

    本文为<在Visual Studio 2012中使用VMSDK开发领域特定语言>专题文章的第二部分,在这部分内容中,将以实际应用为例,介绍开发DSL的主要步骤,包括设计.定制.调试.发布以 ...

  3. 在Visual Studio 2012中使用VMSDK开发领域特定语言(一)

    前言 本专题主要介绍在Visual Studio 2012中使用Visualization & Modeling SDK进行领域特定语言(DSL)的开发,包括两个部分的内容.在第一部分中,将对 ...

  4. Visual Studio 2012 trial version

    Update: vs2012.5.iso http://download.microsoft.com/download/9/F/1/9F1DEA0F-97CC-4CC4-9B4D-0DB45B8261 ...

  5. 在Visual Studio 2012 Blue theme下使用Dark theme的文本编辑器颜色设置

    Visual Studio 2012 默认提供了3种color theme: blue,light,和dark.其中dark的文本编辑器颜色设定很爽,可是整个菜单项加上一些小的窗口如Find Resu ...

  6. 分享10条Visual Studio 2012的开发使用技巧

    使用Visual Studio 2012有一段时间了,并不是追赶潮流,而是被逼迫无可奈何.客户要求的ASP.NET MVC 4的项目,要用.NET 4.5来运行.经过一段时间的摸索,得到一点经验和体会 ...

  7. Visual Studio 2012 Update 4 RC 启动调试失败解决方案

    以下解决办法适用于任何Visual Studio开发环境,及Windows NT 6.1以上系统. 系统:Windows 8.1 Enterprise x64 RTM 开发环境:Visual Stud ...

  8. SQL Server Data Tools – Business Intelligence for Visual Studio 2012安装时提示“The CPU architecture....”的解决方法

    SQL Server Data Tools – Business Intelligence for Visual Studio 2012,一个很强大的工具,下载地址:http://www.micros ...

  9. Visual Studio 2012+jQuery-1.7.1

    今天用Visual Studio 2012开发一个网站项目,在集成jqplot图表控件并进行调试的时候(使用的是MVC4框架),加载网页绘制图表的时候总是报错(提示$.jqplot.barRender ...

随机推荐

  1. NYOJ 12 喷水装置(二)

    pid=12">喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n( ...

  2. Android getTopActivity的方法

    使用例如以下方法能够获得top activity 的name public String getTopActivityPackageName(Context context) { String top ...

  3. k-d tree算法

    k-d树(k-dimensional树的简称),是一种分割k维数据空间的数据结构.主要应用于多维空间关键数据的搜索(如:范围搜索和最近邻搜索). 应用背景 SIFT算法中做特征点匹配的时候就会利用到k ...

  4. hadoop学习;大数据集在HDFS中存为单个文件;安装linux下eclipse出错解决;查看.class文件插件

    sudo apt-get install eclipse 安装后打开eclipse,提示出错 An error has occurred. See the log file /home/pengeor ...

  5. matlab 2014a 改为英文版本号

    1. 在 Matlab 的安装目录以下找到例如以下的路径,X:\MATLAB\R2014a\java\jar,当中 X 为安装盘符,这个不用过多解释了,然后找到目录 zh_CN.此目录就是中文界面的语 ...

  6. ubuntu 系统设置bugzilla制

    随着时间的推移.在大脑中形成的记忆总会慢慢的淡去.人的记忆力就是这样.所以最好的办法就是形成博客去记录下来,一方面给自己以后回想用.一方面也算是自己的一个积累.所以一旦选择了一个行业,最好不要轻 易转 ...

  7. Dreamer 3.0 支持json、xml、文件上传

    自己写的框架,功能类似Struts2.x 下载地址:http://pan.baidu.com/share/link?shareid=3273223286&uk=470382596 新增功能: ...

  8. error: png.h not found.

    跑php设备 --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable- ...

  9. Oracle SQL Lesson (7) - 使用子查询

    使用子查询简单子查询SELECT select_listFROM tableWHERE expr operator (SELECT select_list FROM table);子查询可以出现在se ...

  10. eclipse 于 Tomcat于 热部署 project

    eclipse在 Tomcat中热部署project 1.在eclipse中国安装一个tomcat插件:SysdeoEclipse Tomcat Launcher plugin(http://www. ...