grunt中ejs-static配置

本文介绍如何使用Grunt插件ejs-static来编译EJS模板生成静态HTML页面,并通过配置数据文件和路由JSON来实现动态内容的加载。包括配置方法一与方法二,以及如何在EJS模板中引用JSON数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ejs-static github地址: https://github.com/shaekuronen/grunt-ejs-static

Gruntfile.js中添加ejs-static:

ejs_static: require('./tasks/ejs_static')(grunt)


配置方法一:

ejs_static.js

module.exports=function(grunt){
  var config={
    dest: 'app/html',
    path_to_data: 'data/data.json',
    path_to_layouts: 'app/assets/html/',
    index_page: 'index',
    parent_dirs: false,
    underscores_to_dashes: true,
    file_extension: '.html'
  }
	return {
		preview: {
			options: config
		},
    release:{
			options: grunt.util._.merge({ }, config, {
          dest:'<%= yeoman.dist %>/html'
        })  
    }
	}	
}


data.json

{
  "index": {
    "meta": {
      "title": "Home",
      "description": "Compile static html from ejs templates",
      "keywords": "gruntplugin, ejs, static html, static site generator, templates, templating engine, template, embedded js"
    }
  },

  "about": {
    "path_to_layout": "app/assets/html/about.ejs",
    "meta": {
      "title": "About",
      "description": "Compile static html from ejs templates",
      "keywords": "gruntplugin, ejs, static html, static site generator, templates, templating engine, template, embedded js"
    }
  }
}

ejs模板页中引用方法(对应各文件名的meta):<%= meta.title %>


配置方法二:

ejs_static.js

module.exports=function(grunt){
  var config={
    dest: 'app/html',
    path_to_data: 'data/routes.json',
    path_to_layouts: 'app/assets/html/',
    index_page: 'index',
    parent_dirs: false,
    underscores_to_dashes: true,
    file_extension: '.html'
  }
	return {
		preview: {
			options: config
		},
    release:{
			options: grunt.util._.merge({ }, config, {
          dest:'<%= yeoman.dist %>/html'
        })  
    }
	}	
}

routes.json

{
  "home": {
    "path_to_layout": "app/assets/html/index.ejs",
    "path_to_data": [
      "data/global.json",
      {"data/meta.json": "home"}
    ]
  },
  "a": {
    "path_to_layout": "app/assets/html/about.ejs",
    "path_to_data": [
      "data/global.json",
      "data/a.json"
    ]
  },
  "b": {
    "path_to_layout": "app/assets/html/about.ejs",
    "path_to_data": "data/a.json"
  }
}


meta.json

{
  "home": {
    "title": "Grunt EJS Static | Home",
    "description": "The demo page for Grunt EJS Static",
    "keywords": "gruntplugin, ejs, static html, static site generator, templates, templating engine, template, embedded js"
  }
}

global.json

{
  "site_name": "Grunt EJS Static",
  "google_analytics": "XOXOXO"
}

a.json

{
    "title": "About",
    "description": "Compile static html from ejs templates",
    "keywords": "gruntplugin, ejs, static html, static site generator, templates, templating engine, template, embedded js"
}


ejs模板页中引用:<%= global.site_name%>   <%= meta.title%>    <%= a.title %>
注意:global.site_name中的global对应的是该path_to_data载入的json文件名


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值