Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to tell which regions have content rendered from a helper #75

Open
joshfttb opened this issue Jul 13, 2016 · 2 comments
Open

Comments

@joshfttb
Copy link

For example, I would like to write a template helper that adds a custom class only when I don't have a template in the navbar region. Is it possible to tell this from a template helper? Thanks!

@KaitaniLabs
Copy link

From looking at the code I think this currently isn't possible. The regions are not stored on the BlazeLayout object, they only get passed into its methods.

I'm looking for the same kind of functionality as you. Preferably so that I could do something like the following:

{{#if regionHasData "regionName"}}
  //Do something
{{else}}
  // Do something else
{{/if}}

This could help reduce the number of layouts/templates needed, especially ones that are near identical to each other except for the fact that one is used instead of the other when an optional region has data.

@arunoda Would moving the var currentRegions = new ReactiveDict(); (lib/client/layout.js) onto the BlazeLayout object break something? If not it would be useful, then we can access the list of current regions from inside template helpers.

@KaitaniLabs
Copy link

KaitaniLabs commented Feb 23, 2017

EDIT: On re-reading I realise this isn't exactly what you're looking for. This method will tell you which regions are coming through, but it doesn't tell you if the expected template for each region exists.

Usually when you try to include a template that doesn't exist Meteor gets a bit grumpy and throws an error. It seems that with dynamic templates it just quietly does nothing. So if you use the below method it might not work exactly as expected.


I have almost found a way to do this. You can try the following:

// Example Route
adminConfigurationRoutes.route('/access', {
  name: "AdminConfigurationAccess",
  action: function () {
    BlazeLayout.render('AdminLayout', {subbar: 'AdminConfigurationSubbar', content: 'AdminConfigurationAccess'});
  }
});

// AdminLayout onCreated hook
Template.AdminLayout.onCreated(function() {
  const instance = this;
  instance.regions = new ReactiveVar([]);
  instance.autorun(function () {
    FlowRouter.watchPathChange();
    instance.regions.set(_.keys(instance.data));
  });
});

// AdminLayout helpers
Template.AdminLayout.helpers({
  regions : function(){
    return Template.instance().regions.get();
  }
});

// A little utility helper
Template.registerHelper("inArray",function(a,b){
  b = ((_.isArray(b)) ? b : b.split(","));
  return ((b.indexOf(a) > -1) ? true : false);
});

// Template
<template name="AdminLayout">
  {{#if inArray "subbar" regions}}
    {{>Template.dynamic template=subbar}}
  {{/if}}
</template>

So with that method you can also use it to display something if a dynamic template is not going to be used. For example:

// Template Example
<template name="AdminLayout">
  {{>Template.dynamic template=subbar}}
  {{#if inArray "subbar" regions}}
    {{> someTemplate}}
  {{else}}
    {{> someOtherTemplate}}
  {{/if}}
</template>

Hope that helps @joshfttb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants