janl / mustache.js

Minimal templating with {{mustaches}} in JavaScript

Home Page:https://mustache.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Render null value

tmdoit-zz opened this issue · comments

I'm using mustache to generate dynamic SQL queries, how can I render null value to show up without quotes? Below example:

BEGIN;
{{#payload.data}}
    INSERT INTO "{{{schema}}}"."{{{table}}}" ({{{payload.column_list}}},"SyncMetadataUpdateTS")
    VALUES
    ({{{payload.value_list}}})
    ON CONFLICT ("{{{column_id}}}") 
    DO 
       UPDATE SET {{payload.update_list}};
{{/payload.data}}
COMMIT;

payload.value_list is an array with some null values which for now doesn't render at all. Rendered result:

...
    INSERT INTO "dbo"."Dic_Customers" ("Id","Code","IsDefault","Activity","DefaultPriceTypeID","PaymentFormID","ServerID","Kind","UsedLimit","MaxOvertimeSum","MaxDelay","PayerId","UnlimitedCredit","OtherPayer","IsTrilateralTransaction","Type","AccountingAccountId","Points","PaymentDays","WarehouseType","WarehouseId","TermTypeID","CRMLastConversationTime","PeriodSettlement","TimeSettlement","ConsignationReturn","PeriodConsignationReturn","InvoiceReturn","PeriodInvoiceReturn","SupervisorEmployeeID","SupervisorCustomerID","SyncWasModifiedBy","Mask","CustomerGroupId","CRMType","CanGetLowestPrice","IsEOM","DaysEOM","CompanyOwnerID","DefaultPriceTypeIdForDesktop","ModificationDate","ReverseCharge","CurrencyId","MethodOfDelivery","IsAnonymized","IsSplitPayment","JpkProcedureCodeId","SyncMetadataUpdateTS")
    VALUES
    (91501,'DIGITALOAK',false,true,7,1,,0,0,0,0,,true,false,false,0,,0,,0,,,,,,,,,,,,1,,,711,false,false,0,13,7,'Sat Oct 31 2020 02:56:54 GMT+0100 (Central European Standard Time)',false,1,43,false,false,,1604105814147)
    ON CONFLICT ("Id") 
    DO 
       UPDATE SET "Id"=91501,"Code"='DIGITALOAK',"IsDefault"=false,"Activity"=true,"DefaultPriceTypeID"=7,"PaymentFormID"=1,"ServerID"=null,"Kind"=0,"UsedLimit"=0,"MaxOvertimeSum"=0,"MaxDelay"=0,"PayerId"=null,"UnlimitedCredit"=true,"OtherPayer"=false,"IsTrilateralTransaction"=false,"Type"=0,"AccountingAccountId"=null,"Points"=0,"PaymentDays"=null,"WarehouseType"=0,"WarehouseId"=null,"TermTypeID"=null,"CRMLastConversationTime"=null,"PeriodSettlement"=null,"TimeSettlement"=null,"ConsignationReturn"=null,"PeriodConsignationReturn"=null,"InvoiceReturn"=null,"PeriodInvoiceReturn"=null,"SupervisorEmployeeID"=null,"SupervisorCustomerID"=null,"SyncWasModifiedBy"=1,"Mask"=null,"CustomerGroupId"=null,"CRMType"=711,"CanGetLowestPrice"=false,"IsEOM"=false,"DaysEOM"=0,"CompanyOwnerID"=13,"DefaultPriceTypeIdForDesktop"=7,"ModificationDate"='Sat Oct 31 2020 02:56:54 GMT+0100 (Central European Standard Time)',"ReverseCharge"=false,"CurrencyId"=1,"MethodOfDelivery"=43,"IsAnonymized"=false,"IsSplitPayment"=false,"JpkProcedureCodeId"=null,"SyncMetadataUpdateTS"=1604105814147;
...

Empty values/no values should be NULL values.

I resolved the issue changing null values to string. Maybe adding some switch which will turn on/off rendering null values will be reasonable?
Here is my workaround: https://jsfiddle.net/tmdoit/aq5k24z0/2/

Hi @tmdoit!

Something like what you ended up with makes sense to me as well. It's quite natural to handle specific needs in the views like you've done, either in functions or converting the values from null -> "NULL" before providing those values to be rendered by mustache.

At this point, not having seen many requests to enable special null handling in certain scenarios and it's outside of the mustache spec that mustache.js is made to implement, I'm reluctant handling this specifically in the core of mustache.js.

With that in mind, I'm closing this issue for now. Feel free to re-open or post further questions after the fact if needed.