简体   繁体   中英

rails page preview haml and html

I have a page like this,have a select_tag used to select the engine is nil or haml ,if the selected nil , the body_preview value is $('#mail_body').val() ,direct display the html code. if the selected 'haml', write haml code in the mail_body,and the body_preview value is convert $(#mail_body.val()) to html ,convert the haml code only use the static methods, not use ruby dynamic value .

<%= select_tag "engine", "<option></option><option>haml</option>",
                   :onchange => "$('#body_preview').trigger('load');"%>

<%= text_area_tag 'mail_body', nil,
                   :onchange => "$('#body_preview').trigger('load');" %>

<iframe id="body_preview"  
      onload="$('#engine').val() == 'haml' ? "???????" : $('#mail_body').val()">
</iframe>

when the engine is nil

mail_body

#content
  .left.column
    %h2 Welcome to our site!
    %p 2

body_preview

#content .left.column %h2 Welcome to our site! %p 2

when the engine is haml mail_body

#content
  .left.column
    %h2 Welcome to our site!
    %p 2

i hope the body_preview is

<div id="content">
  <div class="left column">
    <h2>Welcome to our site!</h2>
    <p>2</p>
  </div>
</div>

You can use the haml engine to process haml.

template = File.read('templates/really_cool_template.haml')
haml_engine = Haml::Engine.new(template)
output = haml_engine.render
puts output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM