0

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 Answer 1

3

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

asStr := string(myTplJsObj)
Sign up to request clarification or add additional context in comments.

3 Comments

shame to me :), I was trying: "string.(myObj)" and "myObj.(string)"
@Pascut that format is a type assertion, not a type cast. Type assertions can only be done on interfaces.
The second is a type assertion, the first is just invalid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.