erb

How to create new binding and assign instance variables to it for availability in ERB?

大兔子大兔子 提交于 2019-12-23 19:20:24
问题 I'm implementing HTML templating in a Ruby project (non-rails). To do this I'll be using ERB, but I have some concerns about the binding stuff. First out, this is the method I got so far: def self.template(template, data) template = File.read("#{ENV.root}/app/templates/#{template}.html.erb") template_binding = binding.clone data.each do |k, v| template_binding.local_variable_set(k, v) end ERB.new(template).result(template_binding) end To call it I'll just do Email.template('email/hello', {

How to trim leading whitespace with `<%=` in ERB templates in Rails that end up in `pre` elements?

自古美人都是妖i 提交于 2019-12-23 18:34:24
问题 (As far as I have researched here, this is not a duplicate question. Trimming spaces -- often trailing newlines -- is being discussed for <%- or -%> , but not for <%= . It could be a minor defect in Erubi template engine as well, the one being used by Rails for ERB templates.) I want to render / syntax-highlight code in a view, and my ERB view template contains: <p> <strong>Code:</strong> <pre class="highlight github"> <%= highlight(@code.code, @code.language) %> </pre> </p> The result is

Possible to embed markdown within erb?

↘锁芯ラ 提交于 2019-12-23 18:11:59
问题 If you use haml as rails view template, you can write portion of your page using markdown by using the ":markdown" filter. Is is possible to do the same using erb? 回答1: ERB does not have filtering like this built-in. You'll need to directly use a gem that handles it, like RDiscount or the venerable BlueCloth. 回答2: It's pretty easy to write a method that does it, assuming you're using something like Rails which has #capture , #concat , and #markdown helpers. Here's an example, using Maruku:

What does <%=h … %> means in Rails?

我只是一个虾纸丫 提交于 2019-12-23 09:30:14
问题 I found here the following syntax: <%=h @person.first_name %> What does the h means ? 回答1: It's for escaping the output of the tag to avoid cross-site-scripting. In rails 3, it's been changed to the default for a string (so rather than saying escape this string, you say, this is a safe string). http://api.rubyonrails.org/classes/ERB/Util.html#method-c-h 回答2: h is alias for html_escape method in Rails. If you do not escape the text using h , then someone can write javascript there and it will

Calling ERB without Rails: undefined method 'raw'

筅森魡賤 提交于 2019-12-23 09:22:53
问题 I am using the ERB engine to generate an offline HTML version of a page of my Rails website. The page shows great when shown by Rails, but I have trouble generating with ERB by myself (despite using the same ERB template). First I was getting the error undefined method 't' and I solved it by replacing all <%=t(...)%> calls with <%=I18n.translate(...)%> . Now I get undefined method 'raw' . Should I replace all <%=raw(...)%> calls with something else? If yes, what? 回答1: raw is defined as helper

How does one comment in an erb template?

对着背影说爱祢 提交于 2019-12-23 07:21:27
问题 I have some trivial markup that looks like the following: <li class="someclass"> <=% t'model.attr' %> </li> Is there a trivial way to comment that out? Just wrapping <!-- --> around the block will still leave the ruby code available to the template. This means I have to comment out the HTML and Ruby specific code separately. What's the best way to comment out all three lines with the least amount of markup? 回答1: =begin and =end are the Ruby version of block comments. Using them in an erb

How does one comment in an erb template?

孤者浪人 提交于 2019-12-23 07:21:11
问题 I have some trivial markup that looks like the following: <li class="someclass"> <=% t'model.attr' %> </li> Is there a trivial way to comment that out? Just wrapping <!-- --> around the block will still leave the ruby code available to the template. This means I have to comment out the HTML and Ruby specific code separately. What's the best way to comment out all three lines with the least amount of markup? 回答1: =begin and =end are the Ruby version of block comments. Using them in an erb

What is the scope of Ruby code in a js.erb file and how can it access controller variables

人走茶凉 提交于 2019-12-23 04:28:26
问题 I have a js.erb file in which I want to use asset_path. It is included in a view like this: <%= javascript_include_tag "resources/gallery_resource" %> This works fine while the asset path is self contained, however I need to use a variable defined in the controller (@resource) inside the asset path: element.image = "<%= asset_path 'resources/galleries/'+@resource.uid+'/'+index+'.jpg' %>" The problem being @resource is not defined. So what is the scope of this Ruby code if it doesn't have

Use Nokogiri to replace <img src /> tags with <%= image_tag %>?

不羁岁月 提交于 2019-12-22 11:28:02
问题 How can I use nokogiri to replace all img tags with image tags? This is to utilize Rails' ability to plugin the correct asset server automatically? require 'nokogiri' class ToImageTag def self.convert Dir.glob("app/views/**/*").each do |filename| doc = Nokogiri::HTML(File.open(filename)) doc.xpath("//img").each |img_tags| # grab the src and all the attributes and move them to ERB end # rewrite the file end rescue => err puts "Exception: #{err}" end end 回答1: Somewhat inspired by maerics'

Is there a simple way to use erb files directly like PHP files with NGINX?

萝らか妹 提交于 2019-12-22 08:52:25
问题 Is there a way to simply send a erb file to a ruby parser get the answer back and send it to client with NGINX? Without all the passenger stuff? It should be easy i guess. I DON'T want to use any rails stuff, don't tell me i should use rails etc. 回答1: I created a script to do this two years ago, called ruby-cgi, in response to a similar question. I believe it does exacty what you want. Just set it up the same way you would set up other CGI/FastCGI handlers. 回答2: The following gems are