antlrcs icon indicating copy to clipboard operation
antlrcs copied to clipboard

StringTemplate4 cloned templates (shadow) work only once, subsequent calls render empty values

Open lafar6502 opened this issue 8 years ago • 1 comments

Here is a failing test First template (t0) created with 'CreateShadow' works OK Second (t1) does not, renders all model values as empty ST4.0.8

` public class Test { public string Name { get; set; } public Test Parent { get; set; } public string Title; public decimal Age { get; set; } public DateTime CreatedDate;

        public Test()
        {
            Name = "name of test";
            Title = "titel titel";
            Age = 198.88m;
            CreatedDate = DateTime.Now;
        }
    }

    [TestMethod]
    public void ReuseTemplateTest()
    {
        //temp\sttest is empty dir, just for test
        var stg = new TemplateRawGroupDirectory("c:\\temp\\sttest", '$', '$');

        var tpl = new Template(stg, "$model.Name$ $model.Title$");
        var mdl = new Test { Name = "zzzyzx", Title = "mmm" };
        var t0 = tpl.CreateShadow();
        t0.Add("model", mdl);
        var txt0 = t0.Render();
        Assert.IsTrue(txt0.Contains(mdl.Title) && txt0.Contains(mdl.Name));

        var t1 = tpl.CreateShadow();
        t1.Add("model", mdl);
        var txt1 = t1.Render();
        Assert.IsTrue(txt1.Contains(mdl.Title) && txt1.Contains(mdl.Name));
    }

`

lafar6502 avatar May 23 '17 06:05 lafar6502

Update - possible workaround is to use TemplateGroup.CreateStringTemplateInternally(tpl.impl) for cloning the template have no idea if this is the correct approach

lafar6502 avatar May 23 '17 07:05 lafar6502