I know that you can embed Ruby code in an ERB template. But I have this massive file that has over 200 lines of code. It wouldn't make sense to place every line into the ERB file as then the page would be too big.
I think a better solution would be to require a Ruby file in ERB. Is this possible? If so, how do I do it?
My code itself has a lot of methods defined and I'm not sure they can be added to a view file anyways.
The program is a game that will be played on the home page.
In Sinatra, the following code
haml :index
Will render the file index.html.haml or index.haml
How can I specify file format? I been reading Sinatra README and trying different combinations but I can't see how to do it.
I need to be able to specify a haml template by giving its format too so I can do something like index.js.haml.
Found this while answering another question. Sinatra-Respond_to. May be this is what you're looking for.
As #Kashyap points out
haml :'index.js'
That would do the trick.
Apologies for the length of this question, it's more of a "is this possible" than "how do I do this".
My objective is to remove everything but plain text from Wikipedia markup -- tables, templates, formatting. Whether these are in wikitext markup (e.g. ''bold text'') or HTML (<b>bold text</b>).
Wikipedia text is a mix of custom tags: templates {{ ... }}, tables {| ... |}, links [[ ... ]] and HTML elements. Parsing it is kind of a nightmare. You can't use regular expressions because the tags can be nested, and it can contain HTML so almost anything is possible. Some of the text within the HTML I'd want to keep (stuff within bold text), but other things like tables would need to be stripped entirely.
I thought about re-purposing an XML parser like Nokogiri, adding {{/}} as alternatives to <x>/</x>.
Does anyone who knows Nokogiri (or another Ruby XML parser) know if this is possible or even a good idea?
My alternative is to repurpose an existing parser like WikiCloth for the wiki markup, and then try to remove any leftover HTML via another method.
This sounds like a good idea. However, it would not be possible for you to 'patch' Nokogiri, "adding {{/}} as alternatives to <x>/</x>". This is because the bulk of the work done by Nokogiri—parsing and XPath and generating the string representation of a DOM—is actually done by libxml2 in the back end. You'd have to patch and recompile libxml2 (and then rebuild Nokogiri against your new version)…but at that point I have no idea how Nokogiri would behave.
You might have better luck trying to patch REXML, since that is written in pure Ruby.
Question: Instead of getting transformed output from my ERB template, is there a parameter, setting or hack I can use that will output the raw ruby code that gets generated right before the transformation runs?
Rationale: I am having trouble figuring out the problem with an ERB template syntax error, and I would like to see the plain ruby code that gets generated by ERB. If anyone else has any alternative methods for debugging and tracking down specific problems in ERB, please chime in.
TIA
You can get info about the objects you're using with debug(#object). That may help to see exactly what you're working with and what you can do with it.
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. However I don't want to use the whole Rails stack to do this as it seems like overkill.
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? An existing filter or command that I'm missing?
It's best to combine haml & sass with a tool for building static websites. Here's some links:
StaticMatic
Serve
Haml Jekyll -- a fork of Jekyll that supports haml.
Middleman
I'm using jekyll for my blog, but if you're not building a blog it's probably not appropriate for your needs.
Darn, you're right – there isn't a built-in way. I've used helpers with the haml command line, but always ones whose output was already HTML formatted.
My best suggestion is to write a partial() method and require it. 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.
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.
If someone does get some code working, please put it up on GitHub and make a comment here.
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
/views/file.haml
%h3 Title
%div= haml :content, :layout => false
/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. 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
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.
Once i have the views partitioned in several haml partials, i wanted to reuse this to generate the html files.
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.
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:
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.
This worked for me, maybe it can help you also.