Search code examples
javascriptmean-stackswig-template

How do I split a string in a SWIG template?


I have a SWIG template in my web app where I am rendering the value of a score with a decimal value. Like this:

enter image description here

I am using Angular filters to break apart the number into the whole number and the decimal number. BUT in another one of my server templates I am not using Angular and need to rely only on SWIG to display the number values. How can I do some sort of javascript String.split operation on the server via SWIG?

<span class="ng-binding">
    {{ wine.scoreTotal }}
    <sup class="ng-binding">
        {{ wine.ScoreTotal }}
    </sup>
</span>

I see some filters in the SWIG documentation but didn't see a way to do a SPLIT filter. Is there a way to do inline Javascript string operation like so?

<span class="ng-binding">
    {% String(wine.ScoreTotal).split('.')[0] %}
    <sup class="ng-binding">
        .{% String(wine.ScoreTotal).split('.')[1] %}
    </sup>
</span>

Solution

  • Turns out I was pretty close. I changed the {% %} to {{ }} like so:

    <span class="ng-binding">
        {{ String(wine.ScoreTotal).split('.')[0] }}
        <sup class="ng-binding">
            .{{ String(wine.ScoreTotal).split('.')[1] }}
        </sup>
    </span>
    

    This did the trick.