Jump to content

Recommended Posts

Posted

I have some pages that are used only as partials, to be rendered as part of other pages using $page->render()

I have them inside a /widgets/ branch in my page tree. The problem is that they are viewable from the front-end: if someone visits mydomain.com/widgets/my-widget/ they see the partial HTML.

Is there a way I can keep my widgets accessible to editors in the back-end and capable of being rendered in other pages but without them being directly viewable?

  • Like 1
Posted

Pseudocode from my Phone...

If page->template = widget

Perfom a 404 error

Have a search with Google on the forum About 404 errors and hiding Pages in Frontend...

Regards mr-fan

  • Like 1
Posted

Thanks for the ideas.

@mr-fan: I think this might be the sort of thing you're hinting at - at the top of the widget templates...

<?php if( empty($options['pageStack']) ) throw new Wire404Exception(); ?>

Good info in this post from Jonathan Lahijani here and some info on pageStack from Ryan here. I like this solution.

@LostKobrakai: Sounds like this would work, but it seems to me there are downsides to using wireRenderFile() over $page->render(). I have a lot of different widget templates and widgets are selected by editors via a page field, so using $page->render() keeps the rendering code really simple and maintenance-free.

foreach($page->widgets as $widget) {
    echo $widget->render();
}

But if I use wireRenderFile() I need a whole chain of logic to check the template of each widget and render it with the correct file for that type of widget. Besides avoiding partial pages being directly viewable, are there other benefits of wireRenderFile() that make it preferable to $page->render()?

  • Like 4

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...