JavaScript 프런트엔드 템플릿 엔진 프레임워크 artTemplate 사용 요약 - CSDN 블로그
artTemplate은 Tencent의 오픈 소스 프런트 엔드 템플릿 프레임워크입니다. 콧수염 및 핸들바와 유사하며 웹 프로젝트에서 쉽게 사용할 수 있으며 시작도 빠릅니다. .
학습 과정:
1. 구문 소개:
데이터 바인딩: 뷰와 모델이 단방향 바인딩이라는 점을 제외하면 뷰는 변경되지만 그 반대는 아닙니다.
<script id="tpl1" type="text/template"> <h1 id="data-nbsp-mapping-nbsp-example">1、data mapping example</h1> <h2 id="message">{{message}}</h2> </script> //js中使用模板渲染 var data1 = {message:"hello,artTemplate is a javasript framework."}; $("node1").innerHTML = template("tpl1",data1);
조건 판단: 여기서는 단일 if가 지원되며 else 분기도 추가할 수 있습니다.
{{if isShow}} <h3 id="满足条件展示消息-message">(2、满足条件展示消息:{{message}}</h3> {{else}} <h3 id="x-条件不满足-展示默认消息">(2x、条件不满足,展示默认消息</h3> {{/if}}
컬렉션 탐색:
{{each list as item index}} <h3 id="the-nbsp-index-nbsp-of-nbsp-message-nbsp-is-nbsp-nbsp-index-nbsp-nbsp-item">the index of message is : {{index+1}} -> {{item}}</h3> {{/each}}
보조 기능: 1->normal, 0->error와 같이 백엔드에서 요청한 데이터를 매핑하는 데 사용할 수 있습니다. 이를 사용할 때는 {{message | filterhandler}}와 같이 표현식 뒤에 "|func"만 전달하면 됩니다. 여기서 filterhandler는 사용자 정의 보조 함수입니다.
먼저 보조 함수를 정의합니다. 여기서 정의하는 것은 간단한 날짜 형식 변환 함수입니다.
template.helper("date2str",function(date){ var today = new Date(date); var year = today.getFullYear(); var month = today.getMonth()+1; if(month<10)month = "0"+month; var day = today.getDate(); if(day<10)day = "0"+day; return year+"-"+month+"-"+day; });
보조 기능 사용
<p id="node4"></p> <script id="tpl4" type="text/template"> <h1 id="template-helper-nbsp-func-nbsp-example">4、template.helper func example</h1> <h3 id="today-nbsp-is-nbsp-datenow-nbsp-nbsp-date-str">today is {{datenow | date2str}}</h3> </script> //js代码中调用 var data4 = {datenow:new Date()}; $("node4").innerHTML = template("tpl4",data4);
사전 컴파일: 템플릿 사용과 달리 사전 컴파일에는 문자열 형식의 문서 조각이 필요하며, 렌더링을 위해 데이터를 사전 컴파일된 템플릿에 전달합니다.
var tpl5 = "<h1 id="compile-nbsp-example">5、compile example</h1><h3 id="this-nbsp-is-nbsp-a-nbsp-string-nbsp-the-nbsp-type-nbsp-is-nbsp-not-nbsp-type">this is a string the type is not {{type}}</h3>"; $("node5").innerHTML = template.compile(tpl5)({type:"text/template"});
인용 하위 템플릿:
<p id="node6"></p> <script id="tpl6" type="text/template"> <h1 id="include-nbsp-child-nbsp-template-nbsp-example">6、include child template example</h1> <p class="parenttemplate"> <h3 id="parent-nbsp-template">parent template</h3> {{include 'tpl6-child'}} </p> </script> <script id="tpl6-child" type="text/template"> <p class="childtemplate"> <h3 id="child-nbsp-template">child template</h3> </p> </script>
2. template.js 라이브러리를 다운로드하여 html 파일에 도입합니다.
3. 다음은 앞서 소개한 구문 중 일부를 연습할 수 있는 포괄적인 예입니다.
<!doctype html> <html> <head> <meta charset="UTF-8"/> <title>artTemplate example</title> <style type="text/css"> *{margin:0;} h1,h2,h3{margin:3px;} h2,h3{text-indent:20px;} .parenttemplate{background:#ccc;width:600px;height:60px;} .childtemplate{background:lightblue;} </style> <script type="text/javascript" src="template.js"></script> <script> function $(id){return document.getElementById(id);} window.onload = function(){ //data mapping var data1 = {message:"hello,artTemplate is a javasript framework."}; $("node1").innerHTML = template("tpl1",data1); //if condition var data2 = {isShow:true,message:"hello,template"}; $("node2").innerHTML = template("tpl2",data2); data2.isShow = false; $("node2x").innerHTML = template("tpl2",data2); //list foreach var data3 = {list:["Javascript","JQuery","artTemplate"]}; $("node3").innerHTML = template("tpl3",data3); //helper function template.helper("date2str",function(date){ var today = new Date(date); var year = today.getFullYear(); var month = today.getMonth()+1; if(month<10)month = "0"+month; var day = today.getDate(); if(day<10)day = "0"+day; return year+"-"+month+"-"+day; }); var data4 = {datenow:new Date()}; $("node4").innerHTML = template("tpl4",data4); //compile example var tpl5 = "<h1 id="compile-nbsp-example">5、compile example</h1><h3>this is a string the type is not {{type}} </h3>"; $("node5").innerHTML = template.compile(tpl5)({type:"text/template"}); $("node6").innerHTML = template("tpl6",{}); //escape html $("node7").innerHTML = template("tpl7",{message:"<span>escape html tag</span>"}); } </script> </head> <body> <p id="node1"></p> <script id="tpl1" type="text/template"> <h1 id="data-nbsp-mapping-nbsp-example">1、data mapping example</h1> <h2 id="message">{{message}}</h2> </script> <p id="node2"></p> <p id="node2x"></p> <script id="tpl2" type="text/template"> <h1 id="if-nbsp-condition-nbsp-example">2、if condition example</h1> {{if isShow}} <h3 id="满足条件展示消息-message">(2、满足条件展示消息:{{message}}</h3> {{else}} <h3 id="x-条件不满足-展示默认消息">(2x、条件不满足,展示默认消息</h3> {{/if}} </script> <p id="node3"></p> <script id="tpl3" type="text/template"> <h1 id="list-nbsp-example">3、list example</h1> {{each list as item index}} <h3 id="the-nbsp-index-nbsp-of-nbsp-message-nbsp-is-nbsp-nbsp-index-nbsp-nbsp-item">the index of message is : {{index+1}} -> {{item}}</h3> {{/each}} </script> <p id="node4"></p> <script id="tpl4" type="text/template"> <h1 id="template-helper-nbsp-func-nbsp-example">4、template.helper func example</h1> <h3 id="today-nbsp-is-nbsp-datenow-nbsp-nbsp-date-str">today is {{datenow | date2str}}</h3> </script> <p id="node5"></p> <p id="node6"></p> <script id="tpl6" type="text/template"> <h1 id="include-nbsp-child-nbsp-template-nbsp-example">6、include child template example</h1> <p class="parenttemplate"> <h3 id="parent-nbsp-template">parent template</h3> {{include 'tpl6-child'}} </p> </script> <script id="tpl6-child" type="text/template"> <p class="childtemplate"> <h3 id="child-nbsp-template">child template</h3> </p> </script> <p id="node7"></p> <script id="tpl7" type="text/template"> <h1 id="escape-nbsp-html-nbsp-tag-nbsp-example">7、escape html tag example</h1> <h3 id="origin-nbsp-expression-nbsp-nbsp-message">origin expression : {{#message}}</h3> <h3 id="after-nbsp-escape-nbsp-nbsp-nbsp-message">after escape ==> : {{message}}</h3> </script> </body> </html>
이 예를 실행해 보세요. 다음 효과를 얻을 수 있습니다:
위 내용은 JavaScript 프런트엔드 템플릿 엔진 프레임워크 artTemplate 사용 요약 - CSDN 블로그의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











기사는 JavaScript 라이브러리 작성, 게시 및 유지 관리, 계획, 개발, 테스트, 문서 및 홍보 전략에 중점을 둡니다.

이 기사는 브라우저에서 JavaScript 성능을 최적화하기위한 전략에 대해 설명하고 실행 시간을 줄이고 페이지로드 속도에 미치는 영향을 최소화하는 데 중점을 둡니다.

프론트 엔드 개발시 프론트 엔드 열지대 티켓 인쇄를위한 자주 묻는 질문과 솔루션, 티켓 인쇄는 일반적인 요구 사항입니다. 그러나 많은 개발자들이 구현하고 있습니다 ...

이 기사는 브라우저 개발자 도구를 사용하여 효과적인 JavaScript 디버깅, 중단 점 설정, 콘솔 사용 및 성능 분석에 중점을 둡니다.

기술 및 산업 요구에 따라 Python 및 JavaScript 개발자에 대한 절대 급여는 없습니다. 1. 파이썬은 데이터 과학 및 기계 학습에서 더 많은 비용을 지불 할 수 있습니다. 2. JavaScript는 프론트 엔드 및 풀 스택 개발에 큰 수요가 있으며 급여도 상당합니다. 3. 영향 요인에는 경험, 지리적 위치, 회사 규모 및 특정 기술이 포함됩니다.

이 기사는 소스 맵을 사용하여 원래 코드에 다시 매핑하여 미니어링 된 JavaScript를 디버그하는 방법을 설명합니다. 소스 맵 활성화, 브레이크 포인트 설정 및 Chrome Devtools 및 Webpack과 같은 도구 사용에 대해 설명합니다.

Console.log 출력의 차이의 근본 원인에 대한 심층적 인 논의. 이 기사에서는 Console.log 함수의 출력 결과의 차이점을 코드에서 분석하고 그에 따른 이유를 설명합니다. � ...

동일한 ID로 배열 요소를 JavaScript의 하나의 객체로 병합하는 방법은 무엇입니까? 데이터를 처리 할 때 종종 동일한 ID를 가질 필요가 있습니다 ...
