templateContext: improve the context object idea

This commit is contained in:
Marcin Lulek 2016-07-07 15:33:43 +02:00
parent 50f344ce72
commit f8716e4525
3 changed files with 36 additions and 52 deletions

View file

@ -640,8 +640,8 @@ class PullrequestsController(BaseRepoController):
pull_request_id = safe_int(pull_request_id)
c.pull_request = PullRequest.get_or_404(pull_request_id)
if hasattr(c, 'pylons_dispatch_info'):
c.pylons_dispatch_info['extra']['pull_request'] = pull_request_id
c.template_context['pull_request_data']['pull_request_id'] = \
pull_request_id
# pull_requests repo_name we opened it against
# ie. target_repo must match

View file

@ -425,9 +425,29 @@ class BaseController(WSGIController):
_route_name = '.'.join([environ['pylons.routes_dict']['controller'],
environ['pylons.routes_dict']['action']])
c.pylons_dispatch_info = {
'controller': environ['pylons.routes_dict']['controller'],
'action': environ['pylons.routes_dict']['action'],
c.template_context = {
'repo_name': None,
'repo_type': None,
'repo_landing_commit': None,
'rhodecode_user': {
'username': None,
'email': None,
},
'visual': {
'default_renderer': None
},
'commit_data': {
'commit_id': None
},
'pull_request_data': {'pull_request_id': None},
'timeago': {
'refresh_time': 120 * 1000,
'cutoff_limit': 1000*60*60*24*7
},
'pylons_dispatch':{
'controller': environ['pylons.routes_dict']['controller'],
'action': environ['pylons.routes_dict']['action'],
},
'extra': {'plugins': {}}
}

View file

@ -1,55 +1,19 @@
## -*- coding: utf-8 -*-
<!DOCTYPE html>
<%def name="get_template_context()" filter="n, trim">{
<%
c.template_context['repo_name'] = getattr(c, 'repo_name', '')
## repo data
repo_name: "${getattr(c, 'repo_name', '')}",
% if hasattr(c, 'rhodecode_db_repo'):
repo_type: "${c.rhodecode_db_repo.repo_type}",
repo_landing_commit: "${c.rhodecode_db_repo.landing_rev[1]}",
% else:
repo_type: null,
repo_landing_commit: null,
% endif
if hasattr(c, 'rhodecode_db_repo'):
c.template_context['repo_type'] = c.rhodecode_db_repo.repo_type
c.template_context['repo_landing_commit'] = c.rhodecode_db_repo.landing_rev[1]
## user data
% if getattr(c, 'rhodecode_user', None) and c.rhodecode_user.user_id:
rhodecode_user: {
username: "${c.rhodecode_user.username}",
email: "${c.rhodecode_user.email}",
},
% else:
rhodecode_user: {
username: null,
email: null,
},
% endif
if getattr(c, 'rhodecode_user', None) and c.rhodecode_user.user_id:
c.template_context['rhodecode_user']['username'] = c.rhodecode_user.username
c.template_context['rhodecode_user']['email'] = c.rhodecode_user.email
## visual settings
visual: {
default_renderer: "${h.get_visual_attr(c, 'default_renderer')}"
},
## current commit context, filled inside templates that expose that
commit_data: {
commit_id: null,
},
## current pr context, filled inside templates that expose that
pull_request_data: {
pull_request_id: null,
},
## timeago settings, can be overwritten by custom user settings later
timeago: {
refresh_time: ${120 * 1000},
cutoff_limit: ${1000*60*60*24*7}
},
dispatch_info: ${h.json.dumps(getattr(c, 'pylons_dispatch_info', {}))|n}
}
</%def>
c.template_context['visual']['default_renderer'] = h.get_visual_attr(c, 'default_renderer')
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
@ -81,7 +45,7 @@
<script src="${h.url('/js/rhodecode/i18n/%s.js' % c.language, ver=c.rhodecode_version_hash)}"></script>
<script type="text/javascript">
// register templateContext to pass template variables to JS
var templateContext = ${get_template_context()};
var templateContext = ${h.json.dumps(c.template_context)|n};
var REPO_NAME = "${getattr(c, 'repo_name', '')}";
%if hasattr(c, 'rhodecode_db_repo'):