leafo / lapis

A web framework for Lua and OpenResty written in MoonScript
http://leafo.net/lapis/
MIT License
3.14k stars 247 forks source link

/usr/local/share/lua/5.1/lapis/html.lua:297: attempt to index local 'w' (a function value) #700

Closed ghoomfrog closed 4 years ago

ghoomfrog commented 4 years ago
-- app.moon
export require, Widget
require = require'require'.require
lapis = require 'lapis'

import Widget from require 'lapis.html'

class extends lapis.Application
  layout: 'layout' --> Error
  '/': =>
    render: 'index'
-- views/layout.moon
class Layout extends Widget
  content: =>
    html_5 ->
      head -> title @title or 'TinyMoon'
      body ->
        widget NavBar --> This is THE line making the error. Oops, totally forgot NavBar is a function.
        @content_for 'inner'
-- views/navbar.moon
class NavBar extends Widget
  content: =>
    nav id: 'navbar', ->
      a href: '/', 'Home'
      span '•'
      a href: '/help', 'Help'

In #699 I passed the widget's name rather than a function.

p.s. When I passed NavBar! instead, the error was the same as in #699.

leafo commented 4 years ago

you have no reference to NavBar in the layout, you need to require it.

ghoomfrog commented 4 years ago

@leafo import NavBar from require 'views.navbar' didn't fix it.

leafo commented 4 years ago

@leafo import NavBar from require 'views.navbar' didn't fix it.

I think you should spend some more time looking into how require works, and what import does. At least try some debug print statements to get a better understanding.

Your view file returns a class object, not a table containing a class. import NavBar from require 'views.navbar' is equivalent to NavBar = (require"views.navbar").NavBar. This would assign nil because there is no NavBar field on the object (the class) that is returned by that module.

Instead you should write: NavBar = require "views.navbar"

ghoomfrog commented 4 years ago

@leafo I freaking forgot. The error is the same as in #699 now.

ghoomfrog commented 4 years ago

See #699 for the solution.