Below is an erb ruby code which is want to convert in slim template
<div class="star-rating" data-score= <%= review.rating%> ></div>
In above template i am confused as there are two equals to sign
online converter is giving something like this
.star-rating data-score="<haml_loud" review.rating >
But its not working
This will work for you:
.star-rating data-score=review.rating
Since you're (apparently) using Slim, not Haml, you don't need haml_loud at all.
<%= ... > in Erb means to evaluate the expression inside, and include the result in the outer context. Thus if the rating would be 99, then data-score=99 would become part of the html. That is fine.
The generated output seems wrong. The trailing > should be inside a string, just as the opening counterpart "<haml_loud. And as jeffdill2 correctly pointed out, there is no need to use haml_loud. Just use:
.star-rating data-score=review.rating
Related
I am making a comic book collection app using Middleman.
I am able to get different information from a YAML file and render it in a .erb file.
Now, I created a date using yaml like this:
date: 1977-08-01
I tried looking around the web on how to render it in different format, but I can't find ANYTHING...
Per example, I have a title field, in my yaml document, that goes like this:
title: "Uncanny X-Men"
So I render it in my erb document using this:
<%= comic["title"] %>
But how do I get, let's say, to print the month from my date? Impossible to find any kind of information... I do get how to use different aspects of dates in either ruby or yaml, but not how to render a date FROM yaml to ruby :(
Thanks!
You need to parse your date value in the YAML file into a Ruby date class, first make sure to surround your date values in the YAML file with quotes like so:
date: '1977-08-01'
and then to parse:
<%= Date.parse(comic["date"]) %>
and to get the month you would add .month
like so:
<%= Date.parse(comic["date"]).month %>
that will give you the number 8 if you want more customization to the way your month look like you should look into strftime, if so you wouldn't even need to use .month.
Ideally to DRY things up, you would create a helper in the config.rb
# Methods defined in the helpers block are available in templates
helpers do
def format_date(date_txt)
date = Date.parse(date_txt)
date.strftime("%B")
end
end
And later you can call it inside your template like so:
<%= format_date(comic["date"]) %>
I hope that helps.
First, check the data type with: comic['date'].class
If it's a Date then you can use: strftime to format the date however you want!
If it's not a date let us know and we'll explain the options for conversion
I'd like to process just links written in markdown. I've looked at redcarpet which I'd be ok with using but I really want to support just links and it doesn't look like you can use it that way. So I think I'm going to write a little method using regex but....
assuming I have something like this:
str="here is my thing [hope](http://www.github.com) and after [hxxx](http://www.some.com)"
tmp=str.scan(/\[.*\]\(.*\)/)
or if there is some way I could just gsub in place [hope](http://www.github.com) -> <a href='http://www.github.com'>hope</a>
How would I get an array of the matched phrases? I was thinking once I get an array, I could just do a replace on the original string. Are there better / easier ways of achieving the same result?
I would actually stick with redcarpet. It includes a StripDown render class that will eliminate any markdown markup (essentially, rendering markdown as plain text). You can subclass it to reactivate the link method:
require 'redcarpet'
require 'redcarpet/render_strip'
module Redcarpet
module Render
class LinksOnly < StripDown
def link(link, title, content)
%{#{content}}
end
end
end
end
str="here is my thing [hope](http://www.github.com) and after [hxxx](http://www.some.com)"
md = Redcarpet::Markdown.new(Redcarpet::Render::LinksOnly)
puts md.render(str)
# => here is my thing hope and ...
This has the added benefits of being able to easily implement a few additional tags (say, if you decide you want paragraph tags to be inserted for line breaks).
You could just do a replace.
Match this:
\[([^[]\n]+)\]\(([^()[]\s"'<>]+)\)
Replace with:
\1
In Ruby it should be something like:
str.gsub(/\[([^[]\n]+)\]\(([^()[]\s"'<>]+)\)/, '\1')
I just started checking out sinatra for a project, and I started playing around with HAML.
However, I've run in to an issue -- I have a path with a splat that needs to point to an HAML file with a name the same as the text splatted out of the url, however, any string passed to the [haml] template method is treated as an inline template, and not a filename.
There is no documentation that would suggest there is a way to do this. The only solution I can think of is reading to full text of the necessary template file and passing it to the HAML function; however, such a solution is incredibly cumbersome.
Example
get '/stpl/*.haml' do |page|
haml page # <--- `page' is treated as an inline template
end
Whilst this functionality is expected when one reads the documentation, there is no other means, it would seem, to accomplish what I need.
If you pass a symbol to haml, it will look in views for the matching file, so you can do this instead:
get '/stpl/*.haml' do |page|
haml page.to_sym # attempts to get views/<page>.haml
end
I am using ERB for metaprogramming of some math language. If I could extend ERB functionality to handle %= tags, it would allow me to simplify my sources significantly. I simply want to get output of the line in analogy with <%= %>. I have tried to dig into /usr/lib/ruby/1.9.1/erb.rb file, but got lost very quickly. May be you can help with this problem?
Well, it seems I have managed it by myself. If you save the code at http://pastie.org/1056824 (or http://gist.github.com/487297) as extended_erb.rb and then call it in your script...
require 'extended_erb'
puts ERB.new(File.read('mytemplate.erb'), 0, '%').result
or run ERB from command line...
erb -r extended_erb mytemplate.erb
then the following template...
<%= 1 %>
%= 2
will produce output desired
1
2
For this project, I need to be able to mix Haml tags within the Maruku filter. For example, will I be able to do this:
#contain
:maruku
## Hello H2 Tag
div{:id => 'divinmaruku'}
**Can I do this?**
I know you can just unindent where you want to get out Maruku, but it is a pain to do :maruku whenever I want to use it.
No, you can't do this (at least, not without hacking Maruku so that it calls out to Haml). You can insert literal HTML, or use Maruku's div syntax:
+-----------{#divinmaruku}---
| **You can do this**
+----------------------------
This renders as
<div id='divinmaruku'>
<p><strong>You can do this</strong></p>
</div>