Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Ember.js contains string helper

I'm looking to something like this in template file:

{{#contains 'invalid' item.name}}
  something
{{/contains}}

Basically check if the string contains a certain word

Not sure how the helper function would look like, but this is a random guess although I don't think this complies to above code.

import Helper from 'ember-helper';

export function containsHelper([substr, str]) {
 return str.contains(substr)
}

export default Helper.helper(containsHelper);

Something like that but how do I do it in a Ember.js helper such that I can use it in a template file?

Thanks!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You've guessed it pretty much right. This would be your implementation:

// app/helpers/contains.js

import { helper } from '@ember/component/helper';

function contains([substr, str]) {
  return str.contains(substr)
}

export default helper(contains);

and then in your template:

{{contains 'invalid' item.name}}

or rather

{{#if (contains 'invalid' item.name)}}
 your stuff
{{/if}}

Here's the doc

about 4 years ago · Juan Pablo Isaza Report

0

In addition to @Andrey's answer, I'm in-progress of getting https://github.com/glimmerjs/glimmer-vm/pull/1348 merged, which allows you to use "just plain functions" as helpers (RFC: https://github.com/emberjs/rfcs/pull/756)

There is a polyfill you can install: https://github.com/NullVoxPopuli/ember-functions-as-helper-polyfill

npm install ember-functions-as-helper-polyfill

and then you could do this:

export default class MyComponent extends Component {
  contains = (substr, str) => str.includes(substr);
}
{{#if (this.contains 'invalid' item.name)}}
  something
{{/if}}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!