0

I am working on a project using sinatra with haml for templates. I want to be able to append some of my haml partials using JQuery. I am hoping to add this to a js function being triggered by an event like so

....addEventListener(myWidget, 'click', function() {
    $('targetDiv').append(RenderHamlTempalte('/my/file/location/');
});

or

....addEventListener(myWidget, 'click', function() {
    $('targetDiv').append(RenderHamlTempalte('/my/file/location/', {options: here, options: here);
});

I have checked out a few related projects on github but really can't find something to do this with properly.

thanks for any info

UPDATE:

here was what I ended up going with

var url = '/update&my&div/' + param;
$('.my-div').load(url);

and then in my sinatra app.rb I can just return my haml partial

get ''/update&my&div/:params' do
    # do some stuff
    @make_variables_happen
    haml :my_partial
end

1 Answer 1

1

One way to do this is to use a tool like: https://github.com/uglyog/clientside-haml-js You would have to wrap your partials in <script type="text/haml-template" id="template_id"> YOUR PARTIAL GOES HERE </script> and append them somewhere in your html page (I usually add them at the bottom of the page). You can then reference them from JavaScript with:

 var tmpl = haml.compileHaml('template_id');

and then you can pass the data with:

var html = tmpl({option1: value1, option1: value1});

or based on your example:

$('targetDiv').append(tmpl({option1: value1, option1: value1}));
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the response. I ended up going a slightly different route because having to include my haml partials in the same file wasn't ideal. I edited my first post to show how if anyone is interested. thanks again for the tip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.