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



  • 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?


  • Global Moderator

    {{{ 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


  • Global Moderator

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



  • It worked. Thanks a lot!


Log in to reply
 


Star

Looks like your connection to NodeBB was lost, please wait while we try to reconnect.