Checking whether template part is loaded in a page

I have a template part like this:

<nav id="subpagemenu" class="sidebar">
    <?php get_template_part( 'nav', 'subset' ); ?>
    <?php get_sidebar(); ?>
</nav>

Both of these are conditional, I mean, nothing will load if there’s no content. if there is content, they both load boxes like:

 <div class="widget"> 

Now I would like to be able to know if there are any widgets (sidebar, or my template_part), so that in my functions.php, I can set a variable $mywidgets=true or false.

I know that I can use ‘_is_active_sidebar’ for the one part, but how can I check the other part? I don’t want to register that part as a sidebar… All I need is a ‘true/false’.

I tried ‘is_page_template’ and ‘set_theme_mod’. I also looked at these threads:

https://stackoverflow.com/questions/6366351/getting-dom-elements-by-class-name

Is there a way to check which template file is being loaded, if it is not a page template file?

Am I thinking of this in the wrong way? Need to construct the theme differently?

Thanks!

Solutions Collecting From Web of "Checking whether template part is loaded in a page"

You can define a global variable in your theme functions.php

global $mywidgets;
$mywidgets = false;

in your template part add

global $mywidgets;
$mywidgets = true;

and in your functions you can check if its set to true:

function my_function(){
    global $mywidgets;
    $mywidgets = true;
    if ($mywidgets){
       // YES Your template part is loaded
    }else{
       // NO Your template part is not loaded
    }
}