GithubHelp home page GithubHelp logo

jiniannet / jntemplate Goto Github PK

View Code? Open in Web Editor NEW
141.0 17.0 42.0 6.38 MB

jntemplate is an fast and lightweight html templating engine for C# .NET.

Home Page: https://docs-en.jiniannet.com

License: MIT License

C# 96.50% CSS 1.59% HTML 1.39% Batchfile 0.24% Shell 0.10% PowerShell 0.18% Smalltalk 0.01%
template-engine template html dotnet-core string-template templates templating templating-engine rendering-engines

jntemplate's Introduction

JNTemplate

Build status GitHub stars GitHub stars GitHub license GitHub issues

English | 中文

What is JNTemplate?

JNTemplate is fast, lightweight, extensible .net template engine for generating html, xml, sql, or any other formatted text output.

Special placeholders in the template allow writing code similar to c# syntax. Then the template is passed data to render the final document.

Installation

Install and update using NuGet:

PM> Install-Package JinianNet.JNTemplate

or

> dotnet add package JinianNet.JNTemplate

Quickstart

Basics

Rendering a basic html template with a predefined data model.

c# code

var template = Engine.LoadTemplate(@"c:\wwwroot\view\index.html"); ;
template.Set("name", "jntemplate");
var result = template.Render(); 

index.html

<!DOCTYPE html>
<html>
<body>
  <h1>Hello, ${name}</h1>
</body>
</html>

output:

<!DOCTYPE html>
<html>
<body>
  <h1>Hello, jntemplate</h1>
</body>
</html>

Iteration

Iteration is achieved by using the foreach binding on the element you wish to iterate.

c# code

var template = Engine.LoadTemplate(@"c:\wwwroot\view\view.html"); ;
template.Set("list", new string[] { "github","jntemplate" });
var result = template.Render(); 

view.html

<ul>
${foreach(name in list)}
	<li>${name}</li>
${end}
</ul>

output:

<ul>
	<li>github</li>
	<li>jntemplate</li>
</ul>

Configuration

You can configure JNTemplate with the IOptions class.

Engine.Configure((options)=>{
// .. configure your instance
});

Links

Licenses

MIT

jntemplate's People

Contributors

jiniannet avatar zhou-zhi-peng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jntemplate's Issues

IsLocalPath方法问题

JinianNet.JNTemplate\Resources\FileLoadProvider.cs中这个IsLocalPath方法好像有问题,除非路径为空,否则永远不是本地路径

JNTemplate比微软官方的好在哪里

有什么资料可以证明你的模板引擎比 Razer高吗?
1.易用:上手快,有一定c#基础或者js基础的,只要10分钟就可以学会使用,极大降低学习成本
2.轻量:代码精简,功能不减! 结构清晰,只有最核心的代码, 能适应于绝大部分开发,也能很方便的进行二次开发
3.开源:授权自由,可用于商业目的
4.拓展:引擎可以很方便的过行拓展,甚至可以在不改变原有代码的基础上,很自由的支持解析各种自定义格式的标签

微软最新版的asp.net mvc6 在易用性上要比JNTemplate远远好很多吧,这个不用说了。现在asp.ent vnext 己经把整个framework重写了,各个模块完全独立出来,在实际的开发过程中只需按需引用即可,也变的很轻量级,而且完全开源(https://github.com/aspnet/Mvc)。我真的不知道JNTemplate有什么优势,我找不到一个理由来使用他。

自定义标签

能举个获取 变量 的最终值的例子嘛?
比如我有个 传入的 表信息 变量
{
"tableName":"",
"tableDesc":"",
"params":[{
"colName":"",
"dataType":""
}]
}
我如何在 自定义标签的实现中获取呢?如果有能直接继承的就更好了

ThrowExceptions 貌似不起作用

以前版本,在遇到不认识得 tag 时,渲染为空。不会抛错,而 2.0.1 新版本倒有这个问题。测试代码:

static void Main(string[] args)
{
    Engine.Configure(c =>
    {
        c.ThrowExceptions = false;  //<------
        c.IgnoreCase = true;
    });
    var template = Engine.CreateTemplate("hello $model.Name, $model.name, $model.aa"); //<--- 这里会抛错
    template.Set("model", new User
    {
        Name = "name at here"
    });
    var result = template.Render();
    Console.WriteLine(result);
}
public class User
{
    public string Name { get; set; }
}

错误堆栈:

   at JinianNet.JNTemplate.Compile.TypeGuess.<Initialize>b__2_6(ITag tag, CompileContext ctx)
   at JinianNet.JNTemplate.Compile.TypeGuess.GetType(String name, ITag tag, CompileContext ctx)
   at JinianNet.JNTemplate.Compile.TypeGuess.GetType(ITag tag, CompileContext ctx)
   at JinianNet.JNTemplate.Compile.CompileBuilder.<Initialize>b__20_0(ITag tag, CompileContext c)
   at JinianNet.JNTemplate.Compile.CompileBuilder.GetCompileMethod(String name, ITag tag, CompileContext context)
   at JinianNet.JNTemplate.Compile.CompileBuilder.GetCompileMethod(ITag tag, CompileContext scope)
   at JinianNet.JNTemplate.Compile.CompileBuilder.DefineRender(ITag tag, CompileContext context, Type tagType)
   at JinianNet.JNTemplate.Compile.CompileBuilder.<GeneralDefaultRender>b__9_0(ITag tag, CompileContext scope)
   at JinianNet.JNTemplate.Compile.Compiler.Compile(ITag[] tags, CompileContext ctx)
   at JinianNet.JNTemplate.Compile.Compiler.Compile(String content, CompileContext ctx)
   at JinianNet.JNTemplate.Compile.Compiler.Compile(String name, String content, Action`1 action)
   at JinianNet.JNTemplate.CompileTemplate.Render(TextWriter writer, TemplateContext context)
   at JinianNet.JNTemplate.CompileTemplate.Render(TextWriter writer)
   at JinianNet.JNTemplate.TemplateExtensions.Render(ITemplate template)
   at ConsoleAppForTemplateEngine.Program.Main(String[] args) in C:\Users\MSI-NB\source\repos\WebApplicationDotnetCore\ConsoleAppForTemplateEngine\Program.cs:line 22

Pass array or anonymous variable in static method

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...

ITemplate.Set<T>(string key, T value) value为匿名对象时,无法成功赋值

NUGET 安装版本:2.3.3
发现匿名对象无发成功赋值:

var templateContent = "hello $model.Name";
var template = Engine.CreateTemplate(templateContent);
template.Set("model",object匿名对象);
var result = template.Render();
输出 ”hello “

需要进行强制转换才可成功替换标签
template.Set("model",(SiteInfo)object匿名对象);

尝试使用JNTemple做代码生成器(js代码)的时候,发现C#的bool会转换成 True 或者Flase 字符串,而不是想要的 true或者 false .请问有什么方法解决呢?

示例代码:

            var templateContent = @"${3!=8}  ${3==8} 
            ${bool.ToString().ToLower()} ${boolStr} ";
            var template = Engine.CreateTemplate(templateContent);
            template.Set("bool", true);
            template.Set("boolStr", true.ToString().ToLower());
            var result = template.Render();
            Console.WriteLine(result);

尝试过的方法:

  1. [有效]给模板赋值前使用 true.ToString().ToLower()); 进行转换。
  2. [报错]自定义bool的扩展方法。
    namespace ExtensionMethods
    {
        public static class MyExtensions
        {        
            public static string ToLower(this bool b)
            {
                return b.ToString().ToLower();
            }
        }
    }
  1. [太菜,未能实现]自定义标签。

请问可以解析css文件里的资源路径吗

在有些css文件中,比较图片路径,有时候需要使用动态解析的地址,请问如何实现。现在的解决办法是,必需把有路径的样式复制到html文件中才能正常解析。期待你的回复

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.