Skip to content
Advertisement

How to escape HTML string on Moment.js formatter inside Vue template?

Let’s say I have a current date filtered to February 22nd 2019. I want the nd sign to be inside the <sup class="small"></sup> tag. The problem is, I’m using Vue and Moment.js at the same time.

In PHP, I could just do:

{!! CarbonCarbon::today()->format('F d<sup class="small">S</sup> Y') !!}

but how can I do that in Javascript and Vue? Or maybe there is cleaner way to do so? Please take a look at my script..

JavaScript

Advertisement

Answer

I believe you have to use v-html for that. So you’d have filterCurrentDate generate the HTML from the Moment text, and then use v-html to render that as-is.

JavaScript

Note I changed the name of the filter to make it explicit that it returns HTML.

For filterCurrentDateHTML, perhaps:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement