Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
electerious opened this issue Sep 3, 2014 · 5 comments
Labels

Comments

@electerious
Copy link

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 sintaxi added the bug label Sep 3, 2014
@sintaxi
Copy link
Owner

sintaxi commented Sep 4, 2014

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
Copy link
Author

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
Copy link

jrkCode commented Sep 16, 2017

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
Copy link

amakk commented Dec 14, 2017

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
Copy link

alifr commented Jul 23, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants