Force error in erb when env var unset? - erb

If I use <%= ENV['FOO'] %> in an erb template without setting an environment variable named FOO, then it resolves to an empty string.
Is it possible to force an error (index error, anything) at this point instead; evaluating to a string iff $FOO exists?

Related

Hygen template generator converting quotes to '

I am using Hygen to populate template files and want to print an array of strings eg.
[`'${item}'`]
but the single quotes are being converted to '
All other special characters are unaffected but single or doublequotes get converted so I'm not sure what's going on.
Turns out
<%= %>
tags in the template file escapes some characters, whereas...
<%- %>
does not!

How to test if a string variable in Robot Framework is empty?

How to test if a string variable in Robot Framework is empty?
My first naïve attempt looked like this:
Run Keyword If ${myVar}!=${EMPTY}
but it failed:
Evaluating expression '!=' failed: SyntaxError: unexpected EOF while parsing (, line 1)
I then found this issue at Github but it didn't suggest a solution, just that the error message was unclear. An alternative solution was presented here:
${length}= Get Length ${Portfolio_ste}
Run Keyword If ${length} Go To Edit Portfolio
but is this really the best practice?
(The context is that I use a variable argument list and if a certain variable contains a value something should be done, otherwise just ignore it)
The expression needs to be a valid python expression after variable substitution. Assuming for the moment that myVar might be something like the number 42, your expression would end up looking like this after substitution:
Run Keyword if 42!=
When comparing against the empty string you need to add quotes to guarantee that the expression is a proper python expression after substitution. For example:
Run Keyword If "${myVar}"!="${EMPTY}"
Try Get Variable Value. It solved my problem.

What is the different between Empty String and none Created Variable

What is the different between Empty String and none Created Variable in Batch File?
and if they have any different, can you show me an example of using empty string vs none created string?
#echo off
title Empty String Vs None Created Variable
set String=
set variable=exist
if [%String%] == [] echo its be a Empty String
if [%variable%] == [] echo its be a Empty String
if [%none_exist_var%] == [] echo its be a Empty String
Thanks a Lot!
Variables in batch files can be
Defined: there is a value stored and a associated name used to retrieve the value, that is, the variable name.
Undefined: there is no value and in consecuence there is not any need for an associated name, so it does not exist.
This two simple rules handle how the environment block (where variables/value are stored) is updated. If there is a value, the environment block has an entry for the value and the name to retrieve it. Without a value, the environment block does not create the entry or, when setting the variable to nothing, the entry is removed.
So, if
a never defined variable has not any entry in the environment block
a variable with not value has not any entry in the environment block
there is not any difference between the two cases.
note: While the traditional way to check if a variable stores a value / a variable exists is (as dbenham has commented, this syntax is not recommeded as quotes inside the variable value lead to syntax problems)
if "%varName%"=="" ....
if command extensions are enabled (and the default configuration is to have them enabled) you can also use a safer alternative
if not defined varName ....
if defined varName ....
note that in this syntax, as we are not trying to read the value in the variable, varName is used, not %varName%
There is no difference. Read Delete a variable:
Type SET with just the variable name and an equals sign:
SET _department=
Better still, to be sure there is no trailing space after the = place
the expression in parentheses or quotes:
(SET _department=)
or
SET "_department="

Does heist support substituting (strings / JSON) into an arbitrary location within a template?

Regarding heist, I've got a template such as:
<script>
var json = ???;
</script>
<h1>Example</h1>
Is there a way to substitute the ??? string with another string?
I think the following function might be the solution https://hackage.haskell.org/package/heist-1.0.1.0/docs/Heist-Splices-Json.html#v:bindJson but I have difficulty understanding that function and or what markup to use in the template.
No. You cannot substitute anything inside a <script> tag because the text inside a script tag is not treated as HTML. It's treated as plain text. If this was not done, you wouldn't be able to write JS code like if ( x < 42 ) because the less than would get treated as the beginning of a tag.

In Play Framework 1.2.x, how to use a render arg value in a path expression?

Let's say that I have a renderArg named xyz. In the groovy template, what's the syntax for using the value of the renderArg in a path expression?
For example:
href="##{'/public/stylesheets/whatever/${xyz}.css'}"
The above fails with a template compilation error (which is what I expected, really). How can I use the value of the render arg inside the path string?
I'll also need to use the arg in other path expressions (not just for a css file reference).
You cannot do it directly, but there is one workaround:
First you should have defined route to root of the application, for instance:
GET / Application.index
next you can use it in this way:
href="##{Application.index}public/stylesheets/whatever/${xyz}.css"
If you repeat the structure above very often, then you can use custom tag, to do so:
add file /app/views/tags/customlink.html(customlink is name of the tag, you can use another one),
fill the content:
##{Application.index}public/stylesheet/whatever/${_key}.css
You can use it now in this way:
href="#{customlink key:'xyz' /}"
More about custom tags you can read here

Resources