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

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

Closed
wesleyboar opened this issue Jun 25, 2020 · 16 comments · Fixed by #628 or #630
Closed

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

wesleyboar opened this issue Jun 25, 2020 · 16 comments · Fixed by #628 or #630

Comments

@wesleyboar
Copy link
Contributor

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
Copy link
Member

yakky commented Jul 11, 2020

@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
Copy link
Contributor Author

wesleyboar commented Aug 11, 2020

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 #613 (comment).

@wesleyboar
Copy link
Contributor Author

wesleyboar commented Aug 11, 2020

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
Copy link
Contributor Author

wesleyboar commented Aug 11, 2020

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

  • I have functional CMS.
  • I have functional page meta.
  • I have functional blog (article) meta.

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-metasource 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:

  • These values are available on a blog article page (in this order):
    1. 'meta': <meta.views.Meta object at 0x7fef6214aeb8>,*
    2. 'meta': <module 'meta' from '/usr/local/lib/python3.6/site-packages/meta/__init__.py'>,*
  • This values is available on any page:
    • 'meta': <module 'meta' from '/usr/local/lib/python3.6/site-packages/meta/__init__.py'>,*

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
Copy link
Member

yakky commented Aug 19, 2020

@tacc-wbomar this further documentation update should help clarify the proper template layout #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
Copy link
Member

yakky commented Aug 19, 2020

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
Copy link
Contributor Author

wesleyboar commented Aug 26, 2020

Thank you.

Status

  • Problem 1 is solved via option "c".
  • Problem 2 workaround was me inexpertly trying to solve Problem 3. I trust what you decide.
  • Problem 3 I explained poorly. I gave data without stating the problem.

Expected Result

  • Metadata is rendered for Home page, Blog page, and any Blog post.

Actual Result

  • Metadata is rendered for Home page, and any Blog post.
  • Metadata is not rendered for Blog page.

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
Copy link
Contributor Author

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
Copy link
Member

yakky commented Aug 26, 2020

@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
Copy link
Contributor Author

wesleyboar commented Aug 26, 2020

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.

  • ✅ Home page (has meta)
    Home page
  • ❌ Blog page (has no meta)
    Blog page
  • ✅ Blog post (has meta)
    Blog post

/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.

  • ✅ Home page (has meta)
    Home page
  • ✅ Blog page (has meta)
    Blog page
  • ❌ Blog post (has Blog page meta)
    Blog post

/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.

  • ✅ Home page (has meta)
    Home page
  • ✅ Blog page (has meta)
    Blog page
  • ✅ Blog post (has meta)
    Blog post

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
Copy link
Member

yakky commented Aug 26, 2020

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

@wesleyboar
Copy link
Contributor Author

wesleyboar commented Aug 26, 2020

Okay.

  • settings.py
  • pip freeze
    Note: The content of this file was updated to be accurate 3 minutes after sending this comment.

@yakky
Copy link
Member

yakky commented Aug 26, 2020

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
Copy link
Contributor Author

Yes! That works.

@yakky
Copy link
Member

yakky commented Aug 26, 2020

awesome!

i updated #630 to fix the base template

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

@yakky
Copy link
Member

yakky commented Aug 26, 2020

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

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