picocms / Pico

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

Custom meta tags with more than one word are not slugified #392

Closed 1deadpixl closed 7 years ago

1deadpixl commented 7 years ago

Adding a custom meta tag with more than one word (i.e. with a space) results in a meta key with a space which in turn causes a server-side error during parsing. The expected behavior would be for the space to be converted to an underscore ("slugified"), for example, the meta tag "Logo Type" would become "logo_type", not "logo type".

I'm using Pico 1.0.5 with PHP 7.1.1 running on Apache 2.2.31.

PhrozenByte commented 7 years ago

What exact "server-side error during parsing" do you experience?

Adding a meta value with a whitespace in its key works just fine.

---
key with: some whitespaces
---

results in

array (size=9)
  ...
  'key with' => string 'some whitespaces' (length=16)
  ...

There's no need to slugify anything here, you can access such a meta value in Twig using {{ meta["key with"] }}.

1deadpixl commented 7 years ago

That's fair, I got a parsing error when using dot notation to get the meta value. I see that it works when using bracket notation but I think this breaks the established design pattern. Consider that the built-in meta value "Date Formatted" is stored under the key "date_formatted", as well as many other built-in variables that use underscores in place of spaces. It's easy to assume that custom meta keys would be converted in a similar manner. Thanks for pointing out the use of bracket notation, at least now I can continue using the current version with key names that make sense.

PhrozenByte commented 7 years ago

That's the reason why you shouldn't use spaces in meta keys 😉 Simply use key_with in your YAML frontmatter, so that you can access the meta value using Twig's dot notation (i.e. {{ meta.key_with }}).

btw: date_formatted isn't intended to be set in the YAML frontmatter, its value is automatically generated by Pico's core. You can "slugify" a meta value's key by hooking into Pico's onMetaHeaders event and registering the meta value. The following example plugin registers the meta value Key with (note the space and the uppercase K) and makes it available using the key_with key (note the underscore, i.e. {{ meta.key_with }}):

class SomePicoPlugin extends AbstractPicoPlugin
{
    public function onMetaHeaders(array &$headers)
    {
        $headers['key_with'] = 'Key with';
    }
}
1deadpixl commented 7 years ago

Great! Thanks for the tips—I appreciate the help and explanation.