dongpao1926 2017-06-30 13:24
浏览 1026
已采纳

golang template.JS转换为字符串,template.HTML转换为字符串

I'm using "html/template".

How do I convert the value of a template.JS object to a string? What about template.HTML?

I have a struct where I store some js scripts and css code/files, such as: "HeaderCSS", "FooterJS". I load them to the header/footer of my templates. The problem is that I need to check if they are loaded, so I won't load the same script twice (each reload means a new script load) because I'm using append, in order to be able to insert multiple scripts. In this way, each refresh will append the scripts to the existing scripts.

My idea is to check if they are allready loaded in order to prevent loading them multiple times. I was thinking about using strings.Contains() function. But I can't do that since I don't know to convert templates.HTML and templates.JS to string.

Any idea?

  • 写回答

1条回答 默认 最新

  • duandang2123 2017-06-30 13:26
    关注

    Per the documentation, they're strings. Just type cast to string:

    asStr := string(myTplJsObj)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在codeblocks里主函数运行到打印生成的链表就结束了 没有再往下是为什么呢
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!
  • ¥20 win11账户锁定时间设为0无法登录
  • ¥45 C#学生成绩管理系统
  • ¥30 matlab appdesigner私有函数嵌套整合