Forum Discussion

BowBharath's avatar
7 years ago

How to write inline if query in endpoint ?

When the count > 1 its should show best Answers else best answer.

<li class="item">${bestAnswersCount} Best Answers</li>

 

Also would like to show the count greater than 999 it should show 1K like wise.

 

Appreciate your help.

  • Problem 1) Plural of nouns can also be handled via Lithium Text Strings:

     

     

    // your lithium string (add in Text Editor in the Studio section) would look
    // something like this: 
    // custom.best-answer = Best {0,choice,0#Answers|1#Answer|1<Answers}
    <li class="item">${bestAnswersCount} ${text.format('custom.best-answer', count)}</li>

    Problem 2) Shortening of numbers, I usually do this with the follwing function (maybe overkill for your usecase):

     

     

    <#function math_shorten number>
    	<#local units = [
    		{"factor": 1, "unit": ""},
    		{"factor": 1000, "unit": "K"},
    		{"factor": 1000000, "unit": "M"},
    		{"factor": 1000000000, "unit": "G"},
    		{"factor": 1000000000000, "unit": "T"},
    		{"factor": 1000000000000000, "unit": "P"},
    		{"factor": 1000000000000000000, "unit": "E"}
    	] />
    	
    	<#local order = number?abs?round?c?length />
    	<#local i = ((order - 1) / 3)?floor />
    	<#if (i < 0)>
    		<#local i = 0 />
    	</#if>
    	
    	<#local result = (number / (units[i].factor))?string("0.#") + units[i].unit />
    
    	<#return result />
    </#function>
    
    // you would replace your ${bestAnswersCount} with the following:
    ${math_shorten(count)}
    
    // then you would finally wind up with:
    <li class="item">${math_shorten(count)} ${text.format('custom.best-answer', count)}</li>

     The shortening function is pretty resilient, it will handle negative numbers, cut off a .0 ending etc., the only thing it will break with is if you exceed the maximum possible unit, e.g. you end up asking for an item in the units sequence that does not exists, the likelihood of this happening is very low though...

  • <#assign string = "Best Answer" />
    <#assign bestAnswersCount = count />
    <#if bestAnswersCount &gt; 1>
              <#assign string = "Best Answers" />
    </#if>
    <#if bestAnswersCount &gt; 999 />
            <#assign bestAnswersCount = '1k' />
    </#if>
    <li class="item">${bestAnswersCount} ${string}</li>

    • I am learning....Thank you VikasB . The string best answers works awesome. Now i am worried about 1K...2K...2.2K....1L 

      Thinking about more generic code.

       

      • VikasB's avatar
        VikasB
        Boss

        BowBharath  I think in rare cases answer count may cross this limit. So you can use it

        <#assign string = "Best Answer" />
                <#assign bestAnswersCount = count />
        <#if bestAnswersCount &gt; 1>
                 <#assign string = "Best Answers" />
        <#elseif bestAnswersCount &gt; 999 />
                <#assign bestAnswersCount = "1k" />
        <#elseif bestAnswersCount &gt; 1999 />
               <#assign bestAnswersCount = "2k" />
        <#elseif bestAnswersCount &gt; 2999 />
               <#assign bestAnswersCount = "3k" />
        <#else>
               <#assign bestAnswersCount = "3k+" />
        </#if>
        <li class="item">${bestAnswersCount} ${string}</li>

        LMK If you think it can cross it. If yes, I'll try to find you a better solution.