Django template excape literal string - django-templates

Basically, is something like
{{ ">>"|escape }}
possible?
I can use {{ context_variable|escape}} but can I use a string literal?
I want to avoid littering my template files with >

Yes. Django treats invalid variables as an empty string. That's the case when you pass a string literal. I will have the string literal concatenated to an empty string.
There is also this answer here.
Literal HTML in Django without using a variable?

Related

Aurelia template parse error when string contains HTML entity for quote character

When I try to feed a string to Aurelia from a template and the string contains HTML entities representing the same quoting character as those surrounding the string, I get parser errors. Apparently the HTML enitities are interpreted before they reach Aurelia, but I'm not sure shy.
For example:
${"Why wouldn't "this" work?"}
Results in
Error: Parser Error: Unconsumed token this at column 15 in expression ["Why wouldn't "this" work?"]
Could somebody tell me why entities are interpreted rather than just outputted to the DOM? And what can I do to get this to work?
it seems aurelia parser interprets &quot like equivalent to ".
This makes your expression look incorrect to the parser.
You should escape the " quotes like this:
${"Why wouldn't \"this\" work?"}
Regards.

How do I replace multiple characters in a String?

How do I replace multiple characters in a String?
Like Java's replaceAll(regex:replacement:) function.
str.replaceAll("[$,.]", "") //java code
This answer is very close but I want to change more than one character at the same time.
[$,.] is regex, which is the expected input for Java's replaceAll() method. Kotlin, however, has a class called Regex, and string.replace() is overloaded to take either a String or a Regex argument.
So you have to call .toRegex() explicitly, otherwise it thinks you want to replace the String literal [$,.]. It's also worth mentioning that $ in Kotlin is used with String templates, meaning in regular strings you have to escape it using a backslash. Kotlin supports raw Strings (marked by three " instead of one) which don't need to have these escaped, meaning you can do this:
str = str.replace("""[$,.]""".toRegex(), "")
In general, you need a Regex object. Aside using toRegex() (which may or may not be syntactical sugar), you can also create a Regex object by using the constructor for the class:
str = str.replace(Regex("""[$,.]"""), "")
Both these signal that your string is regex, and makes sure the right replace() is used.
If you're happy to work with regular expressions, then refer to the accepted answer here. If you're curious as to how you can achieve this without regular expressions, continue reading.
You can use the String.filterNot(predicate:) and Set.contains(element:) functions to define a String.removeAll extension function as follows:
/**
* #param charactersToRemove The characters to remove from the receiving String.
* #return A copy of the receiving String with the characters in `charactersToRemove` removed.
*/
fun String.removeAll(charactersToRemove: Set<Char>): String {
return filterNot { charactersToRemove.contains(it) }
}
You would call on this function as follows: myString.removeAll(setOf('$', '.'))

Redis return value for a string object

When I save a string to redis using ServiceStack.Redis:
client.Add("key1","abc");
While fetching the value, it returns:
client.GetValue("key1");
it returns
"\"abc\""
How do I get the complete string?
Thanks
It appears as though the client.Add() method converts the value to a string (even strings) and wraps them in quotes. The client.SetValue() method only accepts strings and does not wrap them in quotes.
One option would be to convert the value into a string yourself. Either via the common ToString() method, or another method to get the needed string from the object.
If the Add() method is necessary however. You could opt to check if the string is wrapped in quotes when you get it via GetValue() and if so, remove them.
Redis converts string to JSON when saving, that's why it's wrapped in quotes.
So you have to treat this string as JSON object and parse it afterwards manually or using deserialization.

String concatenation in stringtemplate

I'm trying to make a lookup to a hash table m.notes using concatenated value of position.qid and literal ".qid" like this:
$tag(name="itemId", content=m.notes.(position.qid".qid").itemId)$
I tried different options, but I get runtime error. Can someone correct my syntax?
Put the 2 items in an array. StringTemplate concatenates all items in an array (or as they call it, a multi-valued-attribute) when it executes a ToString() on it.
[position.qid, ".qid"]
So, if position.qid evaluates to "hello", this expression would become
hello.qid.
Not sure whether such concatenation is possible in string template. Why don't you use a different method that could do the concatenation and return the value.
e.g:
position.fullQid instead of position.qid
where,
public String getFullQid(){
return getQid() + ".qid";
}
in template group, I can do like this, first, define a concantenate template:
concantenate(substr)::=""
then use as following
(concantenate([position.qid,".qid"]))

Escape dot from GString

I would like to learn how to escape dot in GString so groovy (1.8) does not treat it as a part of an variable inside sql.execute. I have the following code:
Map<String, String> dbSettings = [schemaName:"testSchema"];
String myDbPrefix = dbSetting.schemaName + ".";
sql.execute "DELETE FROM ${myDbPrefix}myTable"
And I got this error:
Ignoring groovy.lang.MissingPropertyException: No such property: myTable for class: java.lang.String
Clearly indicating that . was interpreted as part of variable ${myDbPrefix}.
Does escaping the embedded variable help?
  sql.execute "DELETE FROM ${Sql.expand myDbPrefix}myTable"
I got hit by this problem today. GStrings get handled by a special way in GroovySQL. It's mentioned in the javadoc. It does automatic parameter binding.
Each value in the GString will become a parameter (?) which gets set as a JDBC prepared statement parameter.
What a surprise!
I'm going to fix the problem in my application by subclassing the Sql class and overriding the GString handling with plain ".toString()" .
Documented in Groovy wiki:
GString Use Cases - GSQL Another use case for GString is GSQL where
parameters can be passed into SQL statements using this same mechanism
which makes for a neat way to integrate Groovy with other languages
like SQL. GroovySql then converts the expressions to ? and uses a JDBC
PreparedStatement and passes the values in, preserving their types.
If you explicitly want to coerce the GString to a String you can use
the toString() method. Groovy can also automatically coerce GStrings
into Strings for you.