Execute template without quotes in golang - go

I'd like to execute template with noescape and no quotes, but noescape is not supported now.
Any suggestion or do I have to use another template engine? Thank you!
Code here: http://play.golang.org/p/R-Ib5H9bXx

You are encouraged to store safe Javascript in the type template.JS:
type JS string
JS encapsulates a known safe EcmaScript5 Expression,
for example, (x + y * z()). Template authors are responsible for
ensuring that typed expressions do not break the intended precedence
and that there is no statement/expression ambiguity as when passing an
expression like "{ foo: bar() }\n'foo'", which is both a valid
Expression and a valid Program with a very different meaning.
So, the only change you need to do to your code is:
type Var struct {
Name template.JS
Value template.JS
}

A small addition to #ANisus answer. There is also a similar wrapper for HTML (and CSS). Whenever you try to pass an HTML string into a template, it gets quoted. So in order to render the safe HTML properly, use:
yourHTML := "<strong>Hi!</strong>"
yourWrappedHTML := template.HTML(yourHTML)
// pass the later into your template

Related

How does a loosely typed language know how to handle different data types?

I was working on a simple task yesterday, just needed to sum the values in a handful of dropdown menus to display in a textbox via Javascript. Unexpectedly, it was just building a string so instead of giving me the value 4 it gave me "1111". I understand what was happening; but I don't understand how.
With a loosely typed language like Javascript or PHP, how does the computer "know" what type to treat something as? If I just type everything as a var, how does it differentiate a string from an int from an object?
What the + operator will do in Javascript is determined at runtime, when both actual arguments (and their types) are known.
If the runtime sees that one of the arguments is a string, it will do string concatenation. Otherwise it will do numeric addition (if necessary coercing the arguments into numbers).
This logic is coded into the implementation of the + operator (or any other function like it). If you looked at it, you would see if typeof(a) === 'string' statements (or something very similar) in there.
If I just type everything as a var
Well, you don't type it at all. The variable has no type, but any actual value that ends up in that variable has a type, and code can inspect that.

Why are 'new' and 'make' not reserved keywords?

With syntax highlighting enabled, it's distracting while reading code like answer to this question with new used as a variable name.
I'm trying to think of a reason why only a subset of keywords would be reserved and can't come up with a good one.
Edit: Alternate title for this question:
Why are Go's predeclared identifiers not reserved ?
That's because new and make aren't really keywords, but built-in functions.
If you examine the full list of the reserved keywords, you won't see len or cap either...
In short: because using predeclared identifiers in your own declarations don't make your code ambiguous.
new and make are builtin functions, amongst many others. See the full list in the doc of the builtin package.
Keywords are a carefully chosen small set of reserved words. You cannot use keywords as identifiers because that could make interpreting your source ambiguous.
Builtin functions are special functions: you can use them without any imports, and they may have varying parameter and return list. You are allowed to declare functions or variables with the names of the builtin functions because your code will still remain unambiguous: your identifier in scope will "win".
If you would use a keyword as an identifier, you couldn't reach your entity because an attempt to refer to it by its name would always mean the keyword and not your identifier.
See this dirty example of using the predeclared identifiers of the builtin function make and the builtin type int (not recommended for production code) (try it on the Go Playground):
make := func() string {
return "hijacked"
}
int := make() // Completely OK, variable 'int' will be a string
fmt.Println(int) // Prints "hijacked"
If you could use keywords as identifiers, even interpreting declarations would cause headache to the compiler: what would the following mean?
func() - is it calling your function named func or is it a function type with no params and no return types?
import() - is it a grouped import declaration (and importing nothing) or calling our function named import?
...

In Mathematica/Wolfram language, is there any way to check that an expression evaluated?

If I have a function:
Foo[x_] := If[x==2, Print#"Two", Print#"No"]
Then if I write the following:
Foo[oops]; Foo[5]
Where oops is a misspelled name for a global variable, the result is that the call to Foo[oops] just falls through rather than giving an error. I know why this is - because it creates a symbolic expression that, since it is not evaluated, does not do anything - but it's very awkward for procedural programming. Is there any way to specify that a function or expression must be completely evaluated and to give an error or return an appropriate value if it isn't?
If you want to require a numeric argument do something like this:
foo[x_?NumericQ] := whatever
foo[x_] := Print["Error"]
Be sure to Clear your original definition before defining this way.
In your example you could alternately work with the three argument form of If

Is it possible to include a type `T`'s string expansion into the error message given by `static_assert`?

This is a follow up questions from How to convert typename T to string in c++
I am asking because I would really like to generate nice error messages like
static_assert(one_of<T,Components...>::value,
"Unable to access T because you didn't
use it in filter<Components...>.");
Would print
Unable to access Foo because you did not use it in filter<Bar,Baz,Bat>.
Is something like this now possible in C++11 / 14?
Quoting from the poor(lazy?) man's version of the C++ Standard,
Since message(the second argument to static_assert) has to be a string literal, it cannot contain dynamic information or even a constant expression that is not a string literal itself. Typically, it cannot contain the name of the template type argument.
So, there isn't a way of getting the friendly static_assert error messages you desire.
No, no it is not possible to do that.

Check if a variable is used in Smarty template

Is there a way to find out which variables are used by a Smarty template? Consider a function taking in a template as an argument and assigning variables to it. Some of the variables require much computation, and hence I don't want to compute them and assign them to the template if they are not needed. I would like something like this:
function addVariables($tpl) {
if($tpl->usesVariable('foo'))
$tpl->assign('foo', computationallyHeavyFunction());
return $tpl;
}
If $tpl = "some text using some variable {$bar}", foo should not be assigned, but if $tpl = "some text using some variable {$foo}", foo should be computed and assigned. Is this possible?
I don't think so. But probably a better(/working) approach is to create a lazy-loading wrapper plugin and use it instead of direct variable calling.
This way your plugin would be called only if it is used anywhere and if you do the computation here/call the computation heavy parts here you can be sure that the computation will be done only if really required.

Resources