想知道是否可以使用Sass数组,因为我发现自己重复了以下几点:
.donkey
h2
background-color= !donkey
.giraffe
h2
background-color= !giraffe
.iguana
h2
background-color= !iguana
Run Code Online (Sandbox Code Playgroud) 似乎在HAML的:javascript过滤器内部,不允许使用Ruby代码,甚至不是注释.
所以这是不允许的:
:javascript
- 1.upto(10) do |i|
:javascript
-# just a comment not to show to public
Run Code Online (Sandbox Code Playgroud)
(有人说没有办法在:javascript过滤器中隐藏这样的评论
.这是真的吗?
但似乎唯一允许的是
:javascript
$('#aDiv').html('#{a_ruby_variable}');
Run Code Online (Sandbox Code Playgroud)
只允许这个#{}.Ruby中没有其他东西是允许的吗?
我想在我的js.erb文件中渲染一个部分,以便我可以在我的Javascript中使用生成的HTML.这里有一些代码作为例子.
create.js.erb
$(function(){
var html = "<%= render(:partial => `pretty_box`) %>";
$("#container").prepend(html);
});
Run Code Online (Sandbox Code Playgroud)
_pretty_box.html.haml
.pretty_box_container
.title
Something Pretty
Run Code Online (Sandbox Code Playgroud)
当渲染create.js.erb时,我得到以下内容:
$(function(){
var html = "<div class="pretty_box_container>
<div class="title">
Something Pretty
</div>
</div>";
$("#container").prepend(html);
});
Run Code Online (Sandbox Code Playgroud)
正如您所料,这打破了我的JavaScript.我需要从渲染部分的结果中去除空白.问题是的返回值render是一个ActiveSupport::SafeBuffer对象,它覆盖了所有"不安全"的方法(参见UNSAFE_STRING_METHODS),包括strip.所以调用render(:partial =>pretty_box ).strip对整个字符串进行HTML编码.
我尝试过使用这些方法html_safe或各种各样的组合to_s.它们不起作用,因为它们返回self,并且使用raw不起作用,因为它调用to_s.html.safe.
我知道我可以为我的HAML 附加>和<字符,但我不想为每个部分的每一行都这样做.
我想以某种方式设备mako.lookup.TemplateLookup,以便它仅为某些文件扩展名应用某些预处理器.
具体来说,我有一个haml.preprocessor我想应用于文件名结尾的所有模板.haml.
谢谢!
我有这个名为Post的模型,idaelly我很想使用一个局部+一个布局来实现以下功能.
渲染单个对象时,输出:
%div= post.body
Run Code Online (Sandbox Code Playgroud)
在渲染集合时,输出:
%ul
%li= post.body
%li= post.body
%li= post.body
Run Code Online (Sandbox Code Playgroud)
目前我有一个部分帖子/ post.haml看起来像: %li= post.body
每当我渲染一个集合时,我都会这样做%ul=render @posts
问题是:
虽然在我的90%的用例中我正在渲染一个帖子的集合,但是对于一个不能作为独立模板使用的帖子来说没有意义.
我以为我可以做点什么
# view
render partial: 'post', collection: @posts, layout: 'list_of_posts'
# list_of_posts
%ul= yield
# posts/post
%li= post.body
Run Code Online (Sandbox Code Playgroud)
如果它工作,这将解决我的第一个问题,但事实并非如此.显然,render_collection不采用布局选项,因此它几乎是我在渲染集合中找到的死胡同.(Spacer_template可能有用,但是
</li><li> 作为间隔符绝不是一段好的代码.. 至于我的第二个问题,一个简单的方法就是渲染div中的所有内容,但是当事情应该在列表中时,我真的不愿意这样做.但为了使其工作,它可能是唯一的清洁解决方案.div.list-of-posts > div.post代替ul.posts > li
我知道我总能做点什么
# view - collection
%ul= render @posts, in_list: true
# view - object
= render @post
# posts/post.haml
- post_counter ||= false
- tag = post_counter …Run Code Online (Sandbox Code Playgroud) 原文:有没有人想过,或者是否有人有兴趣帮我弄清楚如何在Laravel 4中使用haml?
更新:正在测试使用HAML(MtHaml)的Laravel 4的全新实现.你可以在我的Github回购中获得并测试它.
注意:请 注意,在此阶段只有HAML/PHP引擎正在工作,因此模板继承尚未运行.要使模板继承工作,需要通过HAML/Twig引擎运行MtHaml,然后它将为我们提供所有Twigs模板引擎的强大功能以及我们的循环等非常好的类似OOP的语法.
如果有人喜欢帮助完成包装,将Twig包含在一个功能齐全的模板引擎中,可随意使用Fork and Pull !! :d
谢谢.
这很难解释,所以这里有一个笔,可以看到所有的例子.
<table>
<thead>
<tr>
<th>Thing 1</th>
<th>Thing 2</th>
<th colspan=2>Thing 3</th>
<th>Thing 4</th>
<th colspan=2>Thing 5</th>
<th>Thing 6</th>
<th>Thing 7</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan=2>40</td>
<td rowspan=2>30</td>
<th>Right</th>
<th>Left</th>
<td rowspan=2>50</td>
<th>Right</th>
<th>Left</th>
<td rowspan=2>25</td>
<td rowspan=2>30</td>
</tr>
<tr>
<td>10</td>
<td>20</td>
<td>15</td>
<td>40</td>
</tr>
</tbody>
</table>
Run Code Online (Sandbox Code Playgroud)
<table>
<thead>
<tr>
<th>Thing 1</th>
<th>Thing 2</th>
<th colspan=2>Thing 3</th>
<th>Thing 4</th>
<th colspan=2>Thing 5</th>
<th>Thing 6</th>
<th>Thing 7</th>
</tr>
<tr>
<th colspan=2></th>
<th>Right</th>
<th>Left</th>
<th></th>
<th>Right</th>
<th>Left</th> …Run Code Online (Sandbox Code Playgroud) 我试图在我的一个Haml视图中的一个If/Else语句中放入一些(非渲染的)注释,但它似乎导致了问题.
我想要以下代码:
- # Stuff like ______ activates the if statement
- if @condition
(Some code)
- # Stuff like _____ activates the else statement
- else
(Some other code)
Run Code Online (Sandbox Code Playgroud)
不幸的是,Rails抛出了这个错误:
Got "else" with no preceding "if"
Run Code Online (Sandbox Code Playgroud)
如果我删除'else'评论,即
- # Stuff like ______ activates the if statement
- if @condition
(Some code)
- else
(Some other code)
Run Code Online (Sandbox Code Playgroud)
一切都按预期工作.问题不在于评论本身.我必须删除实际的Ruby代码行(包括连字符)以使其呈现.也就是说,即使我只留下一个前面带有连字符的空行,如下所示:
- # Stuff like ______ activates the if statement
- if @condition
(Some code)
-
- else
(Some other code)
Run Code Online (Sandbox Code Playgroud)
我犯了同样的错误.其他可能相关的细节:稍后有更多的代码与if/else语句(不在其中)相同的缩进级别,并且整个事物嵌套在表单中.有人可以向我解释出现了什么问题吗?非常感谢!
PS这是我的第一个问题,所以如果我不恰当地提出这个问题,请告诉我.
我在Ruby on Rails中编写的Web应用程序具有大量使用的内容面板.我正在尝试编写助手来使用更清晰的语法渲染这些面板.我对编写视图助手并不是很熟悉,所以这可能很简单.但是,当尝试使用传递给块中的助手的"html"(erb代码)时,我会得到奇怪的结果,其中块被渲染两次.
相关代码如下.这个例子使用ERB,因为我简化了它以避免HAML的任何可能的问题,但我得到了与HAML相同的结果.
我想要呈现的HTML是:
<section class="panel panel-default">
<header class="panel-heading">Contacts</header>
<div class="panel-body">
<p>Test</p>
</div>
</section>
Run Code Online (Sandbox Code Playgroud)
这是我的帮手:
module ApplicationHelper
def panel(type = :default, &block)
tag = content_tag(:section, class: "panel panel-#{type.to_s}") do
block.call PanelHelper.new
end
tag
end
class PanelHelper
include ActionView::Helpers::TagHelper
include ActionView::Context
include ActionView::Helpers::CaptureHelper
def header(text = nil, &block)
if block_given?
tag = content_tag(:header, block.call, class: 'panel-heading')
elsif text
tag = content_tag(:header, text, class: 'panel-heading')
else
raise ArgumentError, 'Either a block must be given, or a value must be provided for …Run Code Online (Sandbox Code Playgroud) 我有一个接收变量的HAML部分,bar我想在link_to路径中注入该变量.
例如:
= link_to new_foo_path, class: 'source card' do
.stuff
Run Code Online (Sandbox Code Playgroud)
我想用bar替换foo.
我试过了:
= link_to new_#{bar}_path, class: 'source card' do
Run Code Online (Sandbox Code Playgroud)
还有其他十几件事,但似乎没什么用.
思考?