accessing variable globally in marko template

拜拜、爱过 提交于 2019-12-04 16:07:15

Looks like you can use the $global property to expose data for all templates.

For example:

router.get('/test', function * () {
  this.type = 'html'
  this.body = marko.load("./views/home.marko")
    .stream({
      color: 'red',
      $global: { 
        currUser: { id: 2, username: 'hansel' }
      }
    })
})

And then these templates:

// home.marko
<include('./header.marko') />
<h1>color is ${data.color}</h1>

// header.marko
<h2>Header</h2>
<p if(out.global.currUser)>
  Logged in as ${out.global.currUser.username}
</p>
<p else>
  Not logged in
</p>

That works.

But obviously you don't want to have to pass $global into every .stream(), so one idea is to store it on the Koa context, let any middleware attach data to it, and then write a helper that passes it into the template for us.

// initialize the object early so other middleware can use it
// and define a helper, this.stream(templatePath, data) that will
// pass $global in for us
router.use(function * (next) {
  this.global = {}
  this.stream = function (path, data) {
    data.$global = this.global
    return marko.load(path).stream(data)
  }
  yield next
})

// here is an example of middleware that might load a current user
// from the database and attach it for all templates to access
router.use(function * (next) {
  this.global.currUser = {
    id: 2,
    username: 'hansel'
  }
  yield next
})

// now in our route we can call the helper we defined,
// and pass any additional data
router.get('/test', function * () {
  this.type = 'html'
  this.body = this.stream('./views/home.marko', {
    color: red
  })
})

That code works with the templates I defined above: ${out.global.currUser} is accessible from header.marko, yet ${data.color} is accessible from home.marko.

I've never used Marko, but I was curious enough to read the docs after seeing your question since I've thought of using it from time to time. I didn't feel like figuring out how <layout-use> works, so I used <include> instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!