- 浏览: 200695 次
- 来自: ...
文章分类
最新评论
-
赤道螞蟻:
如果是數據庫有定時任務,定時更新表的數據。 表中數據變化時,主 ...
用socket.io实现WebSocket的一个简单例子 -
cwalet:
在世界的中心呼喚愛 写道提示找不到 expressnpm in ...
用socket.io实现WebSocket的一个简单例子 -
在世界的中心呼喚愛:
提示找不到 express
用socket.io实现WebSocket的一个简单例子 -
Anleb:
def m1(a)
puts 'invoke m1'
pu ...
Ruby的一些疑问 -
biyeah:
补充,任何类,只要实现to_proc方法,都可以与&结 ...
Ruby的一些疑问
http://markaby.rubyforge.org/
Markaby is a very short bit of code for writing HTML pages in pure Ruby. It is an alternative to ERb which weaves the two languages together. Also a replacement for templating languages which use primitive languages that blend with HTML.
Using Markaby as a Rails plugin
Write Rails templates in pure Ruby. Example layout:
Using Markaby as a Ruby class
Markaby is flaming easy to call from your Ruby classes.
Markaby::Builder.new does take two arguments for passing in variables and a helper object. You can also affix the block right on to the class.
See Markaby::Builder for all of that.
A Note About instance_eval
The Markaby::Builder class is different from the normal Builder class, since it uses instance_eval when running blocks. This cleans up the appearance of the Markaby code you write. If instance_eval was not used, the code would look like this:
So, the advantage is the cleanliness of your code. The disadvantage is that the block will run inside the Markaby::Builder object’s scope. This means that inside these blocks, self will be your Markaby::Builder object. When you use instance variables in these blocks, they will be instance variables of the Markaby::Builder object.
This doesn’t effect Rails users, but when used in regular Ruby code, it can be a bit disorienting. You are recommended to put your Markaby code in a module where it won’t mix with anything.
A Note About Rails Helpers
When used in Rails templates, the Rails helper object is passed into Markaby::Builder. When you call helper methods inside Markaby, the output from those methods will be output to the stream. This is incredibly handy, since most Rails helpers output HTML tags.
However, some methods are designed to give back a String which you can use elsewhere. Call the @helpers object with the method and you’ll get the String back and nothing will be output.
p "Total is: #{@helper.number_to_human_size @file_bytes}"
Conversely, you may call instance variables from your controller by using a method and its value will be returned, nothing will be output.
A Quick Tour
If you dive right into Markaby, it’ll probably make good sense, but you’re likely to run into a few kinks. Keep these pointers in mind and everything will be fine.
Element Classes
Element classes may be added by hooking methods onto container elements:
Which results in:
Element IDs
IDs may be added by the use of bang methods:
Which results in:
Markaby assumes XHTML 1.0 Transitional
Output defaults to XHTML 1.0 Transitional. To do XHTML 1.0 Strict, try this:
The capture Method
Want to catch a block of HTML as a string and play with it a bit? Use the capture method.
Commonly used to join HTML blocks together:
The tag! Method
If you need to force a tag at any time, call tag! with the tag name followed by the possible arguments and block. The CssProxy won’t work with this technique.
Credits
Markaby is a work of immense hope by Tim Fletcher and why the lucky stiff. Thankyou for giving it a whirl.
Markaby is inspired by the HTML library within cgi.rb. Hopefully it will turn around and take some cues.
Markaby is a very short bit of code for writing HTML pages in pure Ruby. It is an alternative to ERb which weaves the two languages together. Also a replacement for templating languages which use primitive languages that blend with HTML.
Using Markaby as a Rails plugin
Write Rails templates in pure Ruby. Example layout:
html do head do title 'Products: ' + action_name stylesheet_link_tag 'scaffold' end body do p flash[:notice], :style => "color: green" self << content_for_layout end end
Using Markaby as a Ruby class
Markaby is flaming easy to call from your Ruby classes.
require 'markaby' mab = Markaby::Builder.new mab.html do head { title "Boats.com" } body do h1 "Boats.com has great deals" ul do li "$49 for a canoe" li "$39 for a raft" li "$29 for a huge boot that floats and can fit 5 people" end end end puts mab.to_s
Markaby::Builder.new does take two arguments for passing in variables and a helper object. You can also affix the block right on to the class.
See Markaby::Builder for all of that.
A Note About instance_eval
The Markaby::Builder class is different from the normal Builder class, since it uses instance_eval when running blocks. This cleans up the appearance of the Markaby code you write. If instance_eval was not used, the code would look like this:
mab = Markaby::Builder.new mab.html do mab.head { mab.title "Boats.com" } mab.body do mab.h1 "Boats.com has great deals" end end puts mab.to_s
So, the advantage is the cleanliness of your code. The disadvantage is that the block will run inside the Markaby::Builder object’s scope. This means that inside these blocks, self will be your Markaby::Builder object. When you use instance variables in these blocks, they will be instance variables of the Markaby::Builder object.
This doesn’t effect Rails users, but when used in regular Ruby code, it can be a bit disorienting. You are recommended to put your Markaby code in a module where it won’t mix with anything.
A Note About Rails Helpers
When used in Rails templates, the Rails helper object is passed into Markaby::Builder. When you call helper methods inside Markaby, the output from those methods will be output to the stream. This is incredibly handy, since most Rails helpers output HTML tags.
head do javascript_include_tag 'prototype' autodiscovery_link_tag end
However, some methods are designed to give back a String which you can use elsewhere. Call the @helpers object with the method and you’ll get the String back and nothing will be output.
p "Total is: #{@helper.number_to_human_size @file_bytes}"
Conversely, you may call instance variables from your controller by using a method and its value will be returned, nothing will be output.
# Inside imaginary ProductController def list @products = Product.find :all end # Inside app/views/product/list.mab products.each do |product| p product.title end
A Quick Tour
If you dive right into Markaby, it’ll probably make good sense, but you’re likely to run into a few kinks. Keep these pointers in mind and everything will be fine.
Element Classes
Element classes may be added by hooking methods onto container elements:
div.entry do h2.entryTitle 'Son of WebPage' div.entrySection %{by Anthony} div.entryContent 'Okay, once again, the idea here is ...' end
Which results in:
<div class="entry"> <h2 class="entryTitle">Son of WebPage</h2> <div class="entrySection">by Anthony</div> <div class="entryContent">Okay, once again, the idea here is ...</div> </div>
Element IDs
IDs may be added by the use of bang methods:
div.page! div.content! h1 "A Short Short Saintly Dog" end end
Which results in:
<div id="page"> <div id="content"> <h1>A Short Short Saintly Dog</h1> </div> </div>
Markaby assumes XHTML 1.0 Transitional
Output defaults to XHTML 1.0 Transitional. To do XHTML 1.0 Strict, try this:
xhtml_strict do # innerds end
The capture Method
Want to catch a block of HTML as a string and play with it a bit? Use the capture method.
Commonly used to join HTML blocks together:
div.menu! \ ['5.gets', 'bits', 'cult', 'inspect', '-h'].map do |category| capture { link_to category } end. join( " | " )
The tag! Method
If you need to force a tag at any time, call tag! with the tag name followed by the possible arguments and block. The CssProxy won’t work with this technique.
tag! :select, :id => "country_list" do countries.each do |country| tag! :option, country end end
Credits
Markaby is a work of immense hope by Tim Fletcher and why the lucky stiff. Thankyou for giving it a whirl.
Markaby is inspired by the HTML library within cgi.rb. Hopefully it will turn around and take some cues.
发表评论
-
在Rails中使用Pry
2012-02-07 06:43 2123Pry可看成是IRB的加强版。支持语法高亮等特点。 1、在Ge ... -
camping 一个小巧的ruby web framework
2012-02-04 04:01 1756https://github.com/camping/camp ... -
Ruby的一些疑问
2012-01-26 01:01 13771、网点看到一断程序, def m1(a) puts 'i ... -
RSpec测试框架
2012-01-10 12:59 2949#参考http://www.slideshare.net/ih ... -
[转]Ruby - DUP vs CLONE
2012-01-09 12:57 1790http://railsblogger.blogspot.co ... -
[转]eval, class_eval, instance_eval和binding
2012-01-09 12:10 1058http://www.cnblogs.com/rubylouv ... -
[转]Ruby中的binding
2012-01-09 11:50 1542http://kkito.cn/index.php/blog/ ... -
[转]Method visibility in Ruby
2012-01-04 12:37 1116From:http://weblog.jamisbuck.or ... -
[转]浅谈Ruby on Rails中的include和extend
2011-12-30 02:20 1015http://developer.51cto.com/art/ ... -
[转]Ruby中的Rake任务详述
2011-12-29 03:47 1716Trackback: http://tb.blog.csdn. ... -
[转]ruby中的闭包
2011-12-26 10:42 2734原文: http://kenbeit.com/po ... -
ruby技巧
2011-12-26 10:40 0ruby小技巧之 http://kenbeit.com/pos ... -
[转]Ruby中的多态
2011-12-20 11:56 0原文http://kkito.cn/index.php ... -
ruby中实现闭包
2011-12-20 09:10 914ruby中实现闭包很简单 如果一个方法中返回一个proced ... -
ruby中星号的使用
2011-12-20 05:54 1865ruby中星号的使用 1、数字乘法 2 * 3 = 6 2 ... -
[转]ruby的include与extend
2011-12-20 05:09 868原文http://www.cnblogs.com/rubylo ... -
[转]Ruby常用的内部变量
2011-12-20 03:59 900Ruby常用的内部变量 原文http://www.cnblog ... -
ruby与javascript面向对象编程的比较
2011-12-06 14:05 3309原文:http://howtonode.org/object- ... -
解决ruby中文乱码
2011-12-06 04:20 7974在文件头加上#encoding=UTF-8,示例: #enco ... -
[转帖]重新认识Ruby的多态和duck type
2011-12-05 11:56 948多态在Java中是如何定义的? 原文:http://devel ...
相关推荐
This book is a condensed reference for HTML5 markup. It presents the essential HTML5 elements and attributes in a well-organized format that can be used as a handy reference. HTML5 Quick Markup ...
Markup (vc操作XML)
C++利用Markup解析XML C++利用Markup解析XML
vc6.0环境下,在MFC中用Markup写xml文件的简单的一个小例子,
添加到自己的工程中,可以用来读取XML中的信息,比较方便无须编译其他库
markup 是C/C++编程过程中使用的一种轻量级的XML操作工具库。附件是Markup.h和Markup.cpp文件,导入到程序中即可直接使用的。
xml解析开源代码,markup非常好用,值得推荐给大家
Markup.js, 强大的JavaScript模板 Markup.js-- 功能强大的JavaScript模板Markup.js 是一个简单而强大的JavaScript模板系统。为什么 Markup.js?Markup.js 将结构化数据转换为HTML标记或者其他文本格式的痛苦。 它
本Demo使用了Markup库来对XML文件进行曾删改查,并在博客里给出多种案例来对XML的各项操作进行说明。博客地址https://blog.csdn.net/qq_37529913。
City Geography Markup Language (CityGML) En-coding Standard an OGC Member approved international standard
c++中用来解析xml文件,解压后里面是一个DEMO,将Markup.h .cpp拷贝并添加到工程中可以使用,有官网下载。
在VC中通过第三方程序markup实现XML文档处理,主要用到了两个文件,markup.h和markup.cpp,这里有一个例程,使用起来非常方便。
一个通用的xml操作类,c++ Markup Release 11.5,头文件中含有接口说明和极少数的简单调用.
The Artificial Intelligence Markup Language is a derivative of XML (Extensible Markup Language) that is completely described in this document. Its goal is to enable pattern-based, stimulus-response ...
markup, 我们用来渲染 README.your_favorite_markup的代码 GitHub标记这个库是存储库中的每个标记文件在 GitHub.com: 上呈现之前的第一步。这里库将原始标记转换为 HTML 。 请参见下面的支持标记标记格式列表。HTML...
操作xml文件Markup类和例子,强大的Markup让你操作xml文件随心所以
Intelligent Platform Management Interface Specification Second Generation v2.0 Document Revision 1.0 February 12, 2004 June 12, 2009 Markup
Application=Code+Markup绝对的中文版。在网上长的好长时间
This specification defines the Mathematical Markup Language, or MathML. MathML is an XML application for describing mathematical notation and capturing both its structure and content. The goal of Math...