SNBinderã«ç®ããã¦ãã³ å°ããªMVCãä»ç¾å®ã«
ã¿ã¤ãã«ã¯ã¡ãã£ã¨å¤§ããããã§ããããããã«ãã«ã§ãã
Webã¢ããªã®MVCãã¿ã§ãæ¯åº¦åæãã¾ããã®satoshiããããJSã®ã©ã¤ãã©ãªSNBinderãå
¬éããã¦ãã¾ãã
Life is beautiful: JavaScript HTMLテンプレートエンジン SNBinder 公開
BODYã¿ã°ãªã³ãªã¼å¤§å¥½ãã£åãªã®ã§ãããã¯ä½¿ããã«ã¯å±
ãããªãã¨æãæ©éãã£ã¦ã¿ã¾ããã
ã¤ãã§ã«ãæãå¤ããgithubでForkãã¦readmeã®å訳ãªã©ãã¦ãã¾ãã
ã³ã¼ããµã³ãã«ãå ã«åããã¦ã¿ããã§ããè²ã ã¨åããªãã»ã»ã»
ç§ã®ç解ãã¹ãªæ°ãããã®ã§ãããããã³ãããããã¨ã³ã¼ãæãã¾ãããªãªã¸ãã«ã®ã¾ã¾åãã¦ã人ãããã©ããéããæãã¦ãã ããã¾ãã
SNBinderã®ä½¿ãæ¹ã¨ãã¦ãREADMEã«æ¸ããã¦ããããæ¹ã¯ãï¼ç¨®é¡ããã¾ãã
- å¤æ°ä¸ã¤ããã³ãã¬ã¼ãã«åãè¾¼ã
- SNBinder.bind(template, hash)
- é
åããä¸ã¤ãã¤æãåºããã³ãã¬ã¼ããè¤æ°å±éãå¤æ°ãåãè¾¼ã
- SNBinder.bind_rowset(template, hash)
- å¤é¨ãããã³ãã¬ã¼ããèªã¿è¾¼ã¿å¤æ°ãåãè¾¼ã
- SNBinder.get_named_sections(url, callback)
- å¤é¨ãããã³ãã¬ã¼ããèªã¿è¾¼ã¿ãå¤é¨ã¨ã³ããã¤ã³ãããåå¾ããJSONãã¼ã¿ãåãè¾¼ã
- SNBinder.get(url, params, isJson, callback, options) ã¨ä¸è¨çµã¿åãã
ç¹ã«3,4ã®å¤é¨ãããã³ãã¬ã¼ããèªã¿è¾¼ãã¨ãããããã ã¼ï¼ã£ã¦æãã§ãã
NSBinderã¯ãDOMè¦ç´ ãã¨ã«ããã³ãã¬ã¼ããã¡ã¤ã«ããé¨åãåãåºãå®å
¨ã«JSã®ã³ã³ããã¼ã«ä¸ã§HTMLãã¼ããé
ç½®ãã¦ããã¾ããä¾ãã¦ããã¨ããã¬ããããå¿
è¦ãªè²ãåãåºããã£ã³ãã¹ã彩ã油絵ã®å¦ãããã³ãã¬ã¼ããã¡ã¤ã«å
ããå¿
è¦ãªé¨åãåãåºãå¿
è¦ãªå ´æã«åºãããæ¶ããããã¦ç»é¢ãæ§ç¯ãã¾ãã
JSå ã«ãã³ãã¬ã¼ã(HTML)è¦ç´ ãæ··ãããã¨ããªããããã³ãã¬ã¼ã(HTML)å ã«JSï¼ãã¸ãã¯ï¼ãæ··ãããã¨ããªããé座ããããã®ã³ã³ã½ã¡ã¹ã¼ãã®ãã¨ãä¸åæ··ããç©ãªãã§ã½ã¼ã¹ãã¯ãªã¼ã³ãªç¶æ ã«ä¿ã¤ãã¨ãåºæ¥ããã
NSBinderã¨flaskã§ãã¹ããã¦ã¿ã¾ããã
Flaskãµã¼ãã¼ãµã¤ã
ã¾ãã¯,Flaskã®main.py
#!/usr/bin/env python # coding:utf-8 from flask import Flask, render_template, jsonify from datetime import datetime app = Flask(__name__) app.debug = True @app.route('/') def index(): p = {} return render_template('index.html', p = p) ### API ### @app.route('/api/get/time') def api_index(): hash = {'now':str(datetime.now())} return jsonify(hash) app.run(host='0.0.0.0', port=9091)
'/' 㨠'/api/get/time' ã®ï¼ã¤ã®ã¨ã³ããã¤ã³ãã ããããªãã·ã³ãã«ãªãµã¼ãã¼
'/'ã«ã¢ã¯ã»ã¹ãããã¨index.htmlãè¿ãã¾ãã
ããã¼ã§ä½ã£ã'/api/get/time'ã¯ãã·ã³ãã«ãªjsonãè¿ãã¾ãã
{ "now": "2011-02-01 23:25:20.120097" }
HTMLçãã©ã¦ã¶ãµã¤ã
htmlãã¡ã¤ã«ã¯ãï¼ã¤ã®JSãèªã¿è¾¼ã¿ã¾ãã
- jquery-1.5.min.js (åºãã°ã£ããï¼ï¼
- snbinder-0.5.3.js ï¼ä»åã®ä¸»å½¹ï¼
- index.js (å®è£ ãã¡ã¤ã«ï¼
ããã¦ãindex.jsãã¡ã¤ã«ããéçãã¡ã¤ã«ã®ãã³ãã¬ã¼ããèªã¿è¾¼ã¿ã¾ã
- template.html
Flaskã®ä½æ³ã«å¾ãåãã¡ã¤ã«ã®é ç½®ã¯ä»¥ä¸ã®ããã«ãªãã¾ã(brewã§å ¥ããtreeã ãã©ãªããè¦é£ãæ°ããã)
âââ main.py âââ static ââââ css ââââ js âââââ index.js âââââ lib âââââ jquery-1.5.min.js âââââ snbinder-0.5.3.js ââââ template.html âââ templates âââ index.html
ã§ã¯ãåã½ã¼ã¹
ã¾ãçºç«¯ã¨ãªãHTMLãã¡ã¤ã«
index.html
<!DOCTYPE HTML> <html lang="ja"> <head> <meta charset="UTF-8"> <title></title> <script src="/static/js/lib/jquery-1.5.min.js" type="text/javascript"></script> <script src="/static/js/lib/snbinder-0.5.3.js" type="text/javascript"></script> <script src="/static/js/index.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() { SNBinder.init({}); rend_item(); rend_items(); static_data(); get_data(); }); </script> </head> <body> <h1>SNBinder test</h1> <div id="item"></div> <div id="items"></div> <div id="from_file"></div> <div id="time"></div> </body> </html>
ãããã¼ã®JSå
ã§ãSNBinderãåæåãã¦ï¼ã¤ã¡ã½ããå¼ã³åºãã¦ãã ã
ããã£ã¼ã®ä¸ã«ãä¸è¨ï¼ã¤ã®ã¡ã½ãããããããåãè¾¼ãå
ã®DIVã¿ã°ãåã¤
以ä¸ï¼
åèãªã®ã§ãdivã¿ã°å ¥ãããã©ãbodyæå®ããã°ã³ã¬ãããè¦ããªããbut ã·ã³ãã«isãã¹ã
ããã¦ãä»åã®ä¸»å½¹ã®JSãã¡ã¤ã«
index.js
// å¤æ°ä¸ã¤ããã³ãã¬ã¼ãã«åãè¾¼ã function rend_item(){ var template = "<p>Hello $(.name)!</p>"; var user = { "name":"Leonardo da Vinci" }; $('#item').html(SNBinder.bind(template, user)); } // é åããã³ãã¬ã¼ãã«å¾ãè¤æ°åãè¾¼ã function rend_items(){ var template = "<li>Hello $(.name)!</li>"; var users = [ { "name":"Leonardo da Vinci" }, { "name":"Michelangelo di Lodovico Buonarroti Simoni" }, { "name":"LDonato di Niccol di Betto Bard" } ]; $('#items').html(SNBinder.bind_rowset(template, users)); } // å¤é¨ãã³ãã¬ã¼ããã¡ã¤ã«ãèªã¿è¾¼ã¿å¤æ°ãåãè¾¼ã function static_data(){ SNBinder.get_named_sections("/static/template.html", '', function(templates) { var user = { "name":"Leonardo da Vinci" }; $('#from_file').html(SNBinder.bind(templates.tmpl_name, user)); }); } // å¤é¨ãã³ãã¬ã¼ããã¡ã¤ã«ãèªã¿è¾¼ã¿ãå¥ã®URLããJsonãåå¾ããã¼ã¿ãåãè¾¼ã function get_data(){ SNBinder.get_named_sections("/static/template.html", '', function(templates) { var req_url = '/api/get/time'; console.info(templates); SNBinder.get(req_url, '', true, function(json) { $('#time').html(SNBinder.bind(templates.tmpl_time, json)); }); }); };
ä¸ããé çªã«ãã·ã³ãã«ãªããæ¹ããè¤éãªããæ¹ã«ä¸¦ãã§ãããå®éå©ç¨ãéã«ã¯ãä¸çªä¸ã®å¤ãããã³ãã¬ã¼ããã¡ã¤ã«ãèªã¿è¾¼ã¿ã¤ã¤éåæã«åå¾ããJSONãé£ããã¦ã¬ã³ããªã³ã°ãããã¿ã¼ã³ãå¤ç¨ãããã¨ã«ãªããä¸ã®äºã¤ã¯ä¸ã®æ®µéç説æã®ããã«æãã®ã§ãã£ã¦ãç¹ã«ï¼çªä¸ã®å¥´ã ããå©ç¨ããã±ã¼ã¹ã¯ãªãã¨æãï¼jQueryã ãã§ãã£ãæ¹ãããã¨æãï¼
ã§ãå½±ã®ç«å½¹è ä¸ã¤æãéè¦ãªé¨å template.html
SNBinderã§ãã£ã¨ãé¢å¿ããã¨ããã®template.htmlããã®ãã¡ã¤ã«ã«ã¯ããã¼ã¸å
ã®è¦ç´ ãããªããã£ããªè¦ç´ ã¾ã§å解ããä¸ã¤ãã¤ããã©ãã©ã«è¨è¼ããã¦ãããããã¦ãJSå´ã§è¡¨ç¤ºããããDOMã«å¯¾ãã¦bindã¡ã½ããã§ããªããã£ããªè¦ç´ ãå¼ã³åºãçªã£è¾¼ãã§ã¬ã³ããªã³ã°ããã
ããã®ãã³ãã¬ã¼ããå«ãJSãå
¨ã¦ã¯ã©ã¤ã¢ã³ããµã¤ãã§åããã¨ã«ãªãã®ã§ããµã¼ãã¼ä¸ã®ãã¬ã¼ã ã¯ã¼ã¯ã§æ¯é±ã®ããã«è¨ããã¦ãããã©ã®ãã³ãã¬ã¼ãã¨ã³ã¸ã³ãéãã®ãï¼ãã¨ãã£ã話é¡ã«å
¨ãå½±é¿ãåããäºããªããããã«ãHTMLã³ã¼ãã¨ä½ãã¬ã³ããªã³ã°ãã¹ããèããã³ã³ããã¼ã«ã®JSãã綺éºã«åå²ããã¦ãããã³ã¬ãã¾ãæ°æã¡è¯ãã
template.html
{%}tmpl_name{%} <p>name is $(.name).</p> {%}tmpl_time{%} <p>time is $(.now).</p>
ãã®ãã³ãã¬ã¼ãå
㧠{%}ã§å²ã¾ããé¨åããã³ãã¬ã¼ãã®é¨åã®ååã§ã次ã®ååãå§ã¾ãã¾ã§ãä¸åºåã
è¦ããã«ãã®ãã³ãã¬ã¼ãã«ã¯ï¼ç¨®é¡ã®é¨åããããtmpl_nameã¨tmpl_timeã¨ããäºã¤ã®é¨åãããã
ä¸è¨ã³ã¼ãã§ã¯ãããªããã£ãã®ã®ã£ã©ãªã¼æãå°ãªãã®ã§ãå®ésatoshiãããããã¢ã§ä½æããã
ãµã¤ã http://www.fruence.com/ ã®ãã³ãã¬ã¼ãé¨åãã¨ã¦ãåèã«ãªããhttp://www.fruence.com/static/templates.htm ã«ã¢ã¯ã»ã¹ãããã¨ã§é¨åãç¾
åããã¦ããã®ã
ããåãããã¨æããããããã¼ããJSãé©å®ã¤ãã³ãã«å¿ãã¦å¿
è¦ãªã¨ããã«å¿
è¦ãªã¿ã¤ãã³ã°ã§æ¿ãè¾¼ã
å®è¡ã¨ã
ããã¦ããããã®ãã¡ã¤ã«ãä½æãããããã«
python main.py open http://localhost:9091/ #osxã®å ´å
ã¨ãã©ã¦ã¶ã§ã¢ã¯ã»ã¹ããã¨
SNBinder test Hello Leonardo da Vinci! Hello Leonardo da Vinci! Hello Michelangelo di Lodovico Buonarroti Simoni! Hello LDonato di Niccol di Betto Bard! name is Leonardo da Vinci. time is 2011-02-01 23:48:24.264479.
ã¨è¡¨ç¤ºããããè¦ãç®ãå¤ããããã°ãtemplate.htmlãããã°è¯ãã
ä»ã®ãµã³ãã«ããªã³ã¯
- LBã®ããã°ã³ã¡ã³ãã«ãµã³ãã«ãä½æããã¦ããæ¹ãå± ãã®ã§åèãªã³ã¯
- SNBinderã®githubãªãã¸ããª
- ä»åã®ãµã³ãã«ã³ã¼ãã®ãªãã¸ããª
以åã大ããªMVCã¨å°ããªMVCã¨ããã¨ã³ããªã¼ãæ¸ãããã¨ããã£ããã¢ã¤ãã¢ã¯ãããããããªãããå¿ãããªããæ¯åJSãå®è£ ãã¦ããã¤ããã ã£ããã©ãæãè¾¼ã¿ã ã£ãæãå¼·ããä»åãsatoshiãããSNBinderãå ¬éããã¦ãhtmlã骨çµã¿ã¨ããããªããã£ããªãã¼ãã®ï¼ç¨®é¡ã«åãããã¨ã§ããVCãæ確ã«åé¢ãããäºãè身ã§æãããã¨ãåºæ¥ããè¨ãã ãã¨ãã£ã¦è¦ããéãã¯å¤§ãããã¨ï¼æ¹ãã¦çæããã
ä¹ ãã¶ãã«é·ãããã°ãæ¸ãããç²ããããª
PS:å¸°å® éä¸ã«NEX5è½ã¨ãã¦レンズ割れたã®ãçãããªã»ã»ã»ã»