Create an IF statement for a Variable to see if it contains a range of letters - jinja2

{% if val|first in 'ABCDEFG' %}
This works but doing it this way seems a little clumsy to me. Especially with the number of if statements I will be creating.
I've been searching for a solution for hours but can't quite find what I need. I imagine there is an option to help clean up my script.
Is there a way to make this something like: { if val|first in 'A-G' %} ? Bonus points if you can help me make it case insensitive.

I hope this is the answer you are looking for.
{% if val|first|lower in 'a'-'g' %}
This will convert the first character of val to uppercase and check if it is within the range of A-G. You can make it case insensitive by converting it to either upper or lower.

Related

Type Conversion in Jinja 2

I am trying to convert the string value in 'snrMinMarginUp' to integer or float in Jinja 2. I tried to lookup every possible solution, but got no luck. Any help would be highly appreciated!
{% if 'properties' in value %}
{{ value['serviceProfile']['snrMinMarginUp'] }}
{% endif %}
You can use the float or int filters to coerce a value to a float or int, as in:
{{ value.serviceProfile.snrMinMarginUp|float }}
The example you've given in your question is a little ambiguous -- because you're just displaying the value, it doesn't really matter whether the value is numeric or a string. If this solution doesn't work out for you, please update your question to show a complete example of where things aren't working for you.

Break up Long title attribute Value

I have an web page which has tooltip set as follows:
title="Tel: {%- recordFields.providerTel || 'N/A' %} Email: {%-recordFields.providerEmail || 'N/A' %}"
The line occupies 142 columns...
Is there a way to break up the title string in the source so that it can span multiple lines?
Something along these lines:
title="Tel: {%- recordFields.providerTel || 'N/A' %} \
Email: {%-recordFields.providerEmail || 'N/A' %}"
In a comment on the question I asked:
You only want it to span multiple lines in the source, right? The actual value shouldn't have newlines (e.g., when used)?
and you said:
Let's say that. In this particular case, I also want a newline in the output, but I'll remove it for clarity.
It's a really fundamental part of the question. :-)
If you do want the newlines, the answer is easy but (to my mind) unsatisfying: Just put them in, literally:
<div title="Tel: {%- recordFields.providerTel || 'N/A' %}
Email: {%-recordFields.providerEmail || 'N/A' %}">...</div>
Live example. Note that it's important not to have leading whitespace on the next line, because that whitespace is part of the attribute value. This is what makes it unsatisfying to me, because having that subsequent line start at column 0 in something that's otherwise indented seems unclean (and some tools will fight with you, trying to indent it).
If you don't want the newlines in the attribute's value, I'm not aware of a way to do it. According to the HTML specification, an attribute's value is "...Attribute values are a mixture of text and character references...", and if we follow that link for "text" it doesn't say anything about putting source-only linebreaks in the value.
Since you seem to be using some kind of templating engine, if it runs server-side then you could of course define a property on the values object to hold the title string:
title="{%- getTitleFor(recordFields) %}"
...but that moves the content out of your HTML source (where content generally belongs) into your server-side language source, so it's not a great alternative.

preventing xss hole in django

In the Django docs it says:
Django templates escape specific characters which are particularly
dangerous to HTML. While this protects users from most malicious
input, it is not entirely foolproof. For example, it will not protect
the following:
<style class={{ var }}>...</style>
If var is set to 'class1 onmouseover=javascript:func()', this can
result in unauthorized JavaScript execution, depending on how the
browser renders imperfect HTML.
How can I prevent this?
I'm not especially familiar with Django, but it looks to me like the error they intended to point out is that there are no quotes around the attribute value, meaning that the space in the example value causes the rest of the string (onmouseover=...) to be interpreted as a separate attribute. Instead, you should put quotes like so:
<style class="{{ var }}">...</style>
If I understand correctly, this would be safe since all the characters that could interfere with the quoting are escaped. You might want to verify that interpretation; for example, write <span title="{{ var }}">foo</span>, run the template with foo set to <>"'&, and then make sure that they're properly escaped in the HTML and that the title appears in the browser with the original characters.
One thing you can do is not allow variable classes. You can use something like
<style class={% if class_foo %}foo{% elif class_bar %}bar{% else %}baz{% endif %}>...</style>
There are also filters available to prevent xss elsewhere: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#std:templatefilter-escape

Regex to extract text from inside an HTML tag

I know this has been asked at least a thousand times but I can't find a proper regex that will match a name in this string here:
<td><div id="topbarUserName">Donald</div></td>
I want to get the name 'Donald' and the regex that's the closest is >[a-zA-Z0-9]+ but the result is >Donald.
I'm coding in PureBasic (It's syntax is similar to that of Basic) and it uses the PCRE library for regular expressions.
Can anyone help?
Josh's pattern will work if you only make use of the numbered group, not the whole match. If you have to use the whole match, use something like (?<=>)(\w+?)(?=<)
Either way, regex is widely known to not be good for parsing HTML.
Explanation:
(?<=) is used to check if something appears before the current item.
\w+? will match any "word"-character, one or more times, but stop whenever the rest of the pattern matches something, for this situation the ? could have been left out.
(?=) is used to check if something appears after the current item.
Try this
It should capture anything that is a letter / number
>([\w]+)<
Also I'm not exactly sure what your project limitations are, but it would be much easier to do something like this
$('#topbarUserName').text();
in jQuery instead of using a regex.
>([a-zA-Z]+) should do the Trick. Remember to get the grouping right.
Why not doing it with plain old basic string-functions?
a.w = FindString(HTMLstring.s, "topbarUserName") + 16 ; 2 for "> and topbar...
If a > 0
b.w = FindString(HTMLstring, "<", a)
If b > 0
c.w = b - a
Donald.s = Mid(HTMLstring,a, c)
EndIf
EndIf
Debug Donald

How do I check if values between html tags are blank or empty using regular expressions in notepad plus plus

I'm conducting a mass search of files in notepad++ and I need to determine if there are no values between a set of tags (i.e. ).
".*?" will search for 0 or more characters (well, most), which is fine. But I'm looking for a set of tags with at least one character between them.
".+?" is similar to the above and does work in notepad++.
I tried the following, which was unsuccessful:
<author>.{0}?</author>
Thank you for any help.
Since you look for something that doesn't exist you don't have to make it that complicated. Simply searching for <author></author> would do the trick, wouldn't it? If you want to include space-characters as "nothing" you could modify it to the following:
<author>\s*?</author>
Output:
<author></author> Match
<author> </author> Match
<author>something</author> No match
I don't understand why you are using the "?" operator; ".+" should yield the result you need.