sintaxi / harp

Static Web Server/Generator/Bundler
http://harpjs.com
5k stars 343 forks source link

_data.json not available in _layout.ejs (only when using harp compile) #333

Open electerious opened 10 years ago

electerious commented 10 years ago

Relevant files

_data.json:

{
  "index": {
    "title": "Home"
  }
}

_layout.ejs:

<%- yield %>
<%= title %>

index.ejs:

Hello World

The problem

The construction above works when using harp server. The content of the site is:

Hello World
Home

However, the same constellation throws an error when compiling the code with harp compile:

{
  "source": "EJS",
  "dest": "HTML",
  "filename": "/Users/tobiasreich/Sites/Templates/Harp/public/_layout.ejs",
  "lineno": 2,
  "name": "ReferenceError",
  "message": "title is not defined",
  "stack": "<%- yield %>\n<%= title %>"
}

Provisional solution

It works when I define the title global in the _harp.json:

{
  "globals": {
    "title": ""
}

The content of title will be overwritten by the title in _data.json. It's the same like shown in this example: https://gist.github.com/kennethormandy/6834709

The content of the page is now corrent and also works with harp compile.

sintaxi commented 10 years ago

Thanks for the thorough report.

I think what is happening is title is unavailable to the _layout.ejs when attempting to render soeme file other than index.{ejs|jade|md} and that is what is throwing the error. That is why the fallback in globals fixes the issue.

Do you have any other templates in your project that don't set title? In this specific circumstance the fallback is a good idea or in your _layout file you can put a fallback there

<%= locals.title || "got your back" %>

hope this helps.

electerious commented 10 years ago

Thanks for the response!

I think what is happening is title is unavailable to the _layout.ejs when attempting to render soeme file other than index.{ejs|jade|md}

There's only a index.ejs at the moment, so Harp shouldn't try to render other files.

Do you have any other templates in your project that don't set title?

My public/ folder only contains: _layout.ejs, _data.json and the index.ejs

jrkCode commented 7 years ago

I recently started using harp and run into this problem and the cause of it was when I add README.md file in the root directory. Let me know if this solution works for you.

amakk commented 6 years ago

I am running into this issue as well. I was fighting with it for a while. The solution was to delete the node_modules folder. I have harp installed globally, and wasn't using any locally installed packages until now. My website is in the root folder of the project (not /public).

alifr commented 6 years ago

I found that removing the 404.jade file from my /public directory also fixed this issue. I did not have any matching objects in my _data.json file to match "404" so it seems to be erroring out.

A more detailed explanation is I had several pages in my /public directory including a _data.json file and a _layout.ejs file and the boilerplate 404.jade file. My _layout.ejs file was using variables in the _data.json file. In the _data.json file I had a data object for every page except 404.jade. So I think harp compile was crashing when trying to apply my _layout.ejs to the 404.jade file.