2017-04-12 124 views
0

我是新來的Keystone,但一直試圖找到當前登錄的用戶名,但我不清楚如何做到這一點。keystone.js當前登錄用戶

如果我走索引視圖從梯形例如

{{!< default}} 
<div class="container"> 
    <div class="jumbotron"><img src="/images/logo.svg" width="160"> 
     <h1>Welcome</h1> 
     <p>This is your new <a href='http://keystonejs.com' target='_blank'>KeystoneJS</a> website.</p> 
     <p> 
      It includes the latest versions of 
      <a href='http://getbootstrap.com/' target='_blank'>Bootstrap</a> 
      and <a href='http://www.jquery.com/' target='_blank'>jQuery</a>. 
     </p> 
     <p>Visit the <a href='http://keystonejs.com/guide' target='_blank'>Getting Started</a> guide to learn how to customise it.</p> 
     <hr> 
     <p>We have created a default Admin user for you with the email <strong>[email protected]</strong> and the password <strong>admin</strong>.</p> 
     <p><a href="/keystone/signin" style="margin-right: 10px" class="btn btn-lg btn-primary">Sign in</a> to use the Admin UI.</p> 
     <hr> 
     <p> 
      Remember to <a href='https://github.com/keystonejs/keystone' target='_blank'>Star KeystoneJS on GitHub</a> and 
      <a href='https://twitter.com/keystonejs' target='_blank'>follow @keystonejs</a> on twitter for updates. 
     </p> 
    </div> 
</div> 

,我想用戶名添加到視圖代碼財產以後像

<h1>Welcome Tim</h1> 

我已經試過

<h1>Welcome {{locals.user}}</h1> 
<h1>Welcome {{locals.user.name}}</h1> 
<h1>Welcome {{req.user}}</h1> (using an Express request) 

但無濟於事。如何從用戶模型中找到用戶名?我必須首先在.. \ routes \ views中定義其他內容嗎?

如果有人可以幫助一個例子或指針在正確的方向,我真的很感激!

回答

3

如果使用了Yeoman生成器,則包含的一箇中間件函數(initLocals)會將當前用戶設置爲局部變量。

res.locals.user = req.user; 

https://github.com/keystonejs/generator-keystone/blob/master/app/templates/routes/_middleware.js#L27

你並不需要包括locals在把手變量名的前面。所以刪除它以獲取當前用戶信息。

{{user}} 
{{user.name}} 
+0

你先生,太棒了。在仔細閱讀中間件代碼後,我想我現在明白了。 – masterofimps