Pass array or anonymous variable in static method
Andres128 opened this issue · 2 comments
Hello friend, thanks you for hard work and great template engine.
I have this code:
public class Article
{
public int ID { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string Tag { get; set; }
public string ImgUrl { get; set; }
}
public class ParseHtml
{
public static string Img(string src, object attrs)
{
//<img src="http://test.com/img1.jpg" class=".img" title="Title" alt="Altitle">
return string.Format("<img src=\"{0}\" {1}>", src, string.Join(" ", attrs.GetType.GetProperties.Select(x => string.Format("{0}=\"{1}\"", x.Name, x.GetValue(attrs).ToString))));
}
}
Now i create a example of code:
var R = new List<Article>();
R.Add(new Article() { ID = 1, Title = "What is a computer", Content = "My new computer.", Tag = "Tecnology", ImgUrl = "http://test.com/img1.jpg" });
R.Add(new Article() { ID = 2, Title = "What is a printer", Content = "My new printer.", Tag = "World", ImgUrl = "http://test.com/img2.jpg" });
var template = Engine.LoadTemplate(@"template.txt");
template.Set("Articles", R);
template.SetStaticType("ParseHtml", typeof(ParseHtml));
template.Render(Console.Out);
My [template.txt]:
${foreach(Article in Articles)}
<article id="${Article.ID}">
<h1>${Article.Title}</h1>
${ParseHtml.Img(Article.ImgUrl, [HERE-PROBLEM])}
<div>${Article.Content}</div>
</article>
${end}
I have a problem, is possible in a future add this new feature:
I need passed a variable of type Array of Anonymous in a static method: ParseHtml.Img(Article.ImgUrl, Array or Anonymous)
Example my objective is create a image in html with attributes: <img src="http://test.com/img1.jpg" class=".img" title="Title" alt="Altitle">
Anonymous Types: [Property1 = Value1, Property2 = Value2, ...]
Variations:
[class = ".img", title = "Title", alt = "Altitle"]
[class = ".img", title = Article.Title, alt = Article.Title]
[class = ".img", title = Article.Title, alt = "Aditional text: " + Article.Title]
[class = ".img", title = Article.Title, alt = Article.Tag + " " + " Aditional text: " + Article.Title]
Array string or array of different object: [Value1, Value2, ...]
Variations:
[".img", "Title", "Altitle"]
[1, "Title", true]
[".img", "Title", Article.Title]
[".img", "Title", Article.Tag + " " + " Aditional text: " + Article.Title]
Example of use of new feature:
${ParseHtml.Img(Article.ImgUrl, [class = ".img", title = "Title", alt = "Altitle"])}
Print:
<img src="http://test.com/img1.jpg" class=".img" title="Title" alt="Altitle">
OR when is necessary use Array:
${ParseHtml.Img(Article.ImgUrl, [".img", "Title", "Altitle"])}
Thanks you for your time brother and greetings...
my friend, thank you for your suggestions, we will consider adding arrays in the future, but there is no plan to increase anonymous types
See tag: v2.3.0