美文网首页
2021-11-17 dart模版解析mustache

2021-11-17 dart模版解析mustache

作者: 我是小胡胡分胡 | 来源:发表于2021-11-18 08:19 被阅读0次

    dart实现:
    https://github.com/valotas/mustache4dart

    import 'package:mustache/mustache.dart' as mustache;
    

    使用实例:

      final List<Map<String, dynamic>> pluginConfigs = <Map<String, dynamic>>[];
      for (MagpiePluginInfo p in plugins) {
        pluginConfigs.add(<String, dynamic>{
          'name': p.name,
          'prefix': p.iosPrefix,
          'class': p.pluginClass,
        });
      }
      final Map<String, dynamic> context = <String, dynamic>{
        'os': 'ios',
        'deploymentTarget': '8.0',
        'framework': 'Flutter',
        'plugins': pluginConfigs,
      };
      final String template =
          mustache.Template(sourceTemplate, htmlEscapeValues: false)
              .renderString(context);
    
    

    sourceTemplate内容如下:

    //
    //  Generated file. Do not edit.
    //
    
    #import "GeneratedPluginRegistrant.h"
    
    {{#plugins}}
    #if __has_include(<{{name}}/{{class}}.h>)
    #import <{{name}}/{{class}}.h>
    #else
    @import {{name}};
    #endif
    {{/plugins}}
    
    @implementation GeneratedPluginRegistrant
    
    + (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
    {{#plugins}}
      [{{prefix}}{{class}} registerWithRegistrar:[registry registrarForPlugin:@"{{prefix}}{{class}}"]];
    {{/plugins}}
    }
    
    @end
    
    
    

    就是将模版里面里prefix,class,name用数据填充替换。
    代表数组:
    {{#plugins}}
    {{/plugins}}
    代表某个值
    {{name}};

    一一替换。得到输出结果:

    
    //
    //  Generated file. Do not edit.
    //
    
    #import "GeneratedPluginRegistrant.h"
    
    #if __has_include(<device_info/FLTDeviceInfoPlugin.h>)
    #import <device_info/FLTDeviceInfoPlugin.h>
    #else
    @import device_info;
    #endif
    #if __has_include(<fluttertoast_renameuiviewtoast/FluttertoastPlugin.h>)
    #import <fluttertoast_renameuiviewtoast/FluttertoastPlugin.h>
    #else
    @import fluttertoast_renameuiviewtoast;
    #endif
    #if __has_include(<fluwx_no_pay/FluwxPlugin.h>)
    #import <fluwx_no_pay/FluwxPlugin.h>
    #else
    @import fluwx_no_pay;
    #endif
    #if __has_include(<media_upload/MediaUploadPlugin.h>)
    #import <media_upload/MediaUploadPlugin.h>
    #else
    @import media_upload;
    #endif
    #if __has_include(<path_provider/FLTPathProviderPlugin.h>)
    #import <path_provider/FLTPathProviderPlugin.h>
    #else
    @import path_provider;
    #endif
    #if __has_include(<shared_preferences/FLTSharedPreferencesPlugin.h>)
    #import <shared_preferences/FLTSharedPreferencesPlugin.h>
    #else
    @import shared_preferences;
    #endif
    #if __has_include(<sqflite/SqflitePlugin.h>)
    #import <sqflite/SqflitePlugin.h>
    #else
    @import sqflite;
    #endif
    #if __has_include(<url_launcher/FLTURLLauncherPlugin.h>)
    #import <url_launcher/FLTURLLauncherPlugin.h>
    #else
    @import url_launcher;
    #endif
    #if __has_include(<wuba_bugly_plugin/WubaBuglyPlugin.h>)
    #import <wuba_bugly_plugin/WubaBuglyPlugin.h>
    #else
    @import wuba_bugly_plugin;
    #endif
    
    @implementation GeneratedPluginRegistrant
    
    + (void)registerWithRegistry:(NSObject<FlutterPluginRegistry>*)registry {
      [FLTDeviceInfoPlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTDeviceInfoPlugin"]];
      [FluttertoastPlugin registerWithRegistrar:[registry registrarForPlugin:@"FluttertoastPlugin"]];
      [FluwxPlugin registerWithRegistrar:[registry registrarForPlugin:@"FluwxPlugin"]];
      [MediaUploadPlugin registerWithRegistrar:[registry registrarForPlugin:@"MediaUploadPlugin"]];
      [FLTPathProviderPlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTPathProviderPlugin"]];
      [FLTSharedPreferencesPlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTSharedPreferencesPlugin"]];
      [SqflitePlugin registerWithRegistrar:[registry registrarForPlugin:@"SqflitePlugin"]];
      [FLTURLLauncherPlugin registerWithRegistrar:[registry registrarForPlugin:@"FLTURLLauncherPlugin"]];
      [WubaBuglyPlugin registerWithRegistrar:[registry registrarForPlugin:@"WubaBuglyPlugin"]];
    }
    
    @end
    
    

    大概原理跟js的同类库一样
    https://www.jianshu.com/p/7f1cecdc27e1

    Mustache 是一款经典的前端模板引擎,在前后端分离的技术架构下面,前端模板引擎是一种可以被考虑的技术选型,随着重型框架(AngularJS、ReactJS、Vue)的流行,前端的模板技术已经成为了某种形式上的标配,Mustache 的价值在于其稳定和经典.

    相关文章

      网友评论

          本文标题:2021-11-17 dart模版解析mustache

          本文链接:https://www.haomeiwen.com/subject/rzdrtrtx.html