I'm working on a project using Middleman. In one of the pages (videos.html.markdown.erb), I'd like to add partials working with both markdown and Middleman helpers.
<h3><%= video.title %> : RĂ©cit de tournage</h3>
<%= partial "partials/shootandlook1" %>
</div>
It works fine except that Markdown is not converting into HTML... :-(
I named my partial _shootandlook1.html.markdown.erb and my page videos.html.markdown.erb.
I really don't understand what I did wrong... Could someone please help me?
The whole source code is here.
Many, many thanks in advance!
This should work fine if you name your page template file videos.html.erb, and name your content partial _shootandlook1.md.
The Markdown file will be processed first, then inserted into the ERB template appropriately.
I usually find that it's best to avoid having multiple template formats in one file, unless the format explicitly supports blocks (like Haml)
Related
I'm quite new to Ruby and ERB and for this case I'm using only Ruby and not rails.
E:\ruby
-app.rb
-plan.html.erb
-check.css
-track.js [js + jquery framework]
Inside app.rb I've the following lines
text = File.open(("final.html"), "w+")
text.puts ERB.new(File.read("plan.html.erb")).result binding
I'm not sure how to call the .js and .css files inside the .html.erb file. Kindly let me know if I've to post the .html.erb file in case that would be helpful to debug further, thanks.
You can include the JavaScript in the .html.erb file in the same way you load the text file. The simplest (code wise) solution is doing something along the lines of this:
plan.html.erb
<script>
<%= File.read('some/file.js') %>
</script>
However if you are expecting a <script src="some/file.js"></script> as result you'll have to create your own helper or use an existing one from some light weight web framework. A simple example might be:
lib/html_helpers.rb
require 'builder'
module HtmlHelpers
def javascript_include_tag(path)
Builder::XmlMarkup.new.script('', src: path)
#=> %{<script src="#{html_escaped_path}"></script>}
end
end
plan.html.erb
<% require 'html_helpers' %>
<% include HtmlHelpers %>
<%= javascript_include_tag('some/file.js') %>
Keep in mind that the first solution doesn't escape any HTML characters. Meaning that if your script contains </script> everything following that tag will be interpreted as HTML.
Note: This is a very strange and unique use case so I apologise in advance if it seems a bit ass-backwards.
I have a haml file content.haml and a coffeescript file main.coffee.
I wish to somehow get the html resulting from rendering content.haml into a variable in the coffeescript/resulting javascript.
The end result should be a javascript file rendered to the browser.
let's say they look like this:
# content.haml
.container
.some_content
blah blah blah
-
# main.coffee
html_content = ???
do_something_with_html_content(html_content)
I know, this sounds ridiculous, 'use templates', 'fetch the HTML via ajax' etc. In this instance however, it's not possible, everything needs to be served via one JS file and I cannot fetch other resources from the server. Weird, I know.
Short of manually reconstructing the haml in the coffeescript file by joining an array of strings like this:
html_content = [
'<div class"container">',
'<div class"some_content">',
'blah blah blah',
'</div>',
'</div>',
]
I'm not sure the best way of doing this.
Another way I though of was to put something like this in the coffee file:
html_content = '###CONTENT###'
Then render the haml to html in ruby, render the coffeescript to js and then replace ###CONTENT### with the rendered html before serving to the client. However the html is a multi-line string so it completely destroys the javascript.
I'm convinced there must be some other nice way of rendering the haml into html in a variable such that it forms valid javascript, but my brain has gone blank.
Perhaps you can try something like this in one of your views:
:javascript
html_content = <%= escape_javascript(render partial: "content")%>
## your own logic follows here....
Wouldn't it be better to use a custom html data attribute and then fetch the content of it in js?
<div data-mycontent="YOUR CONTENT GOES HERE"></div>
And then in coffee, use the dataset attribute / data via jquery, if it is available.
If you set a var via writing the file directly it will render your js file uncacheable, among other drawbacks.
You can do that by using the sprockets gem, like Rails does. You just need to rename your CoffeeScript file to main.coffee.erb and use it as you would e.g. a haml template. Pass in your rendered html with an instance variable:
html_content = '<%= #html_content %>'
Edit: Added missing quotes.
I've tried prepending content_for with =, == or - without luck :)
index.slim
- content_for(:senarios) do
h1 Some content
layout.slim
== yield_content(:senarios)
Hope somebody got a solution.
The example should work fine.
You capture content with content_for and insert it in the layout file with yield_content. You can omit the parentheses if you want.
If you use standard Slim settings you need two equal signs. Otherwise the output will be escaped and you will see <h1>Some content</h1> instead of Some content in the rendered output.
I also use this with Middleman and Slim. No issues. Can you please provide more code, errors, etc.?
The only thing which is looking suspicious is the filename index.slim. It should be index.html.slim.
I met same problem, and solved it.
You should change
- content_for(:eyecatch) do
to
= content_for(:eyecatch) do
nested html attributes should no longer be duplicated.
I'm trying to get fancybox (technically fancybox-rails) working in Rails 3.1. I started with the directions here ... https://github.com/hecticjeff/fancybox-rails and then after quite some time discovered that I needed to add
<%= javascript_include_tag :application %>
to get things working (as an aside, why is the new asset pipeline better than just putting a javascript file in a known directory and using a javascipt_include_tag?). Anyway, now I'm not quite sure what to do. First is the css file for fancybox. Is this included somehow already? Do I have to do something similar to the above for the asset pipeline (I believe it's supposed to handle css files also). Finally, here's what I'd like to have "lightboxed" ...
<% #image_files.each do |image_file_name| %>
<%=link_to(image_tag image_file_name, :class=>"fancybox", :size => "200x200") %>
<% end %>
I have some images (jpg) in a directory pointed to by the image_file_name. These are showing up fine, but I'd like to be able to click on them and get the light box effect. So ... what does my link_to/image_tag need to look like?
Am I missing anything else here?
------- Added information --------
I should not that I do have some javascript for this ...
$(document).ready(function(){
// $("a img.fancybox").fancybox({'type': 'image'});
a#single_image").fancybox({'type': 'image'});
// $("$("a:has(img)").fancybox();
});
trying a few different things here and none seem to do much. I also added the following in the html just to simplify things with the Rails stuff ...
<a class="single_image" href="/assets/card_images/birthday_cake.jpeg"><img src="/assets/card_images/birthday_cake.jpeg" alt=""/></a>
The image shows, but when I click on it, it just goes to a page showing the image. I also checked and it looks like the css is there based on this ...
puts Rails.application.assets['jquery.fancybox.css'].body
which gave me something that started with this ...
/*
* FancyBox - jQuery Plugin
* Simple and fancy lightbox alternative
*
* Examples and documentation at: http://fancybox.net
*
* Copyright (c) 2008 - 2010 Janis Skarnelis
* That said, it is hardly a one-person project. Many people have submitted bugs, code, and offered their advice freely. Their support is greatly appreciated.
*
* Version: 1.3.4 (11/11/2010)
* Requires: jQuery v1.3+
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/
followed by a bunch of css. So I believe it's there.
So ... anyone have anything to try?
If you are using:
<a class="single_image" ....
then your script should be:
$(document).ready(function(){
$("a.single_image").fancybox({'type': 'image'});
});
but if with rails you are setting a different class ("fancybox" for instance) like:
<% #image_files.each do |image_file_name| %>
<%=link_to(image_tag image_file_name, :class=>"fancybox", :size => "200x200") %>
<% end %>
then the js script should match the selector's class:
$(document).ready(function(){
$("a.fancybox").fancybox({'type': 'image'});
});
I'll answer your aside first: "why is the new asset pipeline better than just putting a javascript file in a known directory and using a javascipt_include_tag?"
The Pipeline is parts of the Rails "fast by default" strategy. The old way of doing things - linking each file - resulted in multiple downloads and was slower than having just one file. The old way did not compress (minify) the content of the file, adding to download size (and page slowness).
The Pipeline combines content minification and compression with file concatenation to give you a single file for javascript and CSS for use in production. Added to this, the pipeline uses fingerprints in the filenames that are served. Have a read of the Rails asset pipeline guide, as this has a good explanation of why fingerprinting is useful.
Now on to your specific issue.
Fancybox works by attaching a click handler to the image links, and this drives the popup behaviour.
If you click on an image and it shows you full size image, this means that fancybox has not attached itself to the link.
I can see in your code that you are using the class "single_image" on your links. The Javascript snippet you have posted is expecting the class "fancybox". (as JFK has the other answer)
You'll need to change them to match.
The javascript snippet itself should be in the application.js file.
To get the CSS working for this you'll also need to include the application CSS into your layout:
stylesheet_link_tag "application", :media => "all"
javascript_include_tag "application"
I'm not quite sure how this works yet... trying to find documentation.
In my existing app I've got two different ways of rendering strings in my View
<%: model.something %>
<!-- or -->
<%= model.something %>
The first one is html encoded, and the second one is not.
Is there something similarly short in Razor? All I can find is this, which is the encoded version.
#model.something
I guess the best approach would be to use the Raw extension-method: #Html.Raw(Model.Something)
#Model.Something automatically HTML encodes. If you want to avoid HTML encoding (and you want this only if you are absolutely sure what you are doing) you could use #MvcHtmlString.Create(Model.Something) (basically everything that implements IHtmlString won't be encoded). Phil Haack blogged about the Razor view engine syntax.