Embedding Ruby code in HTML? - ruby

In PHP, it is possible to do this:
<input type="text" value=" <?php echo "Hello world"; ?> ">
Is it possible to embed Ruby in HTML like that with or without Rails?

You will need to use a templating engine like Ruby Templates (ERB). Here's an example. Rails uses ERB so you could easily do this in each of your templates.

Rails has something called ERB to do just this sort of thing.

Use HAML for HTML templating, otherwise ERB.

Related

Nokogiri XML Parsing with Ruby Sinatra

I'm having difficulty selecting a URL within an XML document using Nokogiri, have tried using CSS selectors which works fine apart from markup within the child. I think this has to be due to the brackets < being written as &lt and &gt. Is there a way around this?
<br /><strong>URL:</strong> <a href="https://url.com/">https://url.com/</a>
require 'cgi'
CGI.unescapeHTML('<br /><strong>URL:</strong> <a href="https://url.com/">https://url.com/</a>')
gives
<br /><strong>URL:</strong> https://url.com/
Maybe you just need to unescape your html first

Accessing Tag Helper output in Javascript

beginner question with Razor and tag helpers I'm afraid!
Using tag helpers in my razor html, I can e.g. write:
<div><date-picker id="datepicker" value="#DateTime.Now"></date-picker></div>
This will then generate the output
<input id="datepicker" class="datepicker" aria-atomic="true" aria-live="assertive" aria-expanded="false" role="combobox" name="datepicker" placeholder="Select date">
and so on. What I'd like to do is to append the output to other objects like dialog screens, which accept an html string to append.
e.g.
var customDesign = "<div id="something"><date-picker id="datepicker" value="#DateTime.Now"></date-picker></div>";
$(".myDialogfield").after(customDesign);
This doesn't work in Razor - I've tried various things like creating this as a HTML.Raw string first and injecting it as a variable etc - is there a way I can use the output from my tag helper within a script section?
Thanks for any hints!
You can't do it that way. TagHelpers are interpreted. In other words, Razor must see them as actual tags in order to replace them. Here, it's just a JS string, and Razor will not mess with that.
Your best bet would likely to be some sort of JavaScript templating system, but generally speaking you could still get what you want manually via a different path. Instead of hardcoding a string of HTML, include the TagHelper in a script block of type text/html:
<script type="text/html" id="MyTemplate">
<date-picker id="datepicker" value="#DateTime.Now"></date-picker>
</script>
Then, in your JavaScript, you can select this script tag and get its content:
var customDesign = $('#MyTemplate').html();
$(".myDialogfield").after(customDesign);

matching <div></div> tag with regular expression in ruby

Could anyone tell me how can I match the start of <div> tag to the end of </div> tag with a regular expression in Ruby?
For example let say I have a:
<div>
<p>test content</p>
</div>
So far I have this:
< div [^>]* > [^<]*<\/div>
but it doesn't seems to work.
Nokogiri is great but, imho, there are situations when it can not be used.
For your mere case you can use this:
puts str.scan(/<div>(.*)<\/div>/im).flatten.first
<p>test content</p>
To match the <div> when it's all on one line, use:
/<div[^>]*>/
But, that will break on any markup with a new-line inside the tag. It'll also break if there is whitespace between < and div, which there could be.
Eventually, after you've added in all the extra checks for the possible ways a tag can be written you'll want to consider a better way, which would be to use a parser, like Nokogiri, which makes working with HTML and XML much easier.
For instance, since you're trying to tear apart the HTML:
<div>
<p>test content</p>
</div>
it's pretty easy to guess you really want to get to "test content". What if the HTML changed to:
<div><p>test content</p></div>
or worse:
<div
><p>
test
content
</div>
A browser won't care, nor will a good parser, but a regex will get upset and require rework.
require 'nokogiri'
require 'pp'
doc = Nokogiri.HTML(<<EOT)
<div
><p>
test
content
</div>
EOT
pp doc.at('p').text.strip.gsub(/\s+/, ' ')
# => "test content"
That's why we recommend parsers.
An HTML parser such as Nokogiri would probably be a better option than using a Regex as PinnyM pointed out.
Here is a tutorial on the Nokogiri page that describes how to search an HTML/XML document.
This stackoverflow question demonstrates something similar to what you want to accomplish using CSS selectors. Perhaps something like that would work for you.

Do I need to convert regular html code to follow codeigniter standard?

Since I ported my website to codeigniter I am wondering if I really need to convert regular html code to codeigniter coding standard.
Say for example I have the following html code:
<input type="text" id="first_name" name="first_name" size="32" maxlength="32" value="<?php echo $first_name ?>">
In codeigniter I can do the same as above or use the following code:
$format = 'size="32" maxlength="32"';
echo form_input('first_name', '$first_name', $format);
So which one should I follow?
Just do this for ease:
echo '<input type="text" id="first_name" name="first_name" size="32" maxlength="32" value="'.$first_name.'">';
its better and suggestable and also supported by each browser damn sure
Leave your code as it is, there is no standard in writing HTML here, use whatever suits you best and whatever is more readable for you. Your way is much more rapid, than CodeIgniter's as it's basically plain html with echo of one variable. CodeIgniter's helper is basically an option for lazy ones to get things up faster, if you're familiar with them.
If you work in a team and then ask for your designer or the one that's usually working with frontend and he's not familiar with CodeIgniter you will have to change things by yourself.

Alternative to <%= in Ruby On Rails view?

So I was wandering if there is an equivalent to <%= in a Ruby on Rails view using <% and %>.
PHP allows the use of <?=$str ?> and <? echo $str ?>, do views in RoR allow something like this? Doing <%= in a large block of Ruby On Rails code in a view isn't very nice to see, not to mention a nuisance when doing something like for loops. Is it possible?
That isn't Ruby On Rails construction, that's construction of ERB template engine. If you like, you can use haml or any other template engine. This is rather old, but interesting review many of them.
You are talking about *.html.erb files. Try something else. For instance, HAML. http://haml-lang.com/

Resources