nephila / djangocms-blog

django CMS blog application - Support for multilingual posts, placeholders, social network meta tags and configurable apphooks
https://djangocms-blog.readthedocs.io
BSD 3-Clause "New" or "Revised" License
403 stars 190 forks source link

How Does the Meta Block in Base Template Work? #613

Closed wesleyboar closed 4 years ago

wesleyboar commented 4 years ago

Description

I am confused by purpose of the meta block in base.html template code.

Steps to reproduce

  1. Confirm working djangocms-meta setup:
    1. Have working djangocms-blog install.
    2. Have custom djangocms-blog app templates, that still has this code in base.html:
      {% block meta %}
          {%  if meta %}
              {% include "meta/meta.html" %}
          {% endif %}
      {% endblock meta %}
    3. Have working configuration for djangocms-meta app. This includes this code in base.html CMS template:
       <head …>
           …
          {% include "meta/meta.html" %}
           …
       </head>
  2. Try to break working djangocms-meta setup:
    • Remove the {% block meta %} block of code from base.html app template (not the CMS template).
  3. Try, with more force, to break working djangocms-meta setup:
    • Remove the {% include "meta…" %} line of code from base.html CMS template (not the app template).

Expected result

  1. πŸ’‘ The code renders something somewhere.
  2. πŸ’‘ Removal of the code does not render something somewhere.
  3. πŸ’‘ Removal of the code does not render something somewhere.

Actual result

  1. βœ“ The code renders <meta> tags in the <head>.
  2. πŸ€·β€β™‚οΈ Removal of the code seems to render everything the same (as in step 1) everywhere.
  3. βœ“ Removal of the code does not render <meta> tags in the <head>.
yakky commented 4 years ago

@tacc-wbomar could you check if the documentation and comment added in #628 helps clarifying the social meta tags behavior and the role of the {% block meta %}?

wesleyboar commented 4 years ago

Yes. The {% block meta %} is clear*, now. Thanks!

(I will try out the "complete social meta tag rendering", like BLOG_FB next.)

* Update: Or not. See https://github.com/nephila/djangocms-blog/issues/613#issuecomment-672182302.

wesleyboar commented 4 years ago

Regarding BLOG_FB, BLOG_TWITTER, BLOG_GPLUS/BLOG_SCHEMAORG in settings:

I cannot find any results in djangocms-blog repo (searching via Github), nor (desperation) any results on the Internet (Google search). Are these future (1.2) configuration options?

Update: I stumbled upon the docs for these settings. I could not find them in the djangocms-blog repo (I think) because that file is not in the develop branch.

wesleyboar commented 4 years ago

After experimenting with {% if meta %}, I have found that it is not as reliable as expected. This may be because of my ignorance. Below is what I learned and my workarounds. Please, tell me whether any of these problems should be a new issue in this repo or another repo.

Setup

Problems

1. Page Meta: Making it Work (with Blog Meta?)

βœ“ Resolved by option "c" in new documentation.

Problem

Using {% include "meta/meta.html" %} to render page metadata (source djangocms-page-meta β†’ source djangocms-meta) did not work for me.

Workaround

…/templates/base.html

{% load … meta page_meta_tags … %}
{% page_meta request.current_page as page_meta %}
<!doctype html>
<html>

<head {% meta_namespaces %}>
  …
  {% block meta %}
    {% include 'djangocms_page_meta/meta.html' with meta=page_meta %}
  {% endblock meta %}

I do not remember from where I copied/learned the pieces of the workaround code.

2. Logic: Overwrite Block Even If There Is No meta

βœ“ There has since been uncertainty about original solution and a new working solution.

Problem

Using {% block meta %} {% if meta %} … {% endif meta %} {% endblock %} (as documentation offers) will overwrite the block, even if I have no meta with which to overwrite the block.

Workaround

Using {% if meta %} {% block meta %} … {% endblock meta %} {% endif %}, so that I overwrite the block only if I have meta with which to overwrite the block.

3. Data: At Least One meta is Available to Template

βœ“ The here since been a re-explanation and a new working solution.

Problem

Using the documentation, and having both page meta and blog meta:

Thus:

  1. βœ“ On a blog article page, I get blog meta.
  2. X On a blog article page, I do not get page meta.
    • Because, the 'meta': <module 'meta' …> makes {% if meta %} evaluate to true.

* Data retrieved via {% debug %}.

Workaround

…/templates/djangocms_blog/base.html

…
{# A certain code block should be here, but it would cause a bug. #}
{# SEE: `./post_detail.html` #}
{# SEE: https://github.com/nephila/djangocms-blog/issues/613 #}
…

…/templates/djangocms_blog/post_detail.html

…
{# This conditional is irrelevant, because we will always have
   `meta` in this template. This code block belongs in `./base.html`,
   which would benefit from a conditional, but also cause a bug. #}
{# SEE: https://github.com/nephila/djangocms-blog/issues/613 #}
{% if meta %}
  {% block meta %}
    {% include "meta/meta.html" with meta=meta %}
  {% endblock meta %}
{% endif %}
…
yakky commented 4 years ago

@tacc-wbomar this further documentation update should help clarify the proper template layout https://github.com/nephila/djangocms-blog/pull/630

djangocms-page-meta documentation is actually outdated and I will amend that as well

I also included your suggestion at point 2

Regarding point 3, blog overrides page meta on purpose as you can't have both

yakky commented 4 years ago

check updated documentation https://djangocms-blog--630.org.readthedocs.build/en/630/features/meta.html

and updated settings documentation https://djangocms-blog--630.org.readthedocs.build/en/630/autodoc/settings.html (which is not updated on RTD due to a build failure)

wesleyboar commented 4 years ago

Thank you.

Status

Expected Result

Actual Result

Workaround

Move Meta Include Logic From djangocms_blog/base.html to djangocms_blog/post_detail.html

This does does change Actual Result to match Expected Result. I am not suggesting this, because I don't know. I am merely reporting what seemed to work after trial and error.

…
    …
        {% include "meta/meta.html" %}
    …
…

Failed Solutions

Remove Surrounding Meta Include Logic from djangocms_blog/base.html

This does does change Actual Result, but the Blog post has Blog page metadata.

{% include "meta/meta.html" %}

Change Meta Include Logic In djangocms_blog/base.html

This does not change Actual Result.

{% block meta %}
    {% if meta %}
        {% include "meta/meta.html" %}
    {% endif %}
{% endblock meta %}

This does not change Actual Result.

{% if meta %}
    {% block meta %}
        {% include "meta/meta.html" %}
    {% endblock meta %}
{% endif %}
wesleyboar commented 4 years ago

Is my Expected Result strange?

Perhaps, Blog page should not have page metadata. I really don't understand how users use social media, I'm just trying to get the metadata present on all the pages so I can complete implementation of our blog/news/social-media-metadata feature.

yakky commented 4 years ago

@tacc-wbomar I don't understand if you can see any metadata in the post detail view, or you can see the blog post ones and not the CMS page

If you can't see any metadata, that's of course an issue because the it's the whole point of the django-meta integration :)

I will try to replicate the issue in the https://github.com/yakky/djangocms-blog-poc repo

wesleyboar commented 4 years ago

With the documented setup, I do not see any meta tags on the CMS's Blog page (I see meta tags on Home page, and on a Blog post page).

I think this is all the relevant info to show my setup. If helpful, I'll include bits of settings.py.

Variations

/base.html w/ "If & Block" Meta Inclusion

templates/djangocms_blog/base.html

{% if meta %}
    {% block meta %}
        {% include "meta/meta.html" with meta=meta %}
    {% endblock meta %}
{% endif %}

_The templates/djangocms_blog/post_detail.html has no meta inclusion._

/base.html w/ Direct Meta Inclusion

templates/djangocms_blog/base.html

{% include "meta/meta.html" with meta=meta %}

_The templates/djangocms_blog/post_detail.html has no meta inclusion._

/post_detail.html w/ "If & Block" Meta Inclusion

templates/djangocms_blog/post_detail.html

{% if meta %}
    {% block meta %}
        {% include "meta/meta.html" with meta=meta %}
    {% endblock meta %}
{% endif %}

_The templates/djangocms_blog/base.html has no meta inclusion._

Unchanged Files

templates/base.html

{# FAQ: Conditional includes are intentionally verbose #}
{# SEE: https://confluence.tacc.utexas.edu/x/QwI9Cw #}

{% load cms_tags staticfiles sekizai_tags cache i18n %}
<!doctype html>
<html>

{% if settings.FEATURES.blog %}
  {% with path="djangocms_blog_head.html" %}{% include path %}{% endwith %}
{% else %}
<head>
{% endif %}
  …

  <!-- Metadata. -->
  <title>{% block title %} This is my new project home page {% endblock title %}</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  {% if settings.FEATURES.blog %}
    {% block meta %}
      {% with path="djangocms_page_meta.html" %}{% include path %}{% endwith %}
    {% endblock meta %}
  {% endif %}

templates/djangocms_blog_head.html

{% load meta %}

<head {% meta_namespaces %}>

templates/djangocms_page_meta.html

{% load page_meta_tags %}
{% page_meta request.current_page as meta %}

{% include 'djangocms_page_meta/meta.html' %}
yakky commented 4 years ago

@tacc-wbomar the settings and a pip freeze output would be of great help, thanks!

wesleyboar commented 4 years ago

Okay.

yakky commented 4 years ago

thanks for the details I confirm I have been able to reproduce the issue

Can you try replacing in /templates/djangocms_blog/base.html

{% block meta %}
    {% comment %}
    This is needed if you can't add ``{% include "meta/meta.html" %}`` in the django CMS template attached to the blog
    page.
    If you have something like the snippet below in the main template used by the django CMS page, you don't need
    this block and you can safely remove it when customizing the blog templates

    ...
    <html>
        <head>
        <title>{% block title %}{% page_attribute 'title' %}{% endblock title %}</title>
        {% include "meta/meta.html" %}
        ...

    {% endcomment %}
    {% if meta %}
        {% include "meta/meta.html" %}
    {% endif %}
{% endblock meta %}

with

{% block meta %}
    {% if meta %}
        {% include "meta/meta.html" %}
    {% else %}
        {{ block.super }}
    {% endif %}
{% endblock meta %}
wesleyboar commented 4 years ago

Yes! That works.

yakky commented 4 years ago

awesome!

i updated #630 to fix the base template

Thanks a lot for your patience and your super detailed reports!

yakky commented 4 years ago

@tacc-wbomar thanks again. Closing this via merge of #630