template.js to benchpress issue.

Technical Support
  • I have an older plugin I'm trying to update after another developer, however, I'm confused by the change of engine.

    The template.js front-end code is:

          var html = templates.parse(template, {
            item       : item,
            isEdit     : true,
            categories : categories,
            SETTINGS   : ajaxify.data.SETTINGS,
          });
    

    I must be missing something, I really cannot see why it doesn't work with benchpress.

  • What is the value of template? You can't get the rendered HTML directly like that, you have to pass a callback as the last parameter.

    What you should be doing is using

    require(['benchpress'], function (benchpress) {
      benchpress.parse(template, data, function (html) {
        // do whatever
      });
    });
    
  • template.js was only used in two places and I still have a hard time figuring out how he's using template.js

    
      function handlerClickCard_btnEdit(e) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
    
        var uid = $(this).attr('data-item-uid');
    
        if (!uid) return undefined;
    
        var item = ajaxify.data.list.find(function(itm) {
          return itm && itm.uid === uid
        });
    
        ajaxify.loadTemplate(self.config().templates.modalEditAlbum, function(template) {
          var categories = ajaxify.data.categories;
    
          // set selected option in selectbox
          if (item.category && item.category.cid) {
            categories.forEach(function(itm) {
              if (itm && itm.cid === item.category.cid) itm.selected = true;
            });
          }
    
          var html = templates.parse(template, {
            item       : item,
            isEdit     : true,
            categories : categories,
            SETTINGS   : ajaxify.data.SETTINGS,
          });
    
          bootbox.dialog({
            title   : '[[gallery:placeholder.edit]]' + ' "' + item.name + '"',
            message : html,
          });
        });
      }
    
      function handlerClick_btnNewGallery() {
        ajaxify.loadTemplate(self.config().templates.modalEditAlbum, function(template) {
          var categories = ajaxify.data.categories;
    
          console.log(template);
          var html = templates.parse(template, {
            item: {
              isPublic: false,
            },
            isNew      : true,
            categories : categories,
            SETTINGS   : ajaxify.data.SETTINGS,
          });
    
          bootbox.dialog({
            title   : '[[gallery:modal.title.new_gallery]]',
            message : html,
          });
        });
      }
  • @tmikaeld ok so instead of

    ajaxify.loadTemplate(templateName, function (template) {
      // before stuff
      var html = templates.parse(template, data);
      // after stuff
    });
    

    You should just do

    // before stuff
    benchpress.parse(templateName, data, function (html) {
      // after stuff
    });
    
  • What he's doing is super weird, he's loading in the template manually and then parsing it with templatesjs. That kind of flow is no longer supported, so you have to use the (arguably easier) form I suggested.

  • @PitaJ Thanks, I'll test it first thing tomorrow 🙂

  • @PitaJ

    Is benchpress not loaded globally?

    I get "benchpress is not defined", so i guess i need to require it, but do i have to do this in a wrapping function or can i use:

    var benchpress = require(['benchpress']);
    

    UPDATE:

    When i tried with var benchpress = require(['benchpress']); I get benchpress.parse is not a function

  • This is how i rewrote the functions by the way.

    var benchpress = require(['benchpress']);
    
      function handlerClickCard_btnEdit(e) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
    
        var uid = $(this).attr('data-item-uid');
    
        if (!uid) return undefined;
    
        var item = ajaxify.data.list.find(function(itm) {
          return itm && itm.uid === uid
        });
    
          var categories = ajaxify.data.categories;
    
          // set selected option in selectbox
          if (item.category && item.category.cid) {
            categories.forEach(function(itm) {
              if (itm && itm.cid === item.category.cid) itm.selected = true;
            });
          }
    
          benchpress.parse(self.config().templates.modalEditAlbum, {
            item       : item,
            isEdit     : true,
            categories : categories,
            SETTINGS   : ajaxify.data.SETTINGS,
          }, function (html) {
            bootbox.dialog({
              title   : '[[gallery:placeholder.edit]]' + ' "' + item.name + '"',
              message : html,
            });
          });
    
      }
    
    
    
      function handlerClick_btnNewGallery() {
    		
        var categories = ajaxify.data.categories;
    
    		benchpress.parse(self.config().templates.modalEditAlbum, {
          item: {
            isPublic: false,
          },
          isNew      : true,
          categories : categories,
          SETTINGS   : ajaxify.data.SETTINGS,
          }, function (html) {
              bootbox.dialog({
                title   : '[[gallery:placeholder.edit]]' + ' "' + item.name + '"',
                message : html,
              });
            });
    
      }
    

    It this what you had in mind @PitaJ ?

  • require uses a callback.

    require(['benchpress'], function (benchpress) {
      // code that uses benchpress
    });
    
  • @yariplus Work! Thanks 🐶


Suggested Topics


  • default skin issue

    Moved Unsolved Technical Support
    0 Votes
    2 Posts
    290 Views

    @srujan where are you selecting the skin change, on server or client side?

  • 0 Votes
    2 Posts
    226 Views

    @maroonbee have you tried experimenting? It shouldn't be too hard to find

  • 0 Votes
    4 Posts
    1k Views

    We do throttle the confirmation emails, but when you switch your email, you are automatically sent another confirmation email, so that's why you can't send it again manually... I remember someone made an issue about this recently too.

  • 0 Votes
    1 Posts
    2k Views

    Hello, after upgrade from 0.8 to 1.0 i have issue with launching forum in the cloud.
    It crash with message:
    2016-06-30T08:15:39.868567+00:00 app[web.1]: 30/6 11:15 [3] - error: Error: ENOENT: no such file or directory, scandir '/app/public/uploads/sounds'

    For image upload im using: "nodebb-plugin-amazons3": "^0.2.4"

  • 3 Votes
    1 Posts
    1k Views

    I believe you should have as many partials in your main template as efficiently as possible to cut down on redundant code. Here I'll write the standard for a good rule of thumb on how you should organize your templates and all the code within in them.

    Any redundant JS scripts should be in their own partial to be reused in other templates if need be.

    Take full advantage of the NBB API so that you extend your template even further. For example using <!-- IF loggedIn --> condition can greatly improve UX and conversions rates on your site. Having some type of call to action or banner within these will let the user know that they aren't logged. OR when they are logged in, show something else. You can come up with some pretty crafty ideas. But yeah, do what you can with the existing API.

    You can only do so much with the templates but is that good enough? Maybe, maybe not, but it does work when you plan out the logical flow and hopefully not get lost in the div's and such. For example on the Majestic Theme, the topic covers are pretty complex and one can easily get lost in the flow of what's going on. Here's a screenshot of the topics UI.

    Screen Shot 2015-03-17 at 8.12.29 AM.png

    Now lets take a little peek at whats under the hood;

    Screen Shot 2015-03-17 at 8.16.46 AM.png

    Screen Shot 2015-03-17 at 8.14.33 AM.png

    Now there's way more to this, but this is just a little example of how I control the topic covers. The only reason why I'm posting this is because I'm quite certain it would take a while to replicate without all of the other partials in this one template (not shown for obvious reasons).

    You can also do stuff like this to take control over pluralization of your template: post<!-- IF !topics.unreplied -->s<!-- ENDIF !topics.unreplied -->
    Which would mean, if you don't have any posts, remove the s in post, otherwise its posts. Neat huh?

    Anyone else have any cool things they've done with the template system so far in their existing theme? Share some of your ideas here. 🙂