bobthecow / mustache.php

A Mustache implementation in PHP.
http://mustache.github.io/
MIT License
3.23k stars 418 forks source link

How render one section only #330

Open myoldusername opened 6 years ago

myoldusername commented 6 years ago

Dear all Let say i have this html:

{{# section_1 }}
.......
{{/ section_1 }}

{{# section_2 }}
.......
{{/ section_2 }}

{{# section_3 }}
.......
{{/ section_3 }}

how can i chose to render only section_2

bobthecow commented 6 years ago

Pass false for the rest of the sections?

myoldusername commented 6 years ago

you have to sort each section in the page to disable it which may take long time , is there any easy way !

bobthecow commented 6 years ago

I'm not entirely sure what you're asking, and how you would like to do it instead. Can you give me an example?

myoldusername commented 6 years ago

Some time a part of the page has ajax call an i want to call same page but with GET parm referring to the section i want to render.

Well i came up with a solution , the Render now is a function like this :

in HTML i name the section like

{{#ajax_section}}
.............
{{/ajax_section}}
{{# section_1 }}
.......
{{/ section_1 }}

{{# section_2 }}
.......
{{/ section_2 }}

{{# section_3 }}
.......
{{/ section_3 }}

now in the php page i manage some logic . if $section passed then just render this section.

function printTemplate($mustache, $templateName, $context, $section = null)
{
    if (!empty($section))
    {
        $tpl = $mustache->getLoader()->load($templateName);
        preg_match_all("~{{# *$section *}}(.*?){{/ *$section *}}~is", $tpl, $match);
        $mustache->setLoader(new Mustache_Loader_StringLoader);
        echo $mustache->render($match[1][0], $context) ;

    }
    else
    {
        $tpl = $mustache->loadTemplate($templateName);
        echo $tpl->render($context);
    }
}
bobthecow commented 6 years ago

Variables are false by default. Did you try just passing in the section you wanted to render as true?

myoldusername commented 6 years ago

well the problem is :


{{#ajax_section}}
Here there is a lot of Variables , so i have to chose them one by one ! 
e.g {{username}} {{location}}
If i pass Only ajax_section , then all other vars will not rendered !
{{/ajax_section}}
Nullmann commented 6 years ago

Hey, this is probably some gravedigging and you probably don't have this problem anymore, but I just solved it (in a bad way I guess) and this could help people in the future that stumble upon this thread like me.

I also wanted to only update a part of my mustache-tempalte and not the whole thing (because this would fu** up my javascript and I had to rerun it, resulting in an endless loop...).

My solution incorporates the usage of jquery on the resulting html string and then only replacing said data-region:

templates.render('tool_supporter/course_table', data).done(function(html, js) {
        // Get the data-region of the rendered html
    var anchor = $('[data-region="course_filtering"]', $(html));
        // Only render the filtering dropdown of the tables.
        $('[data-region="course_filtering"]').replaceWith(anchor[0].outerHTML);
}).fail(notification.exception);