Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template not rendering #596

Open
kevit-yuvraj-kanakiya opened this issue Oct 3, 2023 · 0 comments
Open

Template not rendering #596

kevit-yuvraj-kanakiya opened this issue Oct 3, 2023 · 0 comments

Comments

@kevit-yuvraj-kanakiya
Copy link

kevit-yuvraj-kanakiya commented Oct 3, 2023

Can someone check this whats wrong with this code. this is working fine in liquid js playground but when I converted it into c# code it is not working correctly.

using Fluid;
using System;
using System.Collections.Generic;

public class MyTemplateContext
{
    public object ApiResponseData { get; set; }
    public Dictionary<string, object> CustomFields { get; set; }
}

public class Program
{
    public static void Main()
    {
        var template = "FYI:- Your are applying OD from {{CustomFields.fromDate | date: '%d/%m/%Y'}} to {{CustomFields.toDate | date: '%d/%m/%Y'}} day(s).{% assign weekOffDates = ApiResponseData | where: \"isWeekOFfStatus\", true | map: \"shiftDate\" %}{% assign holidayDates = ApiResponseData | where: \"isHolidayStatus\", true | map: \"shiftDate\" %}{% if weekOffDates.size > 0 or holidayDates.size > 0 %}{% if weekOffDates.size > 0 %}{{ weekOffDates | join: \", \" }} are week offs.  {% endif %}{% if holidayDates.size > 0 %}{{ holidayDates | join: \", \" }} are holidays.{% endif %}{% endif %}";

        var context = new MyTemplateContext
        {
            ApiResponseData = new List<object>
            {
                new { shiftDate = "2023-10-01", isWeekOFfStatus = true, isHolidayStatus = false },
                new { shiftDate = "2023-10-02", isWeekOFfStatus = false, isHolidayStatus = true }
            },
            CustomFields = new Dictionary<string, object>
            {
                { "fromDate", "2023-10-01" },
                { "toDate", "2023-10-03" }
            }
        };

        var parser = new FluidParser();
        var result = parser.TryParse(template, out var parsedTemplate, out var errors);

        if (result)
        {
            var output = parsedTemplate.Render(new TemplateContext(context));

            Console.WriteLine(output);
        }
        else
        {
            Console.WriteLine("Template parsing failed with errors:");
            foreach (var error in errors)
            {
                Console.WriteLine(error);
            }
        }
    }
}

Output:
FYI:- Your are applying OD from 01/10/2023 to 03/10/2023 day(s).

Expected Output (working in liquid js playground):
FYI:- Your are applying OD from 01/10/2023 to 03/10/2023 day(s). 2023-10-01 are week offs. 2023-10-02 are holidays.

Fluid.Core : 2.5.0
.Net version: 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant