Question

If a user is not logged in, I want to render a SignIn template for him while preserving the URL of the page. When he does log-in, then I want to continue rendering of the URL he original tried to visit.

The following bit of CoffeeScript shows the intent of what I am trying to accomplish. A version close to this worked until recently, until I updated to Meteor 0.8:

# If the user is not signed in, render the signin template instead of the
# actual page. However, keep the URL the same.

mustBeSignedIn = (pause) ->
    if not Meteor.user()?
        # How to render a different template from the intended one here?
        ### vvv ----
        @render('masterLayout'
            yieldTemplates:
                signIn:
                    to: 'content'
        )
        pause()

Router.onBeforeAction(mustBeSignedIn, {
    except: [ 'signIn' ]
})

My layoutTemplate contains a yield region with the name "content":

<template name="masterLayout">
  <div id="container-content">
     {{> yield region="content"}}
  </div>
</template>

The signingIn and signIn templates are defined and other paths render properly when I go to them directly.

Was it helpful?

Solution 2

The correct syntax for yield-ing to templates is described in the DOCS.md file:

You can render manually by calling the render function. There are three ways to call the render method:

  • this.render(): Render all of the templates for the Route or RouteController. This renders the main template into the main yield region, and all of the yieldTemplates into their associated {{> yield region='name'}} regions.
  • this.render('templateName'): Render the template named 'templateName' into the main yield {{> yield}}.
  • this.render('templateName', {to: 'region'}): Render the template named 'templateName' into the region named 'region' {{> yield region='region'}}.

The third point answers the question about how to render a template to a named region.

OTHER TIPS

I think you just need to return the result of the render function.

mustBeSignedIn = (pause) ->
    if not Meteor.user()?

        return @render('masterLayout'
            yieldTemplates:
                signIn:
                    to: 'content'
        )

Also its good practice to favor Meteor.userId() over Meteor.user() since userId() is only reactive if the userId changes where as user() will cause your onBeforeAction to be run any time there is a change to the current users record.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top