getgrav / grav

Modern, Crazy Fast, Ridiculously Easy and Amazingly Powerful Flat-File CMS powered by PHP, Markdown, Twig, and Symfony
https://getgrav.org
MIT License
14.59k stars 1.41k forks source link

Minor issues with meta tags not outputting nicely #690

Closed stephenvoisey closed 8 years ago

stephenvoisey commented 8 years ago

Hey RocketTheme,

I'm trying to nicely generate my meta data tags in my template. As mentioned by Andy in the gitter room, he suggested using the new method as seen in Antimatter template to include a partial twig page with the following code in:

{% for meta in page.metadata %}
<meta {% if meta.name %}name="{{ meta.name }}" {% endif %}{% if meta.http_equiv %}http-equiv="{{ meta.http_equiv }}" {% endif %}{% if meta.charset %}charset="{{ meta.charset }}" {% endif %}{% if meta.property %}property="{{ meta.property }}" {% endif %}{% if meta.content %}content="{{ meta.content }}" {% endif %}/>
{% endfor %}

I ran into a couple of issues:

  1. The output HTML includes two meta tags by default. First off it kicks out a meta tag mentioning Grav CMS as the generator.
<!doctype html>
<html class="no-js" lang="">
    <head>
        <meta name="generator" content="GravCMS" />
<meta name="description" content="Page description here" />
        <title>Page Title | Site Name</title>
  1. Personally, I dislike the fact this is being added like this because I like 100 control over my HTML, for coder/arsehole/perfectionist reasons (take your pick :). I'd like this to be a preference option in the system or site settings as such, which can be on by default. I'll be promoting grav with a site link on my site and podcasting in the future about it.
  2. Following on from the meta generator is the meta description. As you can see - this seems to remove the indent from the formatting. Earlier on I also found this was breaking the twig filter: spaceless - which I am using for minification by leaving a large gap, but I can't repeat that it seems, but it is certainly ugly formatting. Spacing in twig is a pain like this typically.
  3. By default
 <meta charset="utf-8">
 <meta http-equiv="x-ua-compatible" content="ie=edge">

Would be good defaults to add, again as tweakable options in the config settings.

  1. Lastly, in HTML5 meta tags do not end in a /> just a >

Hope this is useful feedback, even if I am being picky. Cheers for a great CMS.

rhukster commented 8 years ago

There are some sample meta tags set by the site.yaml, and also there's a default generator tag. However, you can remove these by simplying putting this in your user/config/site.yaml:

metadata:
    generator:

Basically it removes the description and sets the generator tag to empty/null.

Throughout Grav there's a consistent methodology of being able to set things site-wide and then override by page. The same is true for metadata, if it's not being set by the page, it's probably being set by some site-wide configuration, in this case, site.yaml.

Regarding indenting, this is really to do with the nesting of the twig includes. You can control the whitespace a little with Twig via whitespace control: http://twig.sensiolabs.org/doc/templates.html#whitespace-control

However, because a Twig template can theoretically be called from any other template, and potentially at different nested depths, it's really not practical to do this. Really this is not unique to Twig or Grav, this is something that all CMSes face because by their very nature, they are modular.

Even though spaces are totally ignored by browsers and search-engines alike, I know some people are anal about it :) For those folks I suggest looking at the Tidy extension for PHP (or potentially your webserver). However note that there is going to be some level of performance degradation for this processing. So you should definitely test this kind of solution. Also it could 'corrupt' data it doesn't understand, so testing is key.

Some options:

The charaset and http-equiv tags I believe are coming from the theme. Grav doesn't have any knowledge of the output format other than as it is defined in the theme. You are expected to modify a theme to your needs, and this is the place to do that kind of thing.

Also this goes for the HTML5 meta tag ending in > rather than />. You just change this in the theme's partial for outputting the metadata. Again Grav itself doesn't control the output at all. The theme is considered something the user should manipulate to his/her own needs.

flaviocopes commented 8 years ago

P.S. feel free to post PRs to Antimatter if you feel something can be improved in regards to its output.

stephenvoisey commented 8 years ago

Appreciate the great feedback and help, apologies if I posted here in error.