I want to render an HTML-EMail and send it to our customers using some ERB Templates.
The basic code I am using:
ERB.new("newsletter.html.erb").result(binding)
doesn't allow me to add partials to the html.erb-File. I would love to move the header and footer to a partial and use the render :partial-Method in that call.
Is this possible? What do I have to add?
This is what I came up with:
viewer = ActionView::Base.new(File.join(Rails::Configuration.new.view_path, "PATH/TO/PARTIALS"))
html = viewer.render(
:file => "PATH/TO/FILE.ERB),
:locals => {:variable => #var}
)
please correct me if there is a more elegant solution than this.
Related
I am wondering how can i render in my header file a search model. Pretty much like an include in php, but with rails i want to render the new form into my header. I base my style of search on this video http://railscasts.com/episodes/111-advanced-search-form-revised but not to sure on how to render the code in to my header. Any help is appreciated
How about a partial?
Documentation
You can do something like this:
<%= render "shared/search", :locals => { :search_string => #search_string } %>
Which will render what is in the app/views/shared/_search.html.erb file.
How do I render a partial without having to supply the "_" in front of the file name? Is there a parameter I can call to not use it?
This problem popped up using RABL and Backbone - using RABL requires me to have a file in my views like "index.json.rabl". But, when I use embed the JSON right on the page load (as is usual with Backbone), I'm required to call the file "_index.json.rabl". These 2 files are the exact same thing, just required to have different names. I'm looking to use just 1 file, "index.json.rabl" and force the render() function to look for that file name, without the "_".
=> EDIT
The standard solutions that people have described below don't work. It's likely a RABL issue then? The below code always goes to the views/countries/_index.json.rabl file.
In my .erb file
countryList.reset(<%=get_json("countries", "index", #countries)%>);
In my application_helper.rb file
def get_json(view_path, view_action, object)
path = view_path + '/' + view_action + ".json"
return raw(render(path, object: object, :formats => [:rabl]))
end
You can render a file by doing the following:
render :file => "filename"
From the RailsCast #322 on RABL:
<div id="articles" data-articles="<%= render(template: "articles/index.json.rabl") %>" >
Start from here, and then figure out what's wrong. But it's clear that render template: path is the syntax you want.
did you try render :template => "file_name" ?
Ok try:
<%= render :file => 'views_directory/index' %>
Where views_directory is the name of your directory in the views 8)
OLD:
If the content is the same use:
render :partial => "index"
in index.json.rabl and the content in _index.json.rabl
I have a partial that contains javascript
#shared/monkey.js
:javascript
//javascript code here
and I want to be able to use partial in that
render :partial => 'shared/monkey', :locals => {:monkey => 'HELLO'}
how do i use the variable monkey inside my partial?
actually, found out a way.
in order to execute ruby code in your :javascript block, you need to do
"#{ruby_code}"
I am using stache for server-side evaluation of Mustache templates. I would like to re-use some of these templates on the client-side from JavaScript using ICanHaz.js, but to do so I need to include them into script tags. I would like to avoid duplicating the templates (DRY), but obviously, the templates must not be evaluated before being sent to the client, so using a simple render :partial invocation like in this (HAML) snippet does not work:
%script{:id => 'project_snippet'}
= render :partial => 'project'
Is there any way to include a partial without evaluating it using the underlying template engine (kind of like a raw include)?
In other places the partial is to be used as regular partial, i.e., evaluation is supposed to happen, so changing the file extension to always avoid evaluation is not an option.
do you need a partial as is or you want it to be rendered as HTML with some placeholders for JavaScript templating? you can pass :locals => { ... } with something to be replaced by JS template engine later i.e.
%script{:id => 'project_snippet'}
= render :partial => 'project', :locals => {:name => '{{{ project_name }}}'}
if as is then read the partial content (but it doesn't look like you want this)
%script{:id => 'project_snippet'}
= File.open("#{path/to}/partial.html.haml", "r").read
Well, it seems that I should have read the stache documentation: There is a tag helper available, so
= template_include_tag 'projects/project'
will do the trick after setting the template base directory in an initializer:
Stache.configure do |c|
c.template_base_path = "#{Rails.root}/app/views"
end
I have a file in:
RAILS_ROOT/public/system/pages
It is a snippet of HTML. I would like to render it, along with other things, in one of my views.
But it seems like when I try to do a render from a view, Rails is always looking for a partial. But it doesn't pick up the file even when I name it with a leading underscore. How can I read and display this HTML snippet within a view?
have you tried with
<%= render :file => 'your/path/', :layout => false %>
inside the erb?