Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW: apply a global context for all rendered template(optional) #4

Merged
merged 1 commit into from
Sep 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module.exports = function(grunt) {
options: {
template_path: 'test/fixtures/templates',
context_path: 'test/fixtures/context',
global_context_file: 'test/fixtures/context/global.json'
},
files:[{
expand: true,
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ the src path (relative to template_path) + context_path should be the path of .j

NOTE: the .json file must be the same name as your template name, eg: `index.html` will use the context of `index.json` as its context.

#### options.global_context_file (optional)
Tpye: `String`
Default value: `null`

When provide this option like this: `"global_context_file" : "my_global_context.json"`. The context of `my_global_context.json` will apply to all rendered tempates.


### Usage Examples

Expand Down Expand Up @@ -81,6 +87,7 @@ grunt.initConfig({
options: {
template_path: 'test/fixtures/templates',
context_path: 'test/fixtures/context',
global_context_file : "test/fixures/global.json"
},
files:[{
expand: true,
Expand Down
7 changes: 5 additions & 2 deletions bin/jinja2_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


from docopt import docopt
import jinja2, json, sys
import jinja2, json, sys, re

reload(sys)
sys.setdefaultencoding('utf-8')
Expand All @@ -27,7 +27,10 @@ def render(template_file_path=None, data_file_path=None, base_path=None):
env = jinja2.Environment(loader=jinja2.FileSystemLoader(base_path))
template = env.get_template(template_file_path)
if data_file_path:
context = json.loads(open(data_file_path).read())
file_paht_list = re.split(',\s*',data_file_path)
context = {}
for filePath in file_paht_list:
context = dict((json.loads(open(filePath).read())).items()+context.items())
else:
context = {}
print template.render(context)
Expand Down
11 changes: 10 additions & 1 deletion tasks/jinja2.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,19 @@ module.exports = function(grunt) {
var context_path = options.context_path + template_path.replace(/\.\w+$/,'.json')
//test context file
var use_context = grunt.file.exists(context_path) && grunt.file.readJSON(context_path)
//global context file
var global_context_file = options.global_context_file

var args = ['-t', template_path, '-b', options.template_path]
if (use_context){
if (use_context && global_context_file){
var combinedPath = context_path + "," + global_context_file
// args.push('-d', combinedPath)
args.push('-d', combinedPath)
console.log("combinedPath", combinedPath);
} else if(use_context) {
args.push('-d', context_path)
} else if(global_context_file) {
args.push('-d', global_context_file)
}
// grunt.log.writeln(template_path, context_path, args);
//run python command
Expand Down
6 changes: 5 additions & 1 deletion test/expected/albums.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
456
lastName

userID
userID


userID
value of global var1
3 changes: 2 additions & 1 deletion test/expected/no_context.html
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
This is a test file.
This is a test file.
value of global var1
8 changes: 8 additions & 0 deletions test/fixtures/context/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"photographer": {
"user_id": "userID_from_global.json"
},
"globalVar": {
"var1": "value of global var1"
}
}
5 changes: 5 additions & 0 deletions test/fixtures/templates/albums.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ link
{% block content %}
{{photographer.user_id}}
{% endblock %}

{% block globalContext %}
{{photographer.user_id}}
{{globalVar.var1}}
{% endblock %}
3 changes: 2 additions & 1 deletion test/fixtures/templates/module/base.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ html
{% block reference %}{% endblock %}
456
{% include 'module/header.tpl' %}
{% block content %}{% endblock %}
{% block content %}{% endblock %}
{% block globalContext %}{% endblock %}
3 changes: 2 additions & 1 deletion test/fixtures/templates/no_context.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
This is a test {% if 2 > 1 %}file.{% endif %}
This is a test {% if 2 > 1 %}file.{% endif %}
{{globalVar.var1}}