手柄中的子串辅助器

时间:2016-12-06 06:01:20

标签: javascript node.js handlebars.js substring helper

我可以在Handlebars中编写一个帮助程序来判断字符串是否是另一个字符串的子字符串吗?

我编写了以下代码,但它似乎没有工作

这是帮手:

{{#if_sub colvalues "success"}}
    <td class="log-success"> {{colvalues}} </td>
{{/if_sub}}

这是实施:

enum class PlayerPiece {
  AIRCRAFT,
  BATTLESHIP,
  CRUISER,
  SUBMARINE,
  PATROL,
  EMPTY,
 };

 class Board {
 public:

   PlayerPiece playerBoard[100];
   PlayerPiece enemyBoard[100];

   void reset();
   void display() const;

 };

1 个答案:

答案 0 :(得分:1)

只是一点点错误。 if (a.includes(b))应为if (b.includes(a))

&#13;
&#13;
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>How to render html with Handlebars.js</title>
  <script src="https://cdn.jsdelivr.net/handlebarsjs/4.0.3/handlebars.min.js"></script>
  <script src="http://code.jquery.com/jquery-latest.min.js"    type="text/javascript"></script>
 
  <script id="simple-template" type="text/x-handlebars-template">

      {{#if_sub colvalues "success"}}
    <td class="log-success">  {{colvalues}} </td>
      {{/if_sub}}
  </script>
  
  <script type="text/javascript">
        Handlebars.registerHelper("if_sub",function(a, b, opts) {

     if (b.includes(a)){
       return opts.fn(this);
     }
         
     else{
       return opts.inverse(this);
     }
         
 });
  
    //wait for page to load
    $(document).ready(function(){
      var raw_template = $('#simple-template').html();
      var template = Handlebars.compile(raw_template);
      var data = {colvalues:"suc"}; //change value and see.
      var html = template(data);
      $('#main').append(html);

    });
  </script>
</head>
  
<body>
<!-- Insertion point for handlebars template -->
<div id="main" style="margin-left:100px">
</div>
</body> 
</html>
&#13;
&#13;
&#13;