Skip Navigation

[Resolved] render_view is never truly empty

This support ticket is created 8 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Author
Posts
#346778

I am trying to:

Check if render_view is empty.

              <?php
                $argsView = array('name' => 'featured-news');
                $featuredNewsView = trim( render_view( $argsView ) );
                if (!empty($featuredNewsView) && is_front_page()) {
              ?>

I visited these URLs:

- https://toolset.com/forums/topic/check-if-render_view-returns-null/
- https://toolset.com/forums/topic/even-if-view-is-empty-render_view-still-output-something/
- https://toolset.com/forums/topic/check-if-render_view-returns-null-for-real/

I expected to see:

Nothing (if the view is empty, it shouldn't render anything).

Instead, I got:

<div id="wpv-view-layout-279-CPID6" class="js-wpv-view-layout js-wpv-layout-responsive" data-viewnumber="279-CPID6" data-pagination="{"ajax":"false","effect":"fade","duration":"500","stop_rollover":"false","cache_pages":1,"preload_pages":1,"pre_reach":"1","spinner":"default","spinner_image":"<em><u>hidden link</u></em>......./wp-content/plugins/wp-views/embedded/res/img/ajax-loader.gif","callback_next":","max_pages":1,"page":1}">
  <input id="js-wpv-pagination-page-permalink" value="/?wpv_view_count=279-CPID6&wpv_paged=1" type="hidden">
</div>

This was working just fine until :
WP Views 1.11 Released on: November 4, 2015

#346910

Thank you for contacting us here in the Support Forum and for providing the Debug Informations

Since Views 1.11 we have some additional default HTML around every Views loop output.

This is because we added different features, like infinite scrolling.

Our DEV Team is considering a new feature, that consists on a way to select a View, select a Content Template, and then get the raw results of the Content Template applied to each element of the View results.

By now, a View will wrap that HTML by default, and the only way to avoid it is to add this to your functions.php:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

function prefix_clean_view_output( $out, $id ) {
    if ( $id == '375' ) { //change the View ID
        $start = strpos( $out, '<!-- wpv-loop-start -->' );
        if ( 
            $start !== false
            && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-loop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-loop-end -->' );
            $out = substr( $out, 0, $end );
        }
    }
    return $out;
}

Please acknowledge that Views was and is designed as a Redner Engine instead of a "data provider" engine where you produce bare (raw) results.
With the latest features this default HTML had to be added.

As mentioned we are considering a new feature to produce a "clean" output, and above Filter allows you to output a clean result too, but please acknowledge that with it, Pagination, specially AJAX pagination, Parametric search, specially AJAXed parametric search, and Other future features will not work.

I hope with above approach you can work with your setup, otherwise please don't hesitate to inform me in case you need help or assistance with.

Thank you for your patience.

#347509

Hello Beda,

I am afraid it still doesn't work at all.

I added the code you provided to my functions.php file and changed the View ID and I still get this:

<div class="hero__videowrapper hide-for-small-only">
  <div id="wpv-view-layout-279-CPID6" class="js-wpv-view-layout js-wpv-layout-responsive" data-viewnumber="279-CPID6" data-pagination="{"ajax":"false","effect":"fade","duration":"500","stop_rollover":"false","cache_pages":1,"preload_pages":1,"pre_reach":"1","spinner":"default","spinner_image":"<em><u>hidden link</u></em>","callback_next":","max_pages":1,"page":1}">
  <input id="js-wpv-pagination-page-permalink" value="/?wpv_view_count=279-CPID6&wpv_paged=1" type="hidden">
</div>
#347601

Thanks for the Details

That code works only on Views not using any Customization, as pagination or similar.

As elaborated here, this code will likely break those features and / or produce unexpected output:
https://toolset.com/forums/topic/render_view-is-never-truly-empty/#post-346910

1. <div class="hero__videowrapper hide-for-small-only">

This is not a Views HTML, do you use this perhaps on Layouts and added that class in layouts?

2. The View will always output this default HTML unless you apply my code to the View (ID)
It seems also you use a View that has some Pagination or Query Settings enabled.

If you apply the code to a "default" view, which is outputting only results, with no Pagination, Queries, etc, it will produce a empty HTML output, means, no default HTML.

But for your purpose, to check if a View actually returns some query results, you could use the below API, which is the intended one to check this and will work with current View as well:

get_view_query_results($view_id)

https://toolset.com/documentation/user-guides/views-api/ > Function ‘render_view_template’

This is better because if you want to know whether the View will produce results or not, rendering it is not only not accurate but expensive:
you perform the query and then loop over it.

The looping is not needed here, you just want to know whether the query produces results, right?

Then you can use the above API to check that.

Please let me know if you need further infos about this.

Thank you for your patience.

#348416

Thank you for your answer.

Maybe it would be better if I show you my code:

In my view, I have the following :
There is no pagination.

[wpv-layout-start]
  [wpv-items-found]
  <!-- wpv-loop-start -->
    <wpv-loop>
      <div id="featured-video"></div>
      <h2 class="featured-news-title">[wpv-post-title]</h2>
      <script>
        jwplayer("featured-video").setup({
          playlist: [{
            image: "[wpv-post-featured-image size="Header image" output="url"]",
            sources: [{
              file: "<em><u>hidden link</u></em>:" + "[types field="video-name-news" output="raw"][/types]" + ".mp4"},{
              file: "<em><u>hidden link</u></em>:" + "[types field="video-name-news" output="raw"][/types]" + ".mp4" + "/playlist.m3u8"
            }]
          }],
          primary: "html5",
          width: "100%",
          aspectratio: "16:9",
          androidhls: true,
          advertising: {
            client: 'vast',
            schedule: {
              adbreak1: {
                offset: "pre",
                tag: '<em><u>hidden link</u></em>' + Math.floor(Date.now() / 1000)
              },
              adbreak2: {
                offset: "post",
                tag: '<em><u>hidden link</u></em>' + Math.floor(Date.now() / 1000)
              }
            }
          }
        });
      </script>
    </wpv-loop>
  <!-- wpv-loop-end -->
  [/wpv-items-found]
[wpv-layout-end]

And in my WP template, I have this:

<?php

  $argsView = array('name' => 'featured-news');
  $featuredNewsView = trim( render_view( $argsView ) );

  if (!empty($featuredNewsView) && is_front_page()) {
?>


<div class="hero__videowrapper hide-for-small-only">
   <?php echo $featuredNewsView; ?>
</div>

As you can see, I am already using render_view( $argsView ).

#348556

Thanks for the Details

Please refer to this Post:
https://toolset.com/forums/topic/render_view-is-never-truly-empty/#post-347601

I mention to use get_view_query_results($view_id) to check if a View is producing results, not render_view( $argsView ).

Render a View renders (plots) the View.
This will not be empty in any case since default HTML is there.

get_view_query_results($view_id) get's the rests of the Query.
If you check in this, you can check if get_view_query_results($view_id) returns some content (query results)

It is the correct way to check if a View returns empty or not.
Checking on the RENDERED content renders first the View, then checks on it.
That will include the HTML too.

Does that help?

Please let me know if you need further infos about this.

Thank you for your patience.

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.