Week 3: Adding content to websites

Screenshot 2016-09-25 22.56.31.png

](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference)

Learning outcomes:
By the end of this module, you will be able to:

  1. Separate page structure from page content
  2. Use JavaScript objects
  3. Implement JavaScript templates with Handlebars
  4. Display different data with the same template
  5. Display the same data with different templates
  6. Use a Bootstrap modal

Separating structure and content

When the structure and content mixed together it will cause a lot of work when your what to change the structure.
For example, i wanna my website includes 1000 images -> and it should be like DO HTML ONCE, and then BRING 1000 INDIVIDUELL IMAGES, so that can work, otherwise will it be horrible.


Website with structure AND content

结构:<h1 id="title"> ... </h1> -> same for all image
内容:The Earth seen from Apollo 117 ->unique to each image

JavaScript objects

How can we separate the content out of HTML?
We've actually done that one with VARIABLES.
-> 1) set the variable(JavaScript): Wrap text-variables in quotes var image1Name="1mage 1"
-> 2) use the variable(jQuery): put the text into the website
$("#title").html(image1Name)
***Summary: HTML=STRUCTURE, CSS=STYLE, VARIABLE=CONTENT ***

What we need, is a tool called JavaScript:


JavaScript : definition + using

if we think an a variable as a box, then a object can be seen as a collection of box.


JavaScript: object

Note: JavaScript does not have classes, it just has objects

JavaScript templates with Handlebars

JavaScript is a little bit difference from other languages. But it is really powerful tool for creating structured content, and that really what we need if we want to do the separation thing between structure and content.
-> Now we are going to find out, how can we use those content in our html file

content should be putted into the structure

HTML template: filled those holes using content from a JavaScript objects.
But, javascript do not support template, so we need to use an external library to create these templates, i'm gonna use one called Handlebars.

example of using Handlebars

--- explains ---
<script>...</script> it exists inside a script tag, just like our JavaScript
type="text/x-handlebars-template" says this is a template, NOT JavaScript
{{title}}, {{author}}, {{src}} these are the really important things -> They are placeholders, called template expressions, the holes we talked about.
change the code with templates

Putting data into templates

Turn our template text and our data into actual HTML code:

how did it work

JavaScript code with extra lib jQuery

(1) get the text of the template out of the DOM, so we can use jQuery to do that. Grabbing the HTML out of the script tag it was in.
(2) Then we compile the template. So we call handlebars.compile, that's a function. We parse in the source, which is the text of the template, and we get out this variable template, which is this JavaScript code, which will actually turn objects into HTML.
COMPILING:is the process of taking Handlebars template code and turning it into javaScript that can generate HTML
TEMPLATES: generate HTML that can then be put into the web page
(3) Then the next step is to take the data, which is your object, put it through the template. So that's a function call, we're calling the template function in passing in data. And then what that gives us is some HTML code.
(4) use jQuery again to copy back the HTML into the DOM of our content element.
JQUERY:is the library you use for interacting with the DOM

--------- Demo -----------
Attention:在使用Handlebars之前,要在html文件头部添加<script src="../js/handlebars-v3.0.3.js"></script>

Displaying different data with the same template

HTML中的head部分

----- we want to have: same HTML + same template +different data ----
For example we have hier two pics:


content we want to show

Code:


Code

Displaying different data with the different

----- same data, different templates: (1) normal showing (2) pop-up view -----

show same pic in two different ways

below is a modul from bootstrap, if you wanna see more, go to the bootstrap-website and you will find more!
HTML part from a modul from Bootstrap

$("#imageModal").modal('show'); -> Show the modal
$('#imageModal').on('hidden.bs.modal', function(){ $("#detailsbtn").click(); }) -> DO STH. when you click the foto. Bind a function to an event. jQuery .on( ) function is used to handle all kinds of events. $('#detailsBtn').click( ) is called to automatically click the details button and show the image detail view.

Attention: The rendered template HTML is only "real" once we call jQuery's html( ) function to add it to the DOM.
A modal: refers to a dialog box that appears in front of the main interface

Summary


jQuery: jQuery的是一套跨浏览器的JavaScript的函式库,简化HTML与JavaScript的之间的操作。全球前万个访问最高的网站中,有65%使用了jQuery的,是目前最受欢迎的JavaScript的函式库

The name Handlebar come from an other library called moustache. And someone think the curly brackets look just like moustache.{ }

符号等等:
curly brackets: 花括号
semicolon: 双引号
koma: 逗号
point: 点
hash simbol: #

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容