smarty php in_array not working - smarty

Smarty-3.1.20
Rewriting php webapp backend. Using Smarty because it was required a few years back, worked and why not but now it's wrecking my head. Nested template outputting checkboxes.
Why does the in_array return true if I paste one of the values as a string but not if I reference as a variable? Arrays are var_dump-ed and are arrays and {$otherArray[0]} returns correct variable. $anArray[loop] is printing the correct variable. No extra blanks in array strings. Stop me before I throw it all out, paste json into the bottom of the html and do it all with javascript (next time definitely)
tl;dr:
in_array is not returning true from a variable but is from a string?
<input type='checkbox' name='{$anArray[loop]}' value='{$anArray[loop]}'
{if in_array($anArray[loop]}, $otherArray)} checked{/if}>
<br />
{$anArray[loop]}}

Well, it seems you don't use $ sign here correctly, it should probably be:
<input type='checkbox' name='{$anArray[$loop]}' value='{$anArray[$loop]}'
{if in_array($anArray[$loop]}, $otherArray)} checked{/if}>
<br />
{$anArray[$loop]}}

Try with:
{if $anArray.$loop|in_array:$otherArray}checked{/if}

Related

How To Line Break Inside Code Tag In .slim

So I've been looking all up and down Google and I've got nothing. So can someone help?
I have my .slim file all set up and I'm trying to and a pre - code section with a line break in it...
pre
code[class="language-markup" id="copyHTML-1"]
= '<label for="name-1">Label</label>\
<input id="name-1" type="text" name="name" required>'
The output is...
<label for="name-1">Label</label>\
<input id="name-1" type="text" name="name" required>
I get the line break but I get the trailing backslash. How do I get a link break in there with no trailing backslash?
Thanks in advance.
Actually, line break in string is "\n" in Ruby, and \n must be placed in double quotes "", so you could try this:
= "<label for='name-1'>Label</label>\n
<input id='name-1' type='text' name='name' required>"
Update part:
Or you could just use slim syntax:
label[for="name-1"]
| Label
input#name-1[type="text" name="name" required]
Sounds like you might want to enable the Slim pretty-print feature, which indents nested tags on different lines.
For Rails, you could set this in an environment file., for example config/environments/development.rb:
# Indent html for pretty debugging and do not sort attributes
Slim::Engine.set_options pretty: true, sort_attrs: false
The Slim documentation has more info:
https://github.com/slim-template/slim#configuring-slim

filter_input behavior for integer input tag name

I have form inputs that have their name attribute in Integer like below
<input type="hidden" name="100351312" value="test" />
If I use
echo filter_input( INPUT_POST, '100351312' )
It returns NULL
Whereas
echo $_POST['100351312'] prints the value correctly.
filter_input really needs three inputs; the type, the variable (both of which you have) and the filter, which you don't have. Here are some types of filters: http://php.net/manual/en/filter.filters.php
That said, if you're using WordPress you can almost always find a WordPress helper function you can use instead of filter_input()

Ruby Mechanize, save html as file after filling a form

I want to save the html after filling a form. lets say:
page.form.field.value = 'testing'
page.save 'test.html'
the generated test.html file don't have the modified value attribute
<input name='something' value=''>
I'm expecting:
<input name='something' value='testing'>
You want to use dom functions for that:
page.at('[name=something]')['value'] = 'testing'
In other words there's no reason to expect that changes to the Form fields will update the dom.

Why is #Html.Label() removing some characters

When I use the following code in my razor view it renders <label for=""> someText</label> and not <label for="">1. someText</label> but I can't figure out why 1. is removed while rendering.
#Html.Label(String.Format("{0}. someText",1))
Edit:
The following code renders <label for="">1# someText</label> as expected.
#Html.Label(String.Format("{0}# someText",1))
You are misusing the Html.Label method. It is for:
Returns an HTML label element and the property name of the property
that is represented by the specified expression.
That's why it gets confused if you have a point . in the first parameter because it expects a property expression there.
However, you can use the second overload:
#Html.Label("", String.Format("{0}. someText",1))
Or just write out the HTML:
<label>#String.Format("{0}. someText", 1)</label>
You can avoid using the "Html Helper's label" and directly use html "label" and place whatever you want to display correctly. It can also save some time ;)
The syntax which you are using is wrong or We can say that this is not a way to use property with RAZOR syntax.
You ca use this that may be help full for you.
**
#Html.LabelFor(model => model.PropertyName,
String.Format("{0}. " + #Model.PropertyName.ToString() + ",1))
**
I was using this for a data table that contained a double (Lat/Long) and saw this same problem. Thanks for the tips (I am not allowed to comment).
For me, the problem was solved ..
#foreach (var cell in item.ItemArray)
{
<td>
#Html.Label("",cell.ToString().Trim())
</td>
}

Razor and function call outputting

The scenario is I would like to write a hidden field with a guid value generated by the server.
Why does
<input type="hidden" id="sampleGuid" value="#{Guid.NewGuid().ToString()};" />
yield 'value=""' while
#{
string token = Guid.NewGuid().ToString();
<input type="hidden" id="sampleGuid" value="#token" />
}
properly fill in 'value' with a guid?
You need parentheses instead of braces.
#{ ... } will execute ordinary statements, but won't print anything.
#(...) will print the value of an expression. (and will HTML-encode it)
You've wrapped Guid.NewGuid().ToString() in curly braces.
That just means you want to execute the code, not ouput it.
If you're trying to output a value, wrap the code in parenthesis.

Resources