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

arrays and front-matter variables #57

Open
thedamon opened this issue Jul 2, 2015 · 9 comments
Open

arrays and front-matter variables #57

thedamon opened this issue Jul 2, 2015 · 9 comments

Comments

@thedamon
Copy link

thedamon commented Jul 2, 2015

I have this set up so that I can call strings with the i18n helper, but I need to figure out how to handle front-matter variables on a page, and iterating over arrays of data.

Is there a standard/assumed way of doing this?

I want to achieve something like the following:

en:
  title: Bananas
fr:
  title: Les bananes

---
{{title}}

This of course overwrites the en/fr variable so all the other keys are lost and the build fails. My assumption is I need to set these translations in the i18n\en.yml etc?

Then how do I handle, say, an array in my data using {{each}}? I can't pass a helper to a helper


fruits:
-
  name: apple
  color: red or green
  size: medium
-
  name: banana
  color: yellow
  size: medium
-
  name: blueberry
  color: blue
  size: small
{{#each fruits}}
  {{name}}
{{/each}}

Is the key to do fruits: <%= i18n.fruits %> in the front-matter? that doesn't seem to be working...

@LaurentGoderre
Copy link
Contributor

I think you need to do fruits: <%= i18n[language].fruits %>.

@eikawata
Copy link

That also does not work.

In my case, I have an array of objects stored in an external file as follows.

books.yml

books:
  - book:
      - title: Book Foo
      - description: foo foo
  - book:
      - title: Book Bar
      - description: bar bar

I want to do {{#each}} on the array inside Handlebars template, so I did

---
books: <%= i18n[language].books %>
---

{{#each books}}
  <h2>{{ book.title }}</h2>
  <p>{{ book.description }}</p>
{{/each}}

This #each helper just outputs an empty block, without even rendering <h2> or <p>. But I noticed that if I do {{#each en.books }} ... {{/each}} (without using YFM), it works fine. Isn't this a bug?

By the way, having to define a variable in YFM for each array I want to use is really cumbersome. It'd be nice if we could do something like just i18n.books.

@LaurentGoderre
Copy link
Contributor

The issue is that sometimes you need a string in a specific language, not necessarily the language of the page, that's why it is the way it is but we could have another variables to point to the current language object. Any idea?

@jonschlinkert
Copy link
Member

@LaurentGoderre out of curiosity, in your projects, is there something you can point me to that shows how the languages created/formatted or otherwise converted to data so they can be used in templates as context?

@LaurentGoderre
Copy link
Contributor

@LaurentGoderre
Copy link
Contributor

Although I would much prefer to handle this through Transifex

@jonschlinkert
Copy link
Member

great, thanks for the info. I'm going to look into it a bit, hopefully I can make some of this easier in assemble.

@LaurentGoderre
Copy link
Contributor

The biggest issue I saw with this and assemble is when using the function like navigation. I had to preprocess the front-matter for all files to make sure the i18n substitution was happening other wise I would get this for the first file

Link 1
i18n(link2)
i18n(link2)
``
And this for the second file:

Link 1
Link 2
i18n(link2)
``

And so on

@jonschlinkert
Copy link
Member

yeah, I think that would be expected with front matter in general - since that data won't be on the context until it's actually parsed. with 0.6 this is a lot easier, and we have several options to ensure you don't have that issue anymore. let me know if you want to review sometime

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

No branches or pull requests

4 participants