1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // This provides some helper methods for building and rendering an 6 // internal html page. The flow is as follows: 7 // - instantiate a builder given a webframe that we're going to render content 8 // into 9 // - load the template html and load the jstemplate javascript into the frame 10 // - given a json data object, run the jstemplate javascript which fills in 11 // template values 12 13 #ifndef UI_WEBUI_JSTEMPLATE_BUILDER_H_ 14 #define UI_WEBUI_JSTEMPLATE_BUILDER_H_ 15 16 #include <string> 17 18 #include "base/strings/string_piece.h" 19 #include "ui/base/ui_export.h" 20 21 namespace base { 22 class DictionaryValue; 23 } 24 25 namespace webui { 26 27 // While an object of this class is in scope, the template builder will output 28 // version 2 html. Version 2 uses load_time_data.js and i18n_template2.js, and 29 // should soon become the default. 30 class UI_EXPORT UseVersion2 { 31 public: 32 UseVersion2(); 33 ~UseVersion2(); 34 35 private: 36 DISALLOW_COPY_AND_ASSIGN(UseVersion2); 37 }; 38 39 // A helper function that generates a string of HTML to be loaded. The 40 // string includes the HTML and the javascript code necessary to generate the 41 // full page with support for JsTemplates. 42 UI_EXPORT std::string GetTemplateHtml(const base::StringPiece& html_template, 43 const base::DictionaryValue* json, 44 const base::StringPiece& template_id); 45 46 // A helper function that generates a string of HTML to be loaded. The 47 // string includes the HTML and the javascript code necessary to generate the 48 // full page with support for i18n Templates. 49 UI_EXPORT std::string GetI18nTemplateHtml( 50 const base::StringPiece& html_template, 51 const base::DictionaryValue* json); 52 53 // A helper function that generates a string of HTML to be loaded. The 54 // string includes the HTML and the javascript code necessary to generate the 55 // full page with support for both i18n Templates and JsTemplates. 56 UI_EXPORT std::string GetTemplatesHtml(const base::StringPiece& html_template, 57 const base::DictionaryValue* json, 58 const base::StringPiece& template_id); 59 60 // The following functions build up the different parts that the above 61 // templates use. 62 63 // Appends a script tag with a variable name |templateData| that has the JSON 64 // assigned to it. 65 UI_EXPORT void AppendJsonHtml(const base::DictionaryValue* json, 66 std::string* output); 67 68 // Same as AppendJsonHtml(), except does not include the <script></script> 69 // tag wrappers. 70 UI_EXPORT void AppendJsonJS(const base::DictionaryValue* json, 71 std::string* output); 72 73 // Appends the source for JsTemplates in a script tag. 74 UI_EXPORT void AppendJsTemplateSourceHtml(std::string* output); 75 76 // Appends the code that processes the JsTemplate with the JSON. You should 77 // call AppendJsTemplateSourceHtml and AppendJsonHtml before calling this. 78 UI_EXPORT void AppendJsTemplateProcessHtml(const base::StringPiece& template_id, 79 std::string* output); 80 81 // Appends the source for i18n Templates in a script tag. 82 UI_EXPORT void AppendI18nTemplateSourceHtml(std::string* output); 83 84 // Appends the code that processes the i18n Template with the JSON. You 85 // should call AppendJsTemplateSourceHtml and AppendJsonHtml before calling 86 // this. 87 UI_EXPORT void AppendI18nTemplateProcessHtml(std::string* output); 88 89 } // namespace webui 90 91 #endif // UI_WEBUI_JSTEMPLATE_BUILDER_H_ 92