General developer forum

Mustache Renderer Javascript/Ajax

Picture of Mathieu Domingo
Re: Mustache Renderer Javascript/Ajax
I think i walk one step closer to the solution : regroup end of part 4.1 with 4.2 to get this :
    M.cfg.wwwroot + '/blocks/course_overview/change_visibility.php',
        //step 2 : do something to re render the template now the course visibility have change (any way to change only the line? how it's done, look like it will change the full block)
        //should look something like that I think, but I have no idea how I'm supposed to get the "new_data" ...
        templates.render('block_course_overview/main', new_data).done(function(html,js){

For test purpose, at the end of step 5,  I added
echo json_encode(array("test"=>"return"));

It's working, the course list go away. Now I need to find how I can fill this array to get the list with new values.

I suppose I need to call the public function export_for_template(renderer_base $output) of file classes/output/main.php but i don't understand how to do it / how it's working..
Is there a way to get an already instantiated object to call this method ? Or should I instantiate a new one ?
What is the renderer_base $output variable ?
Average of ratings: -
Picture of Mathieu Domingo
Re: Mustache Renderer Javascript/Ajax

After becoming crazy many times reading/trying, i finally made another step after looking more carefully the html code...

So, in javascript file, in var data , to get the instanceid of the block, i added this :
block_instanceid :$('[data-block="course_overview"]').data('instanceid')

When i call my php file with ajax, to get this value and then get the instance of this object :
$block_instanceid = required_param('block_instanceid', PARAM_INT);
$blockinstance= block_instance_by_id($block_instanceid);

(it's so easy when it's written but it took me so long to write this 3 lines -_- )

This allow me to call function get_content()

The problem is this function already call a renderer and return me the text already rendered... So i can't give it to  templates.render in my javascript file ..
At this step, if I return it to the javascript, like this :
echo json_encode($blockinstance->get_content());

I can do this in the javascript file :
templates.render('block_course_overview/main',new Object()).done(function(html,js){

It's "working", but it doesn't look like the normal code like in the docs (seems a bit hacky i don't like it)

Looking more closely the code of get_content(), it's seems i need to change just the end of the function to get the return I need.

I tried to make another function and change the end of get_content like this :
$renderer = $this->page->get_renderer('block_course_overview');
$main = new block_course_overview\output\main($config, $tabs, $isediting, $tab, $sortorder, $favourites);
return $main->export_for_template($renderer);

Then I call it like this in my php ajax_script :

$test= $blockinstance->get_content_to_render();

The problem is $test contain all the good values expect all the links , which are all empty..

I tried to look values in process_tab, it's look ok, but if I look in export_for_template it's not.

If anyone have any idea of what happens, thanks to say me because i don't have any :'(

(If it's not clear, I actually updated the github fork )

Average of ratings: -
Picture of Mathieu Domingo
Re: Mustache Renderer Javascript/Ajax

Finally used the "hacky version" .. 

Seems to work nicely smile  (Can change visiblity and favourite course without reloading , and it also work if javascript is deactivated)

Average of ratings: -