Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How render one section only #330

Open
myoldusername opened this issue Oct 20, 2017 · 7 comments
Open

How render one section only #330

myoldusername opened this issue Oct 20, 2017 · 7 comments

Comments

@myoldusername
Copy link

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
Copy link
Owner

Pass false for the rest of the sections?

@myoldusername
Copy link
Author

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

@bobthecow
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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

@myoldusername
Copy link
Author

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
Copy link

Nullmann commented May 24, 2018

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);       

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants