Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output slim to variable

I'm using Base64.encode64(val) to convert html to base64. Example:

- val = link_to 'Link', link_path
= Base64.encode64(val)

But how can I get slim markup to variable? Like so:

.class = link_to 'Link', link_path # <- this output with slim div

Or even multiple lines

div
span
.another_div

There is a way by putting slim code into partial and do this:

- var = render 'partial'
= Base64.encode64(var) # Convert into base64

How to do this without partial?

like image 489
Max Paprikas Avatar asked Feb 08 '23 18:02

Max Paprikas


2 Answers

Slim exposes its templating through the Tilt interface, like so:

# Render a template file:
Slim::Template.new("template.slim", options).render(scope)

# Render a string:
Slim::Template.new(options) { "b slim markup" }.render(scope)

Where options is an optional hash of options for slim and scope is the object in which the template code is executed.

So the following:

slim_markup = <<-SLIM
  div
  span
  .another_div
SLIM

# The options hash and scope have been omitted for the sake of simplicity
html_output = Slim::Template.new { slim_markup }.render

Sets the value of html_output to:

<div></div>
<span></span>
<div class="another_div"></div>

But for your example with the url helper link_path, you must provide slim a scope in which the url helpers are available e.g. a controller.

like image 125
wolfemm Avatar answered Feb 11 '23 22:02

wolfemm


This is an old question, but I have wondered about this many times, and I always spend a lot of time researching it.

Using Slim 4 you can use capture directly:

- val = capture
  div
  span
  .another_div

This will put the rendered slim into your variable.

like image 39
donV Avatar answered Feb 12 '23 00:02

donV