gpt4 book ai didi

jquery - 根据用户角色隐藏带有 Handlebars 的元素

转载 作者:太空宇宙 更新时间:2023-11-03 21:49:22 24 4
gpt4 key购买 nike

目前我的项目中有两个可能的用户,即雇主和候选人。

var User = new Schema({
role:String
});

我的 Handlebars 页面上有某些元素,我想根据登录用户隐藏这些元素。这些元素只是字体精美的图标和按钮。有没有办法在助手或其他东西的帮助下做到这一点?或者也许是 jQuery,我不确定,我使用 Passport.js 作为我的登录系统。

最佳答案

猜测您渲染页面并注入(inject)“user”变量,这真的很容易做到。

如果该语句为 true,则可以使用 if 语句来呈现代码块

I found this link that explains it really well.

<小时/>

第一个选项

Here's a complete answer that should help you do what you want to do.(这是它所说的:)。

The easiest thing would be to add a custom if_eq helper:

Handlebars.registerHelper('if_eq', function(a, b, opts) {
if(a == b) // Or === depending on your needs
return opts.fn(this);
else
return opts.inverse(this); }); and then adjust your template:

{{#if_eq this "some message"}}
... {{else}}
... {{/if_eq}}

Demo: http://jsfiddle.net/ambiguous/d4adQ/

If your errors entries weren't simple strings then you could add "is this some message" flags to them and use a standard {{#if}} (note that adding a property directly to a string won't work that well):

for(var i = 0; i < errors.length; ++i) errors[i] = { msg: errors[i], is_status: errors[i] === 'some message' }; and:

{{#if is_status}}
<li>Status</li> {{else}}
<li>{{msg}}</li> {{/if}}

Demo: http://jsfiddle.net/ambiguous/9sFm7/

<小时/>

第二个选项

使用 Elving 的 Swag Handlebars helpers library ,您可以使用帮助器 isisnt点击文档链接

两个例子

Conditionally render a block if the condition is true.

Parameters:

value [string|int] - the value to test against.

Usage:

number = 5

{{#is number 5}}
Kiss my shiny metal ass!
{{else}}
Never mind :(
{{/is}}

=> Kiss my shiny metal ass!

不是

Conditionally render a block if the condition is false. Opposite of is.

Parameters:

value [string|int] - the value to test against.

Usage:

number = 5

{{#isnt number 5}}
Kiss my shiny metal ass!
{{else}}
Never mind :(
{{/isnt}}

=> Never mind :(

关于jquery - 根据用户角色隐藏带有 Handlebars 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43250419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com