picocms / Pico

Pico is a stupidly simple, blazing fast, flat file CMS.
http://picocms.org/
MIT License
3.83k stars 617 forks source link

Is it possible to get meta (yaml) data form an external file #411

Closed omniperspective closed 5 years ago

omniperspective commented 6 years ago

I want to get the variable metadata (YAML) from a external file.

To get the content I do this:

{{ "personalia/#{meta.language}/#{meta.medewerker}"|content }}

But how do I get an external yaml data (meta.xyz) I only get 500 errors.

Please help, it drives me nuts(!) Thanks in advance for your help Henk.

PhrozenByte commented 6 years ago

I'm not sure what you mean by "external" YAML data. Do you want to access the meta headers of a arbitrary page? Try e.g. pages["sub/page"].meta. Just to be sure, you're trying to do this in a Twig template (i.e. a .twig file, not .md), right? What error message shows up in your webserver's error.log? If you remove the lines in question, does the website work? The syntax seems to be correct, but are you sure that the page you're trying to include actually exists? Double check this by printing out the page ID (i.e. {{ "personalia/#{meta.language}/#{meta.medewerker}" }}).

omniperspective commented 6 years ago

I will explain a bit more: testfile.md

variable1: boe variable2: bah

some text

I get the content of en external markdownfile like testfile.md with {{ "testfile.md"|content }}

But how to get variable1 ? {{ "testfile.md"| meta.variable1 }} will get nothing

PhrozenByte commented 6 years ago

Try this:

{{ pages["testfile"].meta.variable1 }}
omniperspective commented 6 years ago

I'm ashamed, it works. Was it that simple......

Zucht Henk.

PhrozenByte commented 6 years ago

Great to hear that! 👍 If you have any other question, don't hesitate to ask 😃

omniperspective commented 6 years ago

For the export as ..

I use {{ "testfile.md"|content }} to get the contect {{ pages["testfile"]|content }} this gets the content as well.

What should I use and any reason why so? Would like to learn .. Henk.

PhrozenByte commented 6 years ago

Actually neither {{ "testfile.md"|content }} nor {{ pages["testfile"]|content }} works 😉 You probably rather mean {{ "testfile"|content }} and {{ pages["testfile"].content }}. You should always use {{ "testfile"|content }}.

{{ pages["testfile"].content }} works only if the page contents have been parsed already. This is only true if...

  1. you have previously called {{ "testfile"|content }}, or
  2. you're accessing the contents of the requested page, or
  3. you have the PicoParsePagesContent plugin enabled (please note that we'll remove this plugin with Pico 2.0 due to performance reasons, so you really shouldn't rely on it).

Always use {{ "testfile"|content }}.

omniperspective commented 5 years ago

filename "bagger.md"

variable1: "aap" variable2: "noot" variable3: "mies" template: index

This is the bread and butter of text.


in Pico 2.0.2 I'm able to retrieve the data in the data part of the file with
{{ "bagger"|content }}

But I'm now unable to get the data from the YAML part
{{ "bagger"|.meta.variable1 }}   gives an error 500
{{ "bagger"|meta.variable2 }}   gives an error 500
{{ pages["bagger"].meta.variable1 }} doesn't work in 2.0.2

What is it that I don't understand?
What is the correct syntax to get the YAML variable data?

I'm unable to solve this, please help.
PhrozenByte commented 5 years ago

The problem is a really, really small typo: You're using ---- (four dashes) instead of the expected --- (three dashes) after the YAML Front Matter. Due to this, Pico won't recognize this as a YAML Front Matter (resulting in variable1: "aap" variable2: "noot" variable3: "mies" template: index getting print out on the page). {{ pages["bagger"].meta.variable1 }} is the correct syntax.

omniperspective commented 5 years ago

facepalm ;-( Thanks for the correction.