diff --git a/modules/web/html/html_template_data.cpp b/modules/web/html/html_template_data.cpp
index eb2762a1c..4d7d1d8d2 100644
--- a/modules/web/html/html_template_data.cpp
+++ b/modules/web/html/html_template_data.cpp
@@ -31,6 +31,136 @@
#include "html_template_data.h"
+#include "core/os/file_access.h"
+
+bool HTMLTemplateData::has_template(const StringName &p_name) const {
+ return _templates.has(p_name);
+}
+String HTMLTemplateData::get_template(const StringName &p_name) const {
+ const String *val = _templates.getptr(p_name);
+
+ if (!val) {
+ return String();
+ }
+
+ return *val;
+}
+void HTMLTemplateData::set_template(const StringName &p_name, const String &p_value) {
+ _templates[p_name] = p_value;
+}
+void HTMLTemplateData::remove_template(const StringName &p_name) {
+ _templates.erase(p_name);
+}
+
+Dictionary HTMLTemplateData::get_templates() const {
+ Dictionary ret;
+
+ for (const HashMap::Element *E = _templates.front(); E; E = E->next) {
+ ret[E->key()] = E->value();
+ }
+
+ return ret;
+}
+void HTMLTemplateData::set_templates(const Dictionary &p_dict) {
+}
+
+HashMap HTMLTemplateData::get_templates_map() const {
+ return _templates;
+}
+void HTMLTemplateData::set_templates_map(const HashMap &p_map) {
+ _templates = p_map;
+}
+
+void HTMLTemplateData::clear() {
+ _templates.clear();
+}
+
+Error HTMLTemplateData::load_from_file(const String &p_file) {
+ clear();
+
+ Error err;
+ FileAccess *f = FileAccess::open(p_file, FileAccess::READ, &err);
+
+ if (err != OK) {
+ if (f) {
+ memdelete(f);
+ }
+
+ return err;
+ }
+
+ String data = f->get_as_utf8_string(true);
+
+ f->close();
+ memdelete(f);
+
+ load_from_string(data);
+
+ return OK;
+}
+Error HTMLTemplateData::save_to_file(const String &p_file) const {
+ Error err;
+ FileAccess *f = FileAccess::open(p_file, FileAccess::WRITE, &err);
+
+ if (err != OK) {
+ if (f) {
+ memdelete(f);
+ }
+
+ return err;
+ }
+
+ String data = save_as_string();
+
+ f->store_string(data);
+ f->close();
+
+ memdelete(f);
+
+ return OK;
+}
+void HTMLTemplateData::load_from_string(const String &p_data) {
+ clear();
+
+ Vector lines = p_data.split("\n", false);
+
+ String current_section_name;
+ String current_str;
+ for (int i = 0; i < lines.size(); ++i) {
+ // Section header: [ Section Name ]
+ // Should not have whitespace in front and back
+ String l = lines[i];
+
+ if (l.begins_with("[") && l.ends_with("]")) {
+ if (!current_section_name.empty()) {
+ _templates[current_section_name] = current_str;
+ }
+
+ // Remove [ and ], and strip it.
+ current_section_name = l.substr_index(1, l.length() - 1).strip_edges();
+ current_str = "";
+ continue;
+ }
+
+ current_str += l + "\n";
+ }
+
+ if (!current_section_name.empty()) {
+ _templates[current_section_name] = current_str;
+ }
+}
+String HTMLTemplateData::save_as_string() const {
+ String data;
+
+ for (const HashMap::Element *E = _templates.front(); E; E = E->next) {
+ data += "[ " + E->key() + " ]\n\n";
+ data += E->value();
+ data += "\n\n";
+ }
+
+ return data;
+}
+
HTMLTemplateData::HTMLTemplateData() {
}
@@ -38,9 +168,20 @@ HTMLTemplateData::~HTMLTemplateData() {
}
void HTMLTemplateData::_bind_methods() {
- //ClassDB::bind_method(D_METHOD("get_item_count"), &HTMLTemplateData::get_item_count);
- //ClassDB::bind_method(D_METHOD("set_item_count", "val"), &HTMLTemplateData::set_item_count);
- //ADD_PROPERTY(PropertyInfo(Variant::INT, "item_count"), "set_item_count", "get_item_count");
+ ClassDB::bind_method(D_METHOD("has_template", "name"), &HTMLTemplateData::has_template);
+ ClassDB::bind_method(D_METHOD("get_template", "name"), &HTMLTemplateData::get_template);
+ ClassDB::bind_method(D_METHOD("set_template", "name", "value"), &HTMLTemplateData::set_template);
+ ClassDB::bind_method(D_METHOD("remove_template", "name"), &HTMLTemplateData::remove_template);
- //BIND_VMETHOD(MethodInfo("_render_indexed", PropertyInfo(Variant::OBJECT, "target", PROPERTY_HINT_RESOURCE_TYPE, "HTMLTemplateData"), PropertyInfo(Variant::INT, "page_index")));
+ ClassDB::bind_method(D_METHOD("get_templates"), &HTMLTemplateData::get_templates);
+ ClassDB::bind_method(D_METHOD("set_templates", "dict"), &HTMLTemplateData::set_templates);
+ ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "templates"), "set_templates", "get_templates");
+
+ ClassDB::bind_method(D_METHOD("clear"), &HTMLTemplateData::clear);
+
+ ClassDB::bind_method(D_METHOD("load_from_file", "file"), &HTMLTemplateData::load_from_file);
+ ClassDB::bind_method(D_METHOD("save_to_file", "file"), &HTMLTemplateData::save_to_file);
+
+ ClassDB::bind_method(D_METHOD("load_from_string", "data"), &HTMLTemplateData::load_from_string);
+ ClassDB::bind_method(D_METHOD("save_as_string"), &HTMLTemplateData::save_as_string);
}
diff --git a/modules/web/html/html_template_data.h b/modules/web/html/html_template_data.h
index 307dc15bf..5441a0082 100644
--- a/modules/web/html/html_template_data.h
+++ b/modules/web/html/html_template_data.h
@@ -32,17 +32,43 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
+#include "core/containers/hash_map.h"
+#include "core/string/string_name.h"
+#include "core/string/ustring.h"
+#include "core/variant/dictionary.h"
+
#include "core/object/resource.h"
class HTMLTemplateData : public Resource {
GDCLASS(HTMLTemplateData, Resource);
public:
+ bool has_template(const StringName &p_name) const;
+ String get_template(const StringName &p_name) const;
+ void set_template(const StringName &p_name, const String &p_value);
+ void remove_template(const StringName &p_name);
+
+ Dictionary get_templates() const;
+ void set_templates(const Dictionary &p_dict);
+
+ HashMap get_templates_map() const;
+ void set_templates_map(const HashMap &p_map);
+
+ void clear();
+
+ Error load_from_file(const String &p_file);
+ Error save_to_file(const String &p_file) const;
+
+ void load_from_string(const String &p_data);
+ String save_as_string() const;
+
HTMLTemplateData();
~HTMLTemplateData();
protected:
static void _bind_methods();
+
+ HashMap _templates;
};
#endif