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

It is possible to render a different template on runtime? #236

Closed
binarykitchen opened this issue Nov 2, 2013 · 3 comments
Closed

It is possible to render a different template on runtime? #236

binarykitchen opened this issue Nov 2, 2013 · 3 comments

Comments

@binarykitchen
Copy link

My aim is to call showConfirmation() which should change the template. Something like that but it is not working, nothing happens ...

define([
    'views/abstract/form',
    'jade!templates/user/create',
    'jade!templates/user/created',
], function (AbstractFormView, createTemplate, createdTemplate) {

    var UserCreateView = AbstractFormView.extend({
        el:         'main',
        template:   createTemplate(),

        showConfirmation: function() {
            this.template = createdTemplate();
        }
    });

    return UserCreateView;
});

Any clues?

@Rich-Harris
Copy link
Member

Yeah, you can't change templates once an instance has been rendered. You can change partials, however, up until the point that they get rendered, so something like this (contrived example) might work...

<div class='container'>
  {{#showContent}}
    {{>content}}
  {{/showContent}}
</div>
var UserCreateView = AbstractFormView.extend({
  el:         'main',
  template:   mainTemplate(),
  partials: {
    content: before()
  },
  showConfirmation: function() {
    this.set( 'showContent', false ); // teardown 'before'
    this.partials.content = after();  // change partial
    this.set( 'showContent', true );  // re-render, this time with 'after'
  }
});

Not especially pretty, I'll grant you. Am thinking about allowing expression syntax in partials, so you could do something like

<div class='container'>
  {{> ready ? "after" : "before" }}
</div>

Haven't worked out the details yet though.

@binarykitchen
Copy link
Author

I see. I will give this a try for another view. Partials are really very handy here. Many thanks again!

@monsanto
Copy link
Contributor

+1 on dynamic partials. Is there any reason to not just make them another dynamic attribute on the viewmodel? As in {{>item}} would mean 'parse and wire up the string referenced by item', just like {{{item}}} means 'include the string referenced by item without escaping html' ?

Anyway, dynamic partials would be very nice for making SPAs with Ractive.

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