meteor - Distinct layouts for distinct user types -
i have 2 distinct usertypes 'reader' , 'publisher'. information stored in meteor.users document meteor.users.usertype: 'publisher'
or can meteor.users.usertype:'reader'
how can dynamically add elements appbody depending on usertype?
the data context , things menu items of course different usertypes
i have referenced https://github.com/eventedmind/iron-dynamic-template
html:
<head> <title>site</title> </head> <template name="appbody"> //this layouttemplate <div class="navbar navbar-fixed-top navbar-custom"> <div class = "container"> <ul class="nav navbar-nav navbar-right"> <li><a href="#">menu title a</a></li> <li><a href="#">menu title b</a></li> ***dynamically add list element depending on 'usertype' logged in here*** </ul> </div> </div> {{> ui.dynamic template=currentuser.usertype }} </template> <template name="reader"> unique layout {{> yield}} </template> <template name="publisher"> unique layout {{> yield}} </template>
router.js
router.configure({ layouttemplate: 'appbody', loadingtemplate: 'apploading' });
if want keep using ui.dynamic
, suggest writing own template selector function:
paraphrasing https://github.com/eventedmind/iron-dynamic-template, needs this:
if (meteor.isclient) { ui.body.helpers({ gettemplate: function () { return something; } }); }
i suggest renaming helper usertype , using follows:
if (meteor.isclient) { ui.body.helpers({ usertype: function () { var user = meteor.user(); return (user && user.usertype)? user.usertype : ''; } }); }
and using {{> ui.dynamic template=usertype }}
you can change '' in return
statement 'anonymous' if fails users not logged in, need write anonymous user type template.
however, not need ui.dynamic achieve this:
you use meteor if template helper this:
{{#if usertypeis "publisher"}} {{ > publisher }} {{/if}} {{#if usertypeis "reader"}} {{ > reader }} {{/if}}
this won't work itself.
next, need include helper function in js template.appbody.usertypeis
to locally, include them template.appbody.helpers()
template.appbody.helpers({ ....other helpers.... usertypeis: function(t){ var user = meteor.user(); return ( (user) && (user.usertype) && (user.usertype === t) ); }
usertypeis
might useful other templates, if desired instead defined globally templates using template.registerhelper()
:
template.registerhelper('usertypeis', function(t){ var user = meteor.user(); return ( (user) && (user.usertype) && (user.usertype === t) ); }
notice function returns boolean, used if, , takes parameter, quoted in template. execution uses "defensive programming" in return false if reason meteor.user() or meteor.user().usertype not defined.
this templating standard in meteor, no need import additional modules project particular task.
also, of in meteor api docs, encourage read in entirety if have not done so.
Comments
Post a Comment