Blog-plugin: Setting page title as post title using onEnd(), no longer works

Created on 12 Jul 2017  路  3Comments  路  Source: rainlab/blog-plugin

Summary
PR #324 broke the suggested method of pulling in a posts title to use as a page title.

When following the 'Post page' example found in README.md:

title = "Blog Post"
url = "/blog/post/:slug"

[blogPost]
==
<?php
function onEnd()
{
    // Optional - set the page title to the post title
    if (isset($this->post))
        $this->page->title = $this->post->title;
}
?>
==
{% if post %}
    <h2>{{ post.title }}</h2>

    {% component 'blogPost' %}
{% else %}
    <h2>Post not found</h2>
{% endif %}

Expected behavior:
If the a post is found, the page title should be set as the post title.

Actual behavior:
The post is not found by the onEnd() function, because the post hasn't been loaded yet.

Loading the post in onRender() pushes it further down page cycle. I can't find a simple way to access that data anymore. Suggestions?

Most helpful comment

I'd say it's the right thing for the blog plugin to load the post on render to support inline parameters. It's more of a CMS issue that the page cycle leaves things a little murky when accessing anything that might use inline parameters as in this example, and onEnd() is a little misleading as it isn't actually run at the true end (after rendering). There's a cms.page.beforeRenderPage event, but if there were a cms.page.renderPage event or an onRenderEnd() method or something that the page could tie into before the layout is rendered, that might be helpful in situations beyond this one. There might also be other workarounds, but that option seems useful to me.

All 3 comments

I think we might need to look at when the component parameters are loaded by Twig with the inline {% component %} tag because if we move the loading of the post back to onRun() then if a post gets loaded from the inline tag, its information will not be available to the onEnd() function, instead it will be using the old or a null value.

I'd say it's the right thing for the blog plugin to load the post on render to support inline parameters. It's more of a CMS issue that the page cycle leaves things a little murky when accessing anything that might use inline parameters as in this example, and onEnd() is a little misleading as it isn't actually run at the true end (after rendering). There's a cms.page.beforeRenderPage event, but if there were a cms.page.renderPage event or an onRenderEnd() method or something that the page could tie into before the layout is rendered, that might be helpful in situations beyond this one. There might also be other workarounds, but that option seems useful to me.

Same here. When using onEnd() page function, post is not loaded:

snimek obrazovky 2017-07-28 v 11 10 46

Master branch of OctoberCMS works well, a development not.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rowild picture rowild  路  4Comments

drmzio picture drmzio  路  7Comments

nycparking picture nycparking  路  7Comments

willouch picture willouch  路  7Comments

silasrm picture silasrm  路  4Comments