Render helper that returns false instead of an error #149

Open
talbet opened this Issue Jun 28, 2013 · 6 comments

Projects

None yet

3 participants

talbet commented Jun 28, 2013

What do you think of a render helper that can check for the availability of a partial and return true or false. That way you could setup fallbacks when working with custom post types like so:

$post_type = get_post_type();
if( is_partial("post/" . $post_type){
  render_partial("posts/" . $post_type);
} else{
  render_partial("posts/post");
};
Member

Looks like a good idea! If you want to attach a pull request we will consider it! Bye

Owner

I'm in doubt why you should check for post type at a lower level than the routing in index.php. If we consider that the scope of the routing is to discriminate post type such as single, archive, post, my_tax and so on, than we have to think that when we call a render_view() we know that such view will be for a specific post_type.

Let's consider another possible approach in routing:

if (is_post_type('foo') || is_post_type('bar')){
    render_view('posts/custom');
}

now is mandatory to code some logic in the view if you want to know what is the post type actually, than call for a specific partial. So where are money to have such a routing in place of

if (is_post_type('foo')){
    render_view('posts/custom_foo');
} elseif (is_post_type('bar')){
    render_view('posts/custom_bar');
}

Thanks @matjack1 and @talbet for your thoughts/ideas/clarifications about this! :)

Member

i think that this might be useful for dynamic routing. I think this is the intention of @talbet, from the snippet that he wrote. If you have lots of post types it could be useful to have a default partial and some specific partial for specific post types

Owner

Ok, if this is the goal, I'm totally with you. And I'd think about something like is_partial or render_post_type_partial. The one more generic and the latter more automagic. I wish to have you think about the most generic and reproducible scope for a helper.

@talbet drop us absolutely your ideas or pull request to review! Thanks for your contribution improving WordLess!

talbet commented Jul 2, 2013

I'll try and get a pull request ASAP. @matjack1 is correct about the intentions of this idea. One way you could do it is to abstract out the part of render_template that checks for valid files. This way you can use a get_template_path function (which returns a valid path or false) as part of render_template, or to check the availability of a template before you request the render. This might be a DRY way to do it , but would require a little refactoring.

Owner

Hey @talbet !

Have you got no news about this? It was a really good idea to work on such an improvement ✌️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment