Conditional inclusion of HTML content in the plugin's template file

NodeBB Development
  • I am writing a plugin. I want that certain HTML included to be displayed only when a user clicks a particular URL (one among the several URLs). How can I achieve this?

  • {{{ if your_condition }}}
    <span>HTML stuff you only want displayed under 'condition'</span>
    {{{ end }}}
    

    Then you have to set your_condition as a property of the data object when calling res.render:

    res.render('template', {
      your_condition: req.path === 'targetted_path',
    });
    

    Just as example, might not actually work.

  • //map.tpl
    <ul class="nav nav-pills">
    <li><a href="{config.relative_path}/map?section=section1">[[map:A]]</a></li>
    <li><a href="{config.relative_path}/map?section=section2">[[map:B]]</a></li>
    </ul>

    {{{ if my_condition }}}
    <span>
    <p>Some Text</p>
    <input type="text" class="form-control input-lg" placeholder="Search"/>
    </span>
    {{{ end }}}

    //In the plugin's controllers.js
    console.log(nconf.get('relative_path')); //returns undefined
    console.log(req.path); //returns /api/map

    In fact I was expecting req.path should equal to http://localhost:4567/map?section=A
    .. but it is returning req.path equal to /api/map

    Thank you very much

  • //map.tpl
    <ul class="nav nav-pills">
    <li><a href="{config.relative_path}/map?section=section1">[[map:A]]</a></li>
    <li><a href="{config.relative_path}/map?section=section2">[[map:B]]</a></li>
    </ul>

    {{{ if my_condition }}}
    <span>
    <p>Some Text</p>
    <input type="text" class="form-control input-lg" placeholder="Search"/>
    </span>
    {{{ end }}}

    //In the plugin's controllers.js
    console.log(nconf.get('relative_path')); //returns undefined
    console.log(req.path); //returns /api/map

    In fact I was expecting req.path should equal to http://localhost:4567/map?section=A
    .. but it is returning req.path equal to /api/map

  • Oh actually in that case you should be able to use req.query.section I think.

  • It worked. Thanks a lot!


Suggested Topics


  • 0 Votes
    5 Posts
    407 Views
  • 1 Votes
    5 Posts
    1451 Views
  • 0 Votes
    8 Posts
    1934 Views
  • 0 Votes
    3 Posts
    1489 Views
  • 0 Votes
    6 Posts
    2083 Views