V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lituancheng
V2EX  ›  2019

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

  •  
  •   lituancheng · 2019-07-30 11:39:08 +08:00 · 1862 次点击
    这是一个创建于 1704 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    以上这种的没办法根据正则得到原始 personList 的,关键点在于 template 不知道 list 的实际条数
    DsuineGP
        6
    DsuineGP  
       2019-07-31 09:14:05 +08:00
    @lituancheng 为啥不写 for 循环再用正则匹配呢
    DsuineGP
        7
    DsuineGP  
       2019-07-31 09:17:47 +08:00
    @lituancheng 如果是模版渲染的是 HTML 还有一些 dom 解析的框架可以用,你这个渲染出来的是文本,只能一行一行的正则匹配
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2761 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 11:59 · PVG 19:59 · LAX 04:59 · JFK 07:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.