建立你的first Chat app - Part 3: Creating and inserting templates
In the previous tutorials in the series, you created the starter files for an app named Learn More and modified the HTML file that's iframed into the chat interface in Zendesk Chat. The HTML file so far displays only the title and footer of your app. In this tutorial, you'll create a template to display a form to search for articles on Wikipedia. You'll also create a template to display an error message in case there's a problem retrieving the data.
Templates are typically the building blocks of an app's user interface. They define different states, or views, for an application. For example, one template can contains an HTML form for entering search terms, and another template can dynamically list the search results.
You can use any templating engine -- and whatever version -- you like with your apps. For this project, you decide to useHandlebars.js, one of the most popular templating libraries.
Import the Handlebars library in your app by inserting the followingtag iniframe.htmlbeforethetag that references yourmain.jsfile:
If you reload the chat window in your browser, the app doesn't change because you haven't inserted the template in the HTML yet. The next step is to insert the template and then confirm that it works.
Inserting the template in your app
You insert templates into an element of your choice in the document. You add a
tag in your HTML file for inserting templates at runtime. Only one template at a time can be inserted, but you can switch to a different template. For example, you can start by displaying the splash page and then switch to another page after the user clicks a button.
Iniframe.htmlfile, add the following
tag after thetag but before thetag:
<divid="content">div>
Inmain.js,添加以下函数调用(强调)房颤ter the resize statement in the self-invoking function:
The first statement in the function references thetag iniframe.htmlthat contains the template. The second statement compiles the template. The third line generates the HTML from the template and uses it to set the HTML content of your
tag from step 1.
Save both files and reload the chat window in your browser. If your ZCLI server is not already started, see "Testing the app" in part 2 of this series for instructions.
Handlebars sets the HTML content of the
tag in theiframe.htmlfile:
Creating a template for error messages
If the app encounters a problem retrieving the data, you want the user to get an error message about what went wrong. The MediaWiki API returns error data structured as follows:
"error":{"code":"codestring","info":"info text"}
wherecodestringis a short error code andinfo textis a more detailed description of the error. SeeErrors and Warningsin the MediaWiki API documentation.
To create an error template
Iniframe.html, add the following error template after your search template:
<scriptid="error-template"type="text/x-handlebars-template"><div><p style="color: red;"><strong>Somethingwent wrong.</strong></p><p><strong>Errorcode</strong>:{{code}}</p><p><strong>Info</strong>:{{info}}</p><p>Pleasereport a bug at the link below or<a id="reset-link"href="">reset</a>the form.</p></div>script>
The template contains two Handlebars expressions –{{code}}and{{info}}. A Handlebars expression typically consists of a variable inside double curly braces. When the app runs in Zendesk Chat, the expressions are replaced by values supplied by a JavaScript data object, otherwise known as acontext.
Inmain.js, add the following function to compile and insert the template in theiframe.htmlfile:
In the first part of the function, theerror_dataobject defines the values, or context, to the Handlebars template. You'll replace the hard-coded values later with real-time data.
The second part passes the error data to the template and renders the HTML.
Call the function (highlighted) from your self-invoking function:
To test it, comment outshowSearchForm();with double forward-slashes, save both files, and reload the chat window in your browser.
Handlebars inserts the values of yourerror_dataobject in the template and generates the final HTML, which the app then sets as the HTML content of the
tag in theiframe.htmlfile.
Once you're satisfied the error template works as intended, delete theshowError();statement in the self-invoking function. You'll call the function from a different location in the next tutorial.
Uncomment theshowSearchForm;statement by removing the double forward-slashes before it.
If you're moving to the next tutorial, you can leave the ZCLI server running. If you're done for this session, you can shut down the server by switching to your command-line interface and pressing Control+C.
In this tutorial, you created templates to display a search form and an error message. You also learned how to insert the templates into your app. In the next tutorial, you learn how to get and display articles from the MediaWiki API. Continue toPart 4 - Getting data.