Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
7 posts Page 1 of 1
Hello @All,

I have been working on a Document Management System project lately for a School. The system aims to store documents of students for a school established back in 1955.

During the development we had to generate the Transfer Certificates (TC) of the students, hence helping the overhead expenses of time, printing and manual generation of TC. During the development of the TC generation module we were posed with a requirement of printing the Date of Birth (DoB) of Students in Words and Figures.

While Figures can be done easily using the <cms:date> tag, the DoB in Words (English) was a challenge. I did search the internet for Cardinal and Ordinal Date formats but did not find something that could be put to use as per the requirement. This led to the development of a small module "Numeric Date to Words (English)". We am sharing the same here. It entirely uses couch tags to convert the numeric data to Words. It is a small code, nothing exquisite but we hope it will benefit the community.

There are four steps in total, required to implement this, viz.:
1. Copy the attached file "date-in-words.html" to the snippets folder.
date-in-words.zip
(1.71 KiB) Downloaded 57 times

2. Renaming the editable (in example, dateofbirth)
Code: Select all
<cms:set convert_date = "<cms:date dateofbirth format='d-m-Y' />" scope="global" />
to your editable type datetime field name

3. Embedding the "date-in-words.html" where you need to call it, using:
Code: Select all
<cms:embed 'date-in-words.html' />

4. Displaying the output using:
Code: Select all
<cms:show date_in_words />


I hope that my fellow Couch Developers find this useful.

Regards,
GenXCoders
Image
where innovation meets technology
A perfect case for Couch Arrays and <cms:func> Functions :)
4+ out of 5 ;)

~~~~~~~~~~~

Here is a proper version based on 'func' and does not use modded 'cms:number_format' —

Cms-Fu DateTime » date-to-english-words function

As simple as:

<cms:call 'date-to-english-words' />

...and resulting code is 5x times smaller

My Documentation, Addons, Functions.
Join COUCH:TALK telegram channel
@Bro Anton,
:D
Thanks!!!
Image
where innovation meets technology
genxcoders wrote: @Bro Anton,
:D
Thanks!!!

Why?

A code can be perfected considerably. I beg you for better localization to other languages - things like "Monday" or "January" should be handled by a localized <cms:date>!

If you wish someday to resolve that technical debt, here is a tip -

Let's define an array:
Code: Select all
<cms:capture into='numbers_array' is_json='1' >
{
"1" : "One",
"91" : "Ninety One"
}
</cms:capture>


Next, I can remove a lot of following conditions:

Code: Select all
<cms:if final_num eq '91'>
   <cms:set tens_ones = 'Ninety One' scope="global" />
</cms:if>


Instead use this only once -

Code: Select all
<cms:set tens_ones = "<cms:get "numbers_array.<cms:show final_num />" />" scope="global" />


There are many improvements waiting to be done in original posted code! :)

My Documentation, Addons, Functions.
Join COUCH:TALK telegram channel
trendoman wrote: There are many improvements waiting to be done in original posted code! :)


Apart from improvements, there is a notice that must be made:

@genxcoder's snippet code is based on modded cms:number_format and will produce wrong result without it.

- - -

@genxcoders, it is a pity that you did not find time to make this code better, wrapping it into a cms:func. Here is a better version that does not use 'cms:number_format' —

Cms-Fu DateTime » date-to-english-words function

As simple as:

<cms:call 'date-to-english-words' />


A few words.

First, it is easy to edit for another language –

Code: Select all
<cms:set words_0_to_19 = '[ "Cero",

   "Uno", "Dos", "Tres", "Cuatro", "Cinco",..etc


Second, it is easy to print result in JSON –

Code: Select all
{
   "day":"Thirty-first",
   "month":"December",
   "year_hi":"Two Thousand",
   "year_lo":"Twenty Two",
   "day_num":"31",
   "month_num":"12",
   "year_hi_num":"20",
   "year_lo_num":"22"
}


Third, it is easy to customize output via a custom func –

Code: Select all
<cms:func 'custom-print' result=''>
   <cms:show result.year_hi_num /><cms:show result.year_lo_num /> / <cms:show result.month /> / <cms:show result.day />
</cms:func>

<cms:call 'date-to-english-words' date='2022-12-31' print_func='custom-print'/>

- to get a custom output as you desire:

2022 / December / Thirty-first


Forth, dates can be "Y-m-d" and "d-m-Y".

- - -

Func is separately connected and works wonders ☺.

- - -

P.S. resulting code is 5x times smaller

My Documentation, Addons, Functions.
Join COUCH:TALK telegram channel
trendoman wrote: @genxcoders, it is a pity that you did not find time to make this code better, wrapping it into a cms:func. Here is a better version that does not use 'cms:number_format' —


I feel the FIRST Step is the most difficult one. I wonder why no one (including you) ever felt the need of such a module. But, it's good to know that you could build upon the First Step that I initiated. Nice you have re-written the code in a more concise manner.

I hope the community benefits at large with it.

Regards,
GenXCoders
Image
where innovation meets technology
genxcoders wrote:
trendoman wrote: @genxcoders, it is a pity that you did not find time to make this code better, wrapping it into a cms:func. Here is a better version that does not use 'cms:number_format' —


I feel the FIRST Step is the most difficult one. I wonder why no one (including you) ever felt the need of such a module. But, it's good to know that you could build upon the First Step that I initiated. Nice you have re-written the code in a more concise manner.

I hope the community benefits at large with it.

Regards,
GenXCoders


Nope, this is not what I meant. I saw a problem with modded 'number_format' - with stock couch tag your snippet produced wrong results for years starting with 19xx. Unfortunately, there was no mention about modded tag anywhere in the original post, so anyone who'd use the original snippet would see the error :!:

I didn't have and still don't have any use in number-to-words conversion, I am a forum moderator and a doc writer, I have no websites to build that could use this func. So that should explain the next paragraph. :roll:

I did not "build upon your first step". I fixed that step ;) I found that the code is not efficient (and reported it initially) and hard to translate to other languages (also reported it). You abandoned the snippet, unfortunately. So, all three reasons (efficiency, mistakes and possibly future languages added) were giving me energy to re-build this functionality completely from grounds, upon the tools that Couch has - func/call tag and couch arrays. I did not use calculations from the orig snippet, btw. So your contribution to the final result is the writing of english words and the initial posting. :)

My Documentation, Addons, Functions.
Join COUCH:TALK telegram channel
7 posts Page 1 of 1
cron