2

Is there an undocumented shortcoming of Live Preview? Seems to only work where the entry variable is concerned.

I'm using a Structure section to create pages with "Chapters". The section (let's call it Areas) has a Max Level of 2. The idea is that level 1 entries load a template that calls the content from all of that entry's children.

For instance, Areas looks like this in the CP:

Learning
-- Our Approach
-- Design Patterns
-- etc.

The front-end works just fine. /areas/learning loads the areas/_entry template where I call all of the entry's children and output their content to the page.

The issue is in Live Preview when editing a child entry, such as Our Approach. It looks like Live Preview only likes using the provided entry variable, but will not update anything nested, found in a loop, or otherwise loaded into the template's context.

For instance, this simplified template code, while rendering as expected, does not update in Live Preview:

{% if craft.request.isLivePreview %}
    {% set myEntry = craft.entries.slug(craft.request.getSegment(2)).first() %}
{% endif %}

{% set chapters = myEntry.children %}

{{ myEntry }}
<ul>
{% for chapter in chapters %}
    <li>{{chapter}}</li>
{% endfor %}
</ul>

I renamed the entry variable as per this answer. Thoughts?

4

We just changed this behavior in Craft 2.3.2616:

Live Preview and Share links will now show the unpublished entry/category changes wherever the entry/category are being referenced in the templates (even areas where the entry/category was fetched from the database).

So now Live Preview (and Share links) should work as expected for you.

  • I like this kind of answers! – carlcs Dec 3 '14 at 23:42
  • this is amazing! thanks for listening Brandon :). Sorry @carlcs, but I'll have to change the correct answer to this one, since I went back and updated my template to reflect this change. – Brian Whitton Dec 4 '14 at 3:02
  • 1
    No way, @noSlouch! Now that I was so creative and found a solution for this, I insist that YOU stick to it. – carlcs Dec 4 '14 at 8:23
2

Update:

Thanks to P&T this workaround is not necessary any more with Craft 2.3.2616!


This was my temporary solution / bugfix:


I think you're right, Live Preview seems to only update if changes to the automatically provided entry EntryModel are detected.

So this causes problems with Live Preview:

{# Get `entry` manually #}
{% set home = craft.entries.section('home').first() %}
{{ home.welcome }}

while this works fine:

{{ entry.welcome }}

To get Live Preview working with your Chapters example, you'd need to somehow make the connection to the entry variable.

What I came up with is this: you still loop through the chapters returned from your craft.entries call. But within the loop you conditionally overwrite the chapter variable in case it matches your current entry EntryModel.

I also replaced getSegment() with getAncestors(), to not rely on any specific paths / routes that might change in the future:

{# Get the top level entry #}
{% if entry.level != 1 %}
    {% set rootEntry = entry.getAncestors().first() %}
{% else %}
    {% set rootEntry = entry %}
{% endif %}

{{ rootEntry.body }}

{% set chapters = rootEntry.children %}
{% for chapter in chapters %}

    {# Bugfix for live preview #}
    {% if chapter.id == entry.id %}{% set chapter = entry %}{% endif %}

    {{ chapter.body }}

{% endfor %}
  • nailed it! Thanks for rooting this out. I think this qualifies as a bug? – Brian Whitton Dec 1 '14 at 18:54
  • @noSlouch yeah, maybe it's a bug. But I think it's more likely that this is simply how Live Preview works right now. It definitely qualifies as a feature request though! – carlcs Dec 1 '14 at 23:53
  • 1
    I'll add something to the G+ page – Brian Whitton Dec 2 '14 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.