2

I would like to generate the following HTML with Mithril's m:

<p>I am a <code>person</code></p>.

I'm currently using m.trust for this:

m.trust("<p>I am a <code>person</code></p>").

But I don't like the HTML string. Is there a better way to do this?

Lask3r
  • 137
  • 1
  • 1
  • 6

3 Answers3

3

Yes, use m function to do this:

m('p', [
    'I am a ',
    m('code', 'person')
])

See a whole component here: https://jsfiddle.net/rtkhanas/02adbhkt/

Ross Khanas
  • 1,491
  • 2
  • 15
  • 23
1

Where the html string come from?

If you are writing your view, then use m('p', ...) instead of m.trust For example, if only the "person" value is dynamic, you should have something like:

window.WhoAmI = {}

WhoAmI.controller = function(attr) {
    var ctrl = this

    ctrl.gender = m.prop(attr.gender)
}
WhoAmI.view = function(ctrl) {
    return m('p', [
        'I am a ',
        m('code', 
            ctrl.gender()
        )
    ])
}

If you get the whole html string from a request, then it's probably a bad thing, and you should try to rewrite your API (if possible) to send only the dynamic value to the client.

fluminis
  • 3,575
  • 4
  • 34
  • 47
0

You should have something like this:

m.module(document.body, {
    view: function() {
    return m('p', [
        'I am a ',
        m('code', 'person')
    ]);
  }
})
Suriyaa
  • 2,222
  • 2
  • 25
  • 44