How to print string if variable is empty - twig

I have the following HTML/TWIG:
<input type="text" id="username" name="_username" value="{{ last_username }}" />
Now I'd like to use value="Username" if last_username is empty. But I also don't want to make an if for this. So, could I do something like:
<input type="text" id="username" name="_username" value="{{ last_username or 'Username'}}" />
I know that I could set a default value on the logic side of my application - not sure what would be better practice

Use the default filter
<input type="text" id="username" name="_username" value="{{ last_username | default('Username') }}" />

Related

remove whitespace between multiple radio box

I add multi radio box in twig page like this:
<div class="cc-selector">
<input id="avatar-1" type="radio" name="avatar" value="1" />
<label class="drinkcard-cc avatar-1" for="avatar-1"></label>
<input id="avatar-2" type="radio" name="avatar" value="2" />
<label class="drinkcard-cc avatar-2" for="avatar-2"></label>
<input id="avatar-3" type="radio" name="avatar" value="3" />
<label class="drinkcard-cc avatar-3" for="avatar-3"></label>
<input id="avatar-4" type="radio" name="avatar" value="4" />
<label class="drinkcard-cc avatar-4" for="avatar-4"></label>
<input id="avatar-5" type="radio" name="avatar" value="5" />
<label class="drinkcard-cc avatar-5" for="avatar-5"></label>
<input id="avatar-6" type="radio" name="avatar" value="6" />
<label class="drinkcard-cc avatar-6" for="avatar-6"></label>
</div>
in Output in see whitespace:
how do can i remove whitespace in twig template?!
You can use spaceless filter. It will do exactly what you want : removing whitespace between HTML elements only (keeping the ones inside).
Using this in Twig :
{% apply spaceless %}
<div class="cc-selector">
<input id="avatar-1" type="radio" name="avatar" value="1" />
<label class="drinkcard-cc avatar-1" for="avatar-1"></label>
<input id="avatar-2" type="radio" name="avatar" value="2" />
<label class="drinkcard-cc avatar-2" for="avatar-2"></label>
<input id="avatar-3" type="radio" name="avatar" value="3" />
<label class="drinkcard-cc avatar-3" for="avatar-3"></label>
<input id="avatar-4" type="radio" name="avatar" value="4" />
<label class="drinkcard-cc avatar-4" for="avatar-4"></label>
<input id="avatar-5" type="radio" name="avatar" value="5" />
<label class="drinkcard-cc avatar-5" for="avatar-5"></label>
<input id="avatar-6" type="radio" name="avatar" value="6" />
<label class="drinkcard-cc avatar-6" for="avatar-6"></label>
</div>
{% endapply %}
Will give this HTML :
<div class="cc-selector"><input id="avatar-1" type="radio" name="avatar" value="1" /><label class="drinkcard-cc avatar-1" for="avatar-1"></label><input id="avatar-2" type="radio" name="avatar" value="2" /><label class="drinkcard-cc avatar-2" for="avatar-2"></label><input id="avatar-3" type="radio" name="avatar" value="3" /><label class="drinkcard-cc avatar-3" for="avatar-3"></label><input id="avatar-4" type="radio" name="avatar" value="4" /><label class="drinkcard-cc avatar-4" for="avatar-4"></label><input id="avatar-5" type="radio" name="avatar" value="5" /><label class="drinkcard-cc avatar-5" for="avatar-5"></label><input id="avatar-6" type="radio" name="avatar" value="6" /><label class="drinkcard-cc avatar-6" for="avatar-6"></label></div>
Since the contents of the template is HTML I'd advise you to use SeeoX's approach.
But I've decided to mention the other option to assign the contents of a template to a variable and apply filters which could be useful sometimes:
{% set contents %}
{% include ('cc-selector.twig') %}
{% endset %}
contents spaceless:
{{ contents | spaceless }}
replace only new lines:
{{ contents | replace({"\n": ""}) | raw }}
working example
It may be because of the linebreak between the input & the label. Removing that space may/may not help, but try that.
<div class="cc-selector">
<input id="avatar-1" type="radio" name="avatar" value="1" /><label class="drinkcard-cc avatar-1" for="avatar-1"></label><input id="avatar-2" type="radio" name="avatar" value="2" /><label class="drinkcard-cc avatar-2" for="avatar-2"></label><input id="avatar-3" type="radio" name="avatar" value="3" /><label class="drinkcard-cc avatar-3" for="avatar-3"></label><input id="avatar-4" type="radio" name="avatar" value="4" /><label class="drinkcard-cc avatar-4" for="avatar-4"></label><input id="avatar-5" type="radio" name="avatar" value="5" /><label class="drinkcard-cc avatar-5" for="avatar-5"></label><input id="avatar-6" type="radio" name="avatar" value="6" /><label class="drinkcard-cc avatar-6" for="avatar-6"></label>
</div>

Some html tags are missing when i write to a file using python requests

I am trying to login to a website using requests.The website requies token for its login.
so decided to parse the html and write it to a file.txt But the file.txt is missing the token tag.
HTML code:
<form id="pw_form" class="exception_password" action="/409514769/password" method="post" data-xsrf-protection="enabled">
<input type="password" id="password" class="exception_password-input iris_input" name="password" placeholder="Enter password" autocomplete="off" data-validators="required">
<input type="hidden" name="is_review" value="">
<input type="hidden" name="is_file_transfer" value="">
<input type="submit" value="Submit" class="iris_btn iris_btn--primary">
<input type="hidden" name="token" value="4dc82c1a780e11667650f856da9b1d9fd31b176b.e7mu8nmqrb.1587446534"></form>
PYTHON code:
from requests import Session
with Session() as s:
site = s.get("https://vimeo.com/409")
with open('page.txt','w') as out:
out.write(site.text)
This is what the file writes:
<form id="pw_form" class="exception_password" action="/409514769/password" method="post">
<input type="password" id="password" class="exception_password-input iris_input" name="password" placeholder="Enter password" class="password iris_form_text" autocomplete="off" data-validators="required">
<input type="hidden" name="is_review" value="">
<input type="hidden" name="is_file_transfer" value="">
<input type="submit" value="Submit" class="iris_btn iris_btn--primary">
</form>
What is happening here?
Website don't allow request from bot.
One possible solution to this problem is to add headers while making the request.

Pyramid ignore missing template variables

I have a simple form using Pyramid and I want to use it as a new or update. I don't particularly want to have to create empty dictionary values for all the fields in my form, I just want it to ignore them without raising an exception.
<div metal:fill-slot="content">
<form method="POST" action="/studentupdate">
<input name="id" value="${studentid}" type="hidden" />
Name: <input type="text" name="studentname" value="${studentname}"/><br />
Exam Score: <input type="text" name="studentexam" value="${studentexam}"/><br />
Quiz Score: <input type="text" name="studentquiz" value="${studentquiz}"/><br />
Homework Score: <input type="text" name="studenthomework" value="${studenthw}"/><br />
<input type="submit" value="Save"/>
</form>
</div>
How can I do this?
Thanks.
You can workaround like this
${studentexam or ''}
% for i in missing_list or []
<li>${i}</li>
% endfor

how to remove html and javascript from input field before sending to server

<form action="process_reg.php" method="post" name="register_form" id="register_form">
<input class="logbar" id="fname" name="fname" type="text" placeholder="Firstname" onfocus="CheckFname(); return true;" onblur="leaveFname();" required>
<span id="fnameMessage"></span>
<input class="logbar" id="lname" name="lname" type="text" placeholder="Lastname" onfocus="CheckLname(); return true;" onblur="leaveLname();" required>
<span id="lnameMessage"></span>
<input class="logbar" id="email" name="email" type="text" placeholder="Email" onfocus="CheckEmail();" onblur="leaveEmail();" required>
<span id="emailMessage"></span>
<input class="logbar" id="password" name="password" type="password" placeholder="Password" keyev="true" required >
<input class="logbar" id="password2" name="password2" type="password" placeholder="Confirm Password" onkeyup="checkPass(); return false;" required>
<span id="confirmMessage" class="confirmMessage"></span>
<input value="Logga in" type="button" onclick="formhash(this.form, this.form.password);" id="register">
</form>
and the Sanitize/XSS protection that i applied on this form is given
if (empty($_REQUEST) === false)
{
$regemail1 = filter_input('INPUT_REQUEST', 'email', 'FILTER_SANITIZE_EMAIL');
$regfirst1 = filter_input('INPUT_REQUEST', 'fname', 'FILTER_SANITIZE_SPECIAL_CHARS');
$reglast1 = filter_input('INPUT_REQUEST', 'lname', 'FILTER_SANITIZE_SPECIAL_CHARS');
$regpass = $_POST['p'];
$regemail = htmlspecialchars($regemail1);
$reglast = htmlspecialchars($reglast1);
$regfirst = htmlspecialchars($regfirst1);
$regemail =
}
When I enter Hi it is perfect in a way that it stop the function of tag. but I want to send only text to the database and remove all the other things.
The only value being send to the server in the sample bellow is what ever is input field, you don't send html to server.
<input class="logbar" id="password" name="password" type="password" placeholder="Password" keyev="true" required >
you always check what you are getting in the server by printf, echo

Empty message with Formt in MODx

Here is my code of send-page:
<p>[[!FormIt?
&hooks=`email,FormItLog,spam,emailUser,redirect`
&emailTo=`heash94#gmail.com`
&emailSubject=`[[+subject]]`
&emailFromName=`[[+name]]`
&emailTpl=`ContactTpl`
&redirectTo=`62` ]]</p>
<div>[[+fi.error.error_message]]</div>
<form action="[[~[[*id]]]]" method="post"><input type="hidden" name="nospam:blank" value="" />
<div>
<label for="name">Name: </label> <input id="name" type="text" name="name:required" value="[[+fi.name]]" /> <span>[[+fi.error.name]]</span>
</div>
<div>
<label for="name">Email: </label> <input id="email" type="text" name="email:email:required" value="[[+fi.email]]" /> <span>[[+fi.error.email]]</span></div>
<div>
<label for="subject">Theme: </label> <input id="subject" type="text" name="subject:required:stripTags" value="[[+fi.subject]]" /> <span>[[+fi.error.subject]]</span></div>
<div>
<label for="message">Text: </label><span>[[+fi.error.message]]</span> <br /> <textarea id="message" name="message:required:stripTags" rows="7" cols="55">[[+fi.message]]</textarea></div>
<div>
<input type="submit" name="Submit" value="Отправить" /></div>
</form>
I tried many examples, but they send to my email empty messages.
Here is my reciver-page code:
This message [[+name]] ([[+email]]) was sand with callback form:
[[+message]]
But in response I have only "This message () was sent with callback form: ".
Modx Revolution.
Sounds like modx is not seeing the name="email:email:required" as 'email' - put your CSS/JS validation in another attribute & also use the &validate attribute for the formit call.

Resources