14.Apache中的模板引擎

(01)

var http = require('http')
var fs = require('fs')
 var template = require('art-template')
var server = http.createServer('request',function(req,res){
var url = req.url
var wwwDir = 'D:/www'
fs.readFile('./template.html',function(err,data){
   if(err){
     return res.end('404')
    }
   fs.readdir(wwwDir,function(err,files){
     if(err){
       return res.end('can not find dir')
     }
      var html = template.render(data.toString(),{
         title:'haha'
      }) //然后去template.html 文件中编写模板语法
   })
 })
})
server.listen(3000,function(){
console.log('服务器已启动')
})

(02) template.html 中添加

{{ each files }}
    <tr><th><font color="white" size="2">{{ $value }}</font></th><tr> 
 {{/each}}

 js文件中添加

files : files

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要引入 Apache Velocity 的相关依赖。在 Maven 项目,可以在 `pom.xml` 文件添加以下依赖: ```xml <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.3.1</version> </dependency> ``` 然后,可以编写一个 Velocity 模板文件,用来生成 `pom.xml` 文件。例如: ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>$groupId</groupId> <artifactId>$artifactId</artifactId> <version>$version</version> <dependencies> #foreach($dependency in $dependencies) <dependency> <groupId>${dependency.groupId}</groupId> <artifactId>${dependency.artifactId}</artifactId> <version>${dependency.version}</version> </dependency> #end </dependencies> </project> ``` 在模板文件,可以使用 Velocity 的语法,通过变量和循环生成 `pom.xml` 文件的内容。例如,可以定义 `$groupId`、`$artifactId`、`$version` 和 `$dependencies` 等变量,分别表示项目的组ID、项目ID、版本号和依赖列表。在 `$dependencies` 变量,使用 `#foreach` 循环遍历依赖列表,生成多个 `<dependency>` 标签。 最后,可以编写一个 Java ,使用 Velocity 引擎加载模板文件,并传入变量值,生成 `pom.xml` 文件。例如: ```java import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; public class PomGenerator { public static void main(String[] args) throws Exception { // 初始化 Velocity 引擎 VelocityEngine ve = new VelocityEngine(); ve.init(); // 加载模板文件 Template t = ve.getTemplate("pom_template.vm"); // 设置变量值 VelocityContext ctx = new VelocityContext(); ctx.put("groupId", "com.example"); ctx.put("artifactId", "example-project"); ctx.put("version", "1.0.0"); List<Dependency> dependencies = new ArrayList<>(); dependencies.add(new Dependency("org.springframework", "spring-core", "5.2.0.RELEASE")); dependencies.add(new Dependency("org.springframework", "spring-web", "5.2.0.RELEASE")); ctx.put("dependencies", dependencies); // 生成输出文件 FileWriter writer = new FileWriter(new File("pom.xml")); t.merge(ctx, writer); writer.close(); } static class Dependency { String groupId; String artifactId; String version; Dependency(String groupId, String artifactId, String version) { this.groupId = groupId; this.artifactId = artifactId; this.version = version; } } } ``` 在代码,首先初始化 Velocity 引擎,然后加载模板文件 `pom_template.vm`。接着,设置变量值,包括 `$groupId`、`$artifactId`、`$version` 和 `$dependencies` 等。最后,使用 Velocity 引擎的 `merge` 方法,将模板和变量合并,并将输出写入文件。这样就可以生成 `pom.xml` 文件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值