how to check empty value of a string in golang template - string

I have this below golang template code snippet where I take values from a map of type map[string]interface{} and check if that string is empty or not but my string empty check is failing as:
template: apps.html:62:29: executing "apps.html" at <eq $src "">: error calling eq: invalid type for comparison . I tried to print the empty value also and it is rendered as <nil> but my {{if eq $src "<nil>"}} check is also failing and even if I put nil then also it fails. Is there any better way to achieve this.
{{$src := (index . "source")}}
{{$tar := (index . "target")}}
{{if eq $src ""}}
<div></div>
{{else}}
<div style="display:none;">
<input id="username" name="source" value="{{ $src }}"/>
<input id="username" name="target" value="{{ $tar }}"/>
</div>
{{end}}

Here is what you're doing (always better to give an example play.golang.org link if possible):
https://play.golang.org/p/uisbAr_3Qy
A few problems with what you're doing: If you're using a map for context, you don't need to use index at all, so your variables are not required. If you want to check if a key exists, just check for a nil entry with if. If your map contains elements of type interface, you can't compare with a string, only use eq when you're sure you have an element but are not sure what it might be, and wrap it in an if test if unsure whether the key exists.
I think you want to do something like this:
{{if .source }}
<div style="display:none;">
<input id="username" name="source" value="{{ .source }}"/>
<input id="username" name="target" value="{{ .target }}"/>
</div>
{{else}}
<div>empty</div>
{{end}}
https://play.golang.org/p/D2DCjAklFE
See the docs for text/template as they have a lot more detail:
https://golang.org/pkg/text/template/#hdr-Actions

If you want to compare with null(nil) use: https://github.com/Masterminds/sprig/issues/53#issuecomment-483414063
{{ kindIs "invalid" $value }}

Related

How to Use Variable as the Key

If I use a String for the $key my code works but if I use $hasorhasnotentered it returns "Array". How can I use a Variable for the $key?
<?php
$user_id = $current_user->ID;
$key = $hasorhasnotentered;
$single = true;
$user_last = get_user_meta( $user_id, $key, $single );
echo '<p>The '. $key . ' value for user id ' . $user_id . ' is: ' . $user_last . '</p>';
?>
The Variable is being defined by user interaction of a form below is the form code
<form method="post" id="adduser" action="/my-likes-list/">
<input type="text" name="<?php echo $hasorhasnotentered;?>" id="<?php echo $hasorhasnotentered;?>" value="<?php echo $yesentered; ?>"/>
<p class="form-submit">
<input name="updateuser" type="submit" id="updateuser" class="submit button" value="update-user" onclick='enterdnew()'/>
<?php wp_nonce_field( 'update-user' ) ?>
<input name="action" type="hidden" id="action" value="Update" />
</p>
</form>
Your code looks OK, but I expect your $hasorhasnotentered variable is not undefined. If it's value comes from user input check for definedness before you use it!
You can use a variable just as you did. It's just that the variable needs to contain a string. Evidently, your $hasorhasnotentered contains. That explains why it would return an array because if $key is left blank get_user_meta returns all of the meta data in an array. See the documentation: https://codex.wordpress.org/Function_Reference/get_user_meta
$key
(string) (optional) The meta_key in the wp_usermeta table for the meta_value to be returned. If left empty, will return all user_meta fields for the given user.
Default: (empty string)
Edit:
In the form code above, I believe you want:
<input type="text" name="hasorhasnotentered" id="hasorhasnotentered" value="<?php echo $hasorhasnotentered; ?>"/>
Then, on load, the value of the element will be set to whatever $hasorhasnotentered is. And the user will set $hasorhasnotentered to be whatever text they type in the element.
Then, on submit, $hasorhasnotentered will be the text of the input field.
I have to admit, I may not be understanding the intent of your code.

I am using material design switch , but i am not able to select it by default or using jquery

Below is the sample html:
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect is-checked " for="distanceUnit">
<input type="checkbox" id="distanceUnit" class="mdl-switch__input "/>
<span class="mdl-switch__label left">{% trans "mi" %}</span>
<span class="mdl-switch__label right">{% trans "km" %}</span>
</label>
I get the value of distanceUnit from User Profile. with something like {{ user.userprofile.distance_unit }} . But I am not sure how to use this in setting the switch to on/off.
Damn , It was as easy adding the checked property.
Going through the documentation again , always helps.

Variables In Sublime Text 3 Snippets

In PHPStorm, when creating a snippet, you can do something like this:
<!-- $VALUE$ Form Input -->
<div class="form-group">
<input type="text" name="$NAME$" class="form-control">
</div>
With variables and such, so you can easily write a value. I'm wondering how to do the same thing with Sublime Text 3. This is what I have in my CDATA tag:
<!-- $VALUE Form Input -->
<div class="form-group">
<input type="text" name="$NAME" class="form-control">
</div>
However, it's not working.
Unfortunately, the only variables that are available in snippets are specified in the docs under snippets environment-variables, as well as any you declare in a .tmPreferences metadata file, or in a .sublime-options file. However, you can have predefined values in snippets. For example, this snippet:
<snippet>
<content><![CDATA[<!-- ${1:VALUE} Form Input -->
<div class="form-group">
<input type="text" name="${2:NAME}" class="form-control">
</div>$0]]></content>
<tabTrigger>form_group</tabTrigger>
<scope>text.html</scope>
</snippet>
gives this result when triggered:
You can then hit Tab and the focus will move from field 1 to field 2:
and again to move to the exit point $0:

How to concatenate a variable to a string in smarty?

I've following smarty code:
{foreach from=$preview_data key=key item=value}
<input type="hidden" class="form-control" name="units_$key" id="units_$key" value="{$value.units}">
{/foreach}
I want to attach a string units_ to a value contained in variable $key. I tried above code but it didn't work. How to achieve this?
$key is just a variable, use the same syntax:
<input type="hidden" class="form-control" name="units_{$key}" id="units_{$key}" value="{$value.units}">

xpath to get checkbox inside label

I have this code
<label>
<input type="radio" checked="checked" value="fOejPdlZIx83HA" name="btnRad">
Test1
</label>
<label>
<input type="radio" checked="checked" value="fdsaf4waff4sssd" name="btnRad">
Test2
</label>
<label>
<input type="radio" checked="checked" value="fg43fasd43wsat4" name="btnRad">
Test3
</label>
I wish to access the radio button depending on the label text via xpath
I already tried multiple thing:
//input[#name='btnRad]']/following::*[contains(text(),'Test3')]
//label[text()='Test3']/input[#name='btnRad']
//*[contains(text(),'Test3')]
Even the last one return me nothing, so xpath think that "Test3" is not the text of the label... anyone have an idea how to do this?
Your expression is failing because your label has more that one text node: an empty string before the input, and Test3. The way you're using contains means it will only check the first text node, ie empty string.
Two ways of solving this:
eliminating the empty strings with normalize-space():
//*[contains(text()[normalize-space()], 'Test3')]
querying each text():
//*[text()[contains(.,'Test3')]]
For a more detailed explanation, see How to search for content in XPath in multiline text using Python?.
This works also //label[contains(.,'Test3')]/input .

Resources