简体   繁体   English

在Rails之外的HAML中渲染HAML部分

[英]Rendering HAML partials from within HAML outside of Rails

I'm using HAML to generate some static html pages for a site, and I was wanting to split out common components into partials that I can include in multiple pages, just like in Rails. 我正在使用HAML为网站生成一些静态html页面,我想将常见组件拆分为可以包含在多个页面中的部分,就像在Rails中一样。 However I don't want to use the whole Rails stack to do this as it seems like overkill. 但是我不想使用整个Rails堆栈来执行此操作,因为它看起来有点矫枉过正。

I've looked around on the Internet but haven't found anything, better than just doing something like: 我在互联网上环顾四周,但没有找到任何东西,比做以下事情更好:

Haml::Engine.new(IO.read("header.haml")).render

Is there a nicer way of including so-called partials from within HAML? 是否有更好的方法在HAML中包含所谓的部分内容? An existing filter or command that I'm missing? 我缺少的现有过滤器或命令?

It's best to combine haml & sass with a tool for building static websites. 最好将haml和sass与用于构建静态网站的工具结合起来。 Here's some links: 这是一些链接:

I'm using jekyll for my blog, but if you're not building a blog it's probably not appropriate for your needs. 我正在为我的博客使用jekyll,但如果你没有建立一个博客,它可能不适合你的需求。

Darn, you're right – there isn't a built-in way. Darn,你是对的 - 没有内置的方式。 I've used helpers with the haml command line, but always ones whose output was already HTML formatted. 我已经使用了haml命令行的帮助程序,但总是那些输出已经是HTML格式的。

My best suggestion is to write a partial() method and require it. 我最好的建议是编写一个partial()方法并要求它。 It looks like you have already started down this path. 看起来你已经开始走这条路。 I would suggest anyone writing such a function consider keeping the original binding around in some way. 我会建议任何人写这样的函数考虑以某种方式保持原始绑定。 Haml::Helpers#capture_hame looks to be the easiest way to make this happen. Haml :: Helpers#capture_hame看起来是实现这一目标的最简单方法。

If execution speed is an issue, it might also be a good idea to cache some of the parsed template in the same way that Merb does it. 如果执行速度是一个问题,那么以与Merb相同的方式缓存一些已解析的模板也是一个好主意。

If someone does get some code working, please put it up on GitHub and make a comment here. 如果有人确实使用了一些代码,请将其放在GitHub上并在此处发表评论。

I finally found what I was looking for, so I thought I'd share my find with you. 我终于找到了我想要的东西,所以我想我会和你分享我的发现。 you can simply use the same syntax you use for haml in the first place - http://www.semicomplete.com/blog/geekery/partials-in-haml-in-sinatra.html 你可以在第一时间使用与haml相同的语法 - http://www.semicomplete.com/blog/geekery/partials-in-haml-in-sinatra.html

/views/file.haml /views/file.haml

%h3 Title
%div= haml :content, :layout => false

/views/content.haml /views/content.haml

%p your content here

All of the solutions seemed bulky for my purposes, though I'm trying to do basically the same thing. 所有的解决方案对我来说似乎都很笨重,尽管我试图基本上做同样的事情。 Here is a ruby script I wrote that does precious little - evaluates Haml with the option to stick in = partial "test.haml" anywhere you like. 这是我编写的一个非常小的红宝石脚本 - 评估Haml可以选择在任何你喜欢的地方加入= partial "test.haml" It does a trivial amount of logic to try and find a partial file. 它尝试找到一个部分文件需要很少的逻辑。

require 'haml'

def find filename
  return filename if File.exists? filename
  path = File.dirname ARGV[0]
  filename = path+"/"+filename
  return filename if File.exists? filename
  throw "Could not find file."
end

def partial filename
  source = File.read(find(filename))
  engine = Haml::Engine.new(source)
  engine.render(binding)
end

puts partial ARGV[0]

you execute it like so : ruby hamlize.rb input.haml > output.html 你像这样执行它:ruby hamlize.rb input.haml> output.html

I had the same problem. 我有同样的问题。 I needed to generate some html snippets to optimize the server response, once this snippets have a lot of calculations(for drawing graphs) and every time a user do a request it was doing all the stuff unnecessarily. 我需要生成一些html片段来优化服务器响应,一旦这个片段有很多计算(用于绘制图形),并且每次用户执行请求时它都会不必要地执行所有操作。

Once i have the views partitioned in several haml partials, i wanted to reuse this to generate the html files. 一旦我将视图分区为几个haml partials,我想重用它来生成html文件。

My first approach was trying with the Haml engine, but once i have partials rendering other partials and some functions of the application helper, it didn't worked. 我的第一种方法是尝试使用Haml引擎,但是一旦我有部分渲染应用程序助手的其他部分和一些功能,它就没有用了。 Finality i found a solution that passed by creating a function in the models that i wanted to create an html file ( to_html ) Here is it: Finality我找到了一个解决方案,通过在我想创建一个html文件的模型中创建一个函数来传递(to_html)这是它:

def to_html
  ActionView::Base.send :include, ActionView::Helpers::ApplicationHelper

  File.open(File.join(Rails.root, "public", 'test.html'), "w") do |file|

  file.print ActionView::Base.new(Rails.configuration.paths["app/views"].first).render(
                :partial => 'partial_folder/partial', 
                :format => :html,
                :locals => { :model => self}
              )
  end  
end

This way, i was able to use the already done partials, to print the html files. 这样,我就可以使用已经完成的部分来打印html文件。 This worked for me, maybe it can help you also. 这对我有用,也许它也可以帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM