lituancheng
V2EX  ›  2019

请问有没有可以逆向解析的模板引擎

  •  
  •   lituancheng · Jul 30, 2019 · 2658 views
    This topic created in 2481 days ago, the information mentioned may be changed or developed.

    常规的模板引擎是传入 template 和 params,得到 content,我想要根据 content 和 template 得到 params,有什么好办法吗?

    7 replies    2019-07-31 09:17:47 +08:00
    zhaishunqi
        1
    zhaishunqi  
       Jul 30, 2019
    正则
    lituancheng
        2
    lituancheng  
    OP
       Jul 30, 2019
    @zhaishunqi 正则的话只能处理 value 为 String 的,如果 value 是 List 类型的,就没法处理了啊
    DsuineGP
        3
    DsuineGP  
       Jul 30, 2019
    @lituancheng 正则捕获组
    lituancheng
        4
    lituancheng  
    OP
       Jul 30, 2019
    lituancheng
        5
    lituancheng  
    OP
       Jul 30, 2019
    @DsuineGP
    假设 content 为:
    姓名:张三 年龄:20
    姓名:李四 年龄:21
    姓名:王五 年龄:22
    template 为:
    <#list personList as person>
    姓名:${person.name} 年龄:${person.age}
    </#list>

    以上这种的没办法根据正则得到原始 personList 的,关键点在于 template 不知道 list 的实际条数
    DsuineGP
        6
    DsuineGP  
       Jul 31, 2019
    @lituancheng 为啥不写 for 循环再用正则匹配呢
    DsuineGP
        7
    DsuineGP  
       Jul 31, 2019
    @lituancheng 如果是模版渲染的是 HTML 还有一些 dom 解析的框架可以用,你这个渲染出来的是文本,只能一行一行的正则匹配
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   931 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 563ms · UTC 19:37 · PVG 03:37 · LAX 12:37 · JFK 15:37
    ♥ Do have faith in what you're doing.