2014-12-11 6 views
2

템플릿 사용을 위해 내 웹 세트와 마코를 구성하기 위해 flask-assets을 사용하고 싶습니다.플라스크 - 마코와 함께 플라스크 - 자산 사용

:

% assets 'coffee': 
     <script type="text/javascript" src="{{ ASSET_URL }}"></script> 
    % endassets 

그러나 이것은 컴파일 오류가 발생합니다 다음과 같은 것이다 (내가 아는 한)

{% assets "js_all" %} 
    <script type="text/javascript" src="{{ ASSET_URL }}"></script> 
{% endassets %} 

마코에 해당 : 플라스크-자산은 일반적으로 다음과 같은 방법으로 신사를 사용

mako.exceptions.CompileException 
CompileException: Unsupported control keyword: 'assets' in file '/index.html' at line: 8 char: 1 

Mako에서 맞춤 자산 컨트롤 키워드 (예 : '애셋')를 사용할 수 있습니까?

import os 
from flask import Flask, render_template 
from flask.ext import assets 
from flask import config 
from flask.ext.mako import MakoTemplates 
from flask.ext.mako import render_template 

app = Flask(__name__) 
app.config['ASSETS_DEBUG'] = True 

mako = MakoTemplates(app) 
env = assets.Environment(app) 

# Tell flask-assets where to look for our coffeescript and sass files. 
env.load_path = [ 
    os.path.join(os.path.dirname(__file__), 'js'), 
    os.path.join(os.path.dirname(__file__), 'styles'), 
] 

coffee = assets.Bundle('**/*.coffee', filters='coffeescript', output="app.js") 
env.register('coffee', coffee) 

@app.route("/") 
def index(): 
    return render_template('index.html', name='mako') 


if __name__ == "__main__": 
    app.run(debug=True) 

답변

2

글쎄, 나는 해결책을 내놓았다. 환경을 기본 템플릿으로 가져 와서 작업 할 수 있습니다.

assets.py :

import os 
from flask.ext import assets 
from app import app 

env = assets.Environment(app) 

# Tell flask-assets where to look for our coffeescript and sass files. 
env.load_path = [ 
    os.path.join(os.path.dirname(__file__), 'js'), 
    os.path.join(os.path.dirname(__file__), 'styles'), 
] 

coffee = assets.Bundle('**/*.coffee', filters='coffeescript', output="app.js") 
env.register('coffee', coffee) 

템플릿 :

<%! 
    from assets import env 
%> 

<!doctype html> 

<html> 
    <head> 
     <title>Hello Flask</title> 
    <head> 
    <body> 
     <h1>Hello Flask</h1> 

     % for url in env['coffee'].urls(): 
      <script src="${url}"></script> 
     % endfor 

    </body> 
</html> 

것은 거기 MAKO_IMPORTS의 구성 매개 변수를 사용하여 가져 오기를 피하는 방법이 될 수도 있지만 나는 그것을 연주하지 않았습니다.