How to append a character while using the conditional statement in sightly? - aem

I need to set the href value depending on the nullity of sling:vanityPath. The hit.properties.sling:vanityPath returns the string value for sling:vanityPath property. I need to append a / before the hit.properties.sling:vanityPath value. Is it possible to do that in the following code or do I have to test it twice, but that comes with code repetition?
<a href="${hit.properties.sling:vanityPath == null? hit.path : hit.properties.sling:vanityPath # extension='html'}"/>

Concatenation or binary operators are not supported in HTL. You can either use prependPath or double test for the slash.

I don't think you can do it in one line. But you can try defining a variable for the concatenated value instead of duplicating code for the a tag. Also, do you have to check for null specifically? can't you just reverse the conditions and check if the value exists?
<sly data-sly-test.concatenatedURL="${['/', hit.properties.sling:vanityPath] # join = ''}"/>
<a href="${hit.properties.sling:vanityPath ? concatenatedURL : hit.path # extension='html'}"/>

You can use # join to concat an array:
<a href="${hit.properties.sling:vanityPath ? ['/', hit.properties.sling:vanityPath] : hit.path # join ='', 'extension='html'}"/>

Related

Check if string contains any of the following

I'm trying to check if a string contains one of four sub strings in a simpler way than this:
if (imageUrl.contains('.jpg') ||
imageUrl.contains('.png') ||
imageUrl.contains('.tif') ||
imageUrl.contains('.gif')) {
}
Is there a way to do this? For example checking against a list?
You can use a regex pattern instead of a simple string:
imageUrl.contains(new RegExp("\.(jpg|png|tif|gif)"))
Might be somewhat simpler.
RegularExpression can solve your problem. RegEx are used to search patterns in strings.
RegEx example:
^The matches any string that starts with The
end$ matches a string that ends with end
^The end$ exact string match (starts and ends with The end)
abc* matches a string that has ab followed by zero or more c

How to check if value in field is decimal and not string (DATASTAGE)?

How to check if value in field is decimal and not string (DATASTAGE) ?
I am using with Datastage Version 8.
Try using IsValid() with the 'decimal' sub-type on the incoming string.
If IsValid("decimal" , in.value_string ) Then in.value_tring Else SetNull()
You can use Alpha & num function inside transformer which gives you whether the given value contains only alphabets.
If alpha(column) is 1 then its purely alphabetical.
Else check if Num(column) is 1, if this true then it is purely a number.
Reference to official doc-https://www.ibm.com/support/knowledgecenter/SSZJPZ_11.5.0/com.ibm.swg.im.iis.ft.usage.doc/topics/r_deeref_String_Functions.html

How to split string with trailing empty strings in result?

I am a bit confused about Scala string split behaviour as it does not work consistently and some list elements are missing. For example, if I have a CSV string with 4 columns and 1 missing element.
"elem1, elem2,,elem 4".split(",") = List("elem1", "elem2", "", "elem4")
Great! That's what I would expect.
On the other hand, if both element 3 and 4 are missing then:
"elem1, elem2,,".split(",") = List("elem1", "elem2")
Whereas I would expect it to return
"elem1, elem2,,".split(",") = List("elem1", "elem2", "", "")
Am I missing something?
As Peter mentioned in his answer, "string".split(), in both Java and Scala, does not return trailing empty strings by default.
You can, however, specify for it to return trailing empty strings by passing in a second parameter, like this:
String s = "elem1,elem2,,";
String[] tokens = s.split(",", -1);
And that will get you the expected result.
You can find the related Java doc here.
I believe that trailing empty spaces are not included in a return value.
JavaDoc for split(String regex) says: "This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array."
So in your case split(String regex, int limit) should be used in order to get trailing empty string in a return value.

creating an array and append values to it in smarty template

I want to create an array in smarty and do an append functionality in it! Like if I declare a variable in smarty template like {assign var=sizearr value=''} and then i want to append values to this in a loop, and i can access values like {sizearr.0}, how can i do that?
Use append. I'm not sure if this is also available in Smarty 2
{append var='sizearr' value='' index=0}
In smarty3 yould also use a more php-like approach:
{$sizearr[] = 'your value'}
and either loop through the array like
{foreach $sizearr as $value}
{$value#key}: {$value}
{/foreach}
or just hit a specific index:
{$sizearr[2]}
You can use this too:
{$sizearr[] = "Size value"}
Here you can see the full doc (Section Appending an array)
You can simply use the smarty built-in function append :
Now lets take this example:
{assign var="ages" value=[] }
{for $i=1 to 3}
{append var="ages" value=$i }
{/for}
In the above example we didn't specify index parameter in the append function, so the value will be appended at the end of the ages array.
Hope this is helpful for everyone.

How do you concatenate strings in a Puppet .pp file?

Here is my naive approach:
# puppet/init.pp
$x = 'hello ' +
'goodbye'
This does not work. How does one concatenate strings in Puppet?
Keyword variable interpolation:
$value = "${one}${two}"
Source: http://docs.puppetlabs.com/puppet/4.3/reference/lang_variables.html#interpolation
Note that although it might work without the curly braces, you should always use them.
I use the construct where I put the values into an array an then 'join' them.
In this example my input is an array and after those have been joined with the ':2181,' the resulting value is again put into an array that is joined with an empty string as separator.
$zookeeperservers = [ 'node1.example.com', 'node2.example.com', 'node3.example.com' ]
$mesosZK = join([ "zk://" , join($zookeeperservers,':2181,') ,":2181/mesos" ],'')
resulting value of $mesosZK
zk://node1.example.com:2181,node2.example.com:2181,node3.example.com:2181/mesos
Another option not mentioned in other answers is using Puppet's sprintf() function, which functions identically to the Ruby function behind it. An example:
$x = sprintf('hello user %s', 'CoolUser')
Verified to work perfectly with puppet. As mentioned by chutz, this approach can also help you concatenate the output of functions.
The following worked for me.
puppet apply -e ' $y = "Hello" $z = "world" $x = "$y $z" notify { "$x": } '
notice: Hello world
notice: /Stage[main]//Notify[Hello world]/message: defined 'message' as 'Hello world'
notice: Finished catalog run in 0.04 seconds
The following works as well:
$abc = "def"
file { "/tmp/$abc":
You could use the join() function from puppetlabs-stdlib. I was thinking there should be a string concat function there, but I don't see it. It'd be easy to write one.
As stated in docs, you can just use ${varname} interpolation. And that works with function calls as well:
$mesosZK = "zk://${join($zookeeperservers,':2181,')}:2181/mesos"
$x = "${dirname($file)}/anotherfile"
Could not use {} with function arguments though: got Syntax error at '}'.