balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

SailsJS EJS filters not working #6945

Open Degreane opened 4 years ago

Degreane commented 4 years ago

Node version: 12.13 Sails version (sails): 1.2.3 ORM hook version (sails-hook-orm): 2.1.1 Sockets hook version (sails-hook-sockets): 2.0.0 Grunt hook version (sails-hook-grunt): 4.0.0 Uploads hook version (sails-hook-uploads):


Hi Guys. I am trying to create a filter for usage inside EJS templates. the way described in the docs and issues related to versions 0.11 and 0.12 and none related to 1.x version of sails. i tried the methods and procedures in the old versions and yet it does not work in 1.2.3 version of sails.

what i need is to define a helper which successfully done and added the corresponding module and exported as it should with sync: true attribute in the module.exports.

the /api/helpers/timm-text.js :

module.exports = {
  friendlyName: 'Trimm text',
  description: '',
  inputs: {
    txt:{
      type:'string'
    },
    num:{
      type:'number',
      defaultsTo:50
    }
  },
  sync : true,
  exits: {
    success: {
      description: 'All done.',
    },
  },
  fn:  function (inputs,exits) {
    sails.log.debug('trimming ');
    return  exits.success(inputs.txt.slice(0,inputs.num));
  }
};

and in my template am trying to access it as such :

<p><%= movie.plot | filters.trimmText %></p>

and yet with no luck as it just outputs

<p>0</p>

Any idea how to use filters in the current version ?

Much Regards

sailsbot commented 4 years ago

@Degreane Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

johnabrams7 commented 4 years ago

Hey @Degreane, thanks for the detailed notes on this.

I do see two different spellings between the filter name /api/helpers/timm-text.js and code to access it <p><%= movie.plot | filters.trimmText %></p> - timm and trimm

Helpers generally aren’t included in sails locals by default, so we normally use them in the backend action instead. Can you try this as a workaround?