I ask this question a few day ago, and perhaps I don't explain myself really well.
I'm trying to use Silex2, so I copy the code in the documentation to make my first page.
index.php
require_once __DIR__.'/../vendor/autoload.php';
$app = require __DIR__.'/../src/app.php';
require __DIR__.'/../config/dev.php';
require __DIR__.'/../src/controllers.php';
$pg_cnt['temp'] = 'Welcome !';
$pg_cnt['name'] = 'tutu';
$app->get('/', function () use ($app, $pg_cnt) {
return $app['twig']->render('index.html.twig', array(
'pg_cnt' => $pg_cnt,
));
});
$app->get('/home', function () use ($app, $pg_cnt) {
return $app['twig']->render('index.html.twig', array(
'pg_cnt' => $pg_cnt,
));
});
$app->run();
index.html.twig
{% extends "arche/layout.html.twig" %}
{% block content %}
<br /><br /><br /><br /><br />
{{ global.request }}
<h1>{{ pg_cnt.temp }}</h1>
<h1>{{ pg_cnt.name|default('default titi') }}</h1>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
{% endblock %}
I use the same code for the route / and /home, but for this one / I have an error :
Twig_Error_Runtime in index.html.twig line 7: Variable "pg_cnt" does
not exist.
For the other one /home, everything work.
I don't understand what is happening here ?
I'm using the silex-skeleton
Your code should work as is, perhaps try to switch it up to:
<?php
require_once __DIR__.'/../vendor/autoload.php';
$app = require __DIR__.'/../src/app.php';
require __DIR__.'/../config/dev.php';
require __DIR__.'/../src/controllers.php';
$pg_cnt['temp'] = 'Welcome !';
$pg_cnt['name'] = 'tutu';
$func = function () use ($app, $pg_cnt) {
return $app['twig']->render('index.html.twig', array(
'pg_cnt' => $pg_cnt,
));
};
$app->get('/', $func);
$app->get('/home', $func);
$app->run();
Related
I have the following set up below.
My issue is I'm not sure how to convert an inline echo $_GET. Is it just the same as {{ fn( echo $_GET['units'] === 'Miles' ? 'selected' : null ) }}??
<form method="get" action="{{ post.link }}">
<div>
<span>Find a Store within</span>
<input name="proximity" type="number" placeholder="15" value="<?php echo $_GET['proximity'] ?>" />
<select name="units">
<option value="Miles" <?php echo $_GET['units'] === 'Miles' ? 'selected' : null; ?>>Miles</option>
<option value="K" <?php echo $_GET['units'] === 'K' ? 'selected' : null; ?>>Km</option>
</select>
<span>from</span>
<input name="origin" type="text" placeholder="Your Address" value="<?php echo $_GET['origin'] ?>" />
</div>
<div>
<input type="submit" value="Search" />
Reset
</div>
</form>
Thanks to #DarkBee for pointing the above out: Passing PHP global variables via Twig/Timber
To access $_POST variables, use this:
{{ request.post.name_of_var }}
To access $_GET variables, use this:
{{ request.get.name_of_var }}
So that would make this:
<input name="origin" type="text" placeholder="Your Address" value="<?php echo $_GET['origin'] ?>" />
Be this:
<input name="origin" type="text" placeholder="Your Address" value="{{ request.get.origin}}
I've got a site where I display specific values of a stock in a table and then should be able to change those values again when clicking on a button, that opens a modal with a form inside.
Now this is my first flask app and I’ve got a problem in overwriting my “original” data with the new Data I’m getting from my form. I don't really know how I can do that. My code looks like this:
views.py
#app.route("/stock-editor", methods=['GET', 'POST'])
def stock_editor():
index = Index(initial_date=app.config['INCEPTION_DATE'], initial_value=10000)
end_date = datetime.today()
start_date = app.config['INCEPTION_DATE']
empty_stocks = dict()
for symbol, shares in index.share_distribution.items(): # Here I get every empty stock and put inside a dict so that I can show it on my page
stock_value = StockValue.query.filter(StockValue.value_eur == 0.000000, StockValue.identifier == symbol, StockValue.date <= end_date, StockValue.date >= start_date).all()
if stock_value:
empty_stocks[symbol] = stock_value
if request.method == 'POST': # this is where I am failing, more info below
result = request.form
new_stock_value = request.values.get('new-stock-value')
new_source_value = request.values.get('new-source-value')
return render_template('stock-editor.html', result=result, empty_stocks=empty_stocks, start_date=start_date, end_date=end_date, new_stock_value=new_stock_value, new_source_value=new_source_value)
else:
return render_template('stock-editor.html', empty_stocks=empty_stocks, start_date=start_date, end_date=end_date)
The html:
<table class="table">
<tbody>
{% for symbol, stock_values in empty_stocks.items() %}
<tr class="table-active">
<th>Aktie</th>
<th>Datum</th>
<th>Wert</th>
<th colspan="2">Quelle</th>
</tr>
{% for value in stock_values %}
<tr>
<th>{{ symbol }}</th>
<td>{{ value.date.strftime('%d.%m.%Y')}}</td>
<td>{{ '%d' % value.value_eur }}</td>
<td>{{ value.source }}</td>
<td>
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#modal-{{ value.id }}">Bearbeiten <i class="fas fa-pencil-alt"></i></button>
<!-- The Modal -->
<div class="modal fade" id="modal-{{ value.id }}">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Aktie bearbeiten</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<form method="POST">
<label for="new-stock-value" class="col-form-label">Neuer Wert:</label>
<input type="number" name="new-stock-value" class="form-control" id="new-stock-value" placeholder="{{ '%d' % value.value_eur }}">
<label for="new-source-value" class="col-form-label">Quelle:</label>
<input type="text" name="new-source-value" class="form-control" id="new-source-value" placeholder="{{ value.source }}">
<!-- Modal footer -->
<div class="modal-footer">
<button type="submit" class="btn btn-success">Ändern</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Abbrechen</button>
</div>
</form>
</div>
</div>
</div>
</div>
</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
So in the if request.method == 'POST' I am getting my new values (new_stock_value and new_source_value) but I’m a little lost in HOW to actually set them as the new value of the stock where I am setting those new values?
Add a hidden field like below to the modal form,
<input type='hidden' name='id' value='{{ value.id }}'>
The above code snippet allows you to send the id of the current stock which is being edited. And in the route you can grab this id by,
id = request.values.get('id')
Using this id, update the empty_stocks dictionary with the new value of stock.
for symbol, stock_values in empty_stocks:
for value in stock_values:
if value.id == id:
value.value_eur = request.values.get('new-stock-value')
value.source = request.values.get('new-source-value')
break
I hope this helped you.
I would like to wrap the checkboxes with custom HTML in twig template when Symfony3 renders the form.
Instead of this:
<input type="checkbox" id="form_role_1" name="form[role][]" value="1" />
<label for="form_role_1">ROLE 1</label>
<input type="checkbox" id="form_role_2" name="form[role][]" value="2" />
<label for="form_role_2">ROLE 2</label>
<input type="checkbox" id="form_role_3" name="form[role][]" value="3" />
<label for="form_role_3">ROLE 3</label>
I would like to get something like this:
<div class="input-group">
<input type="checkbox" id="form_role_1" name="form[role][]" value="1" />
<label for="form_role_1">ROLE 1</label>
</div>
<div class="input-group">
<input type="checkbox" id="form_role_2" name="form[role][]" value="2" />
<label for="form_role_2">ROLE 2</label>
</div>
<div class="input-group">
<input type="checkbox" id="form_role_3" name="form[role][]" value="3" />
<label for="form_role_3">ROLE 3</label>
</div>
in the controller action:
$form = $this->createForm(RolesFormType::class, $roles);
$form->handleRequest($request);
return $this->render(
'role/edit_roles.html.twig',
array(
'form' => $form->createView()
)
);
in the form type:
class RolesFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('role', EntityType::class, array(
'label' => 'form.elements.roles',
'class' => 'RoleBundle\Entity\Role',
'choice_label' => 'role_name',
'expanded' => true,
'multiple' => true
));
}
}
the classic template looks like this:
<div class="container-fluid">
<div class="row">
{{ form_start(form, {'method': "POST"}) }}
<div class="col-md-12">
<div class="form-group">
{{ form_errors(form.role) }}
{{ form_widget(form.role, {}) }}
</div>
</div>
{{ form_rest(form) }}
{{ form_end(form) }}
</div>
</div>
I tried it this way in the twig template, but only the labels were rendered:
<div class="container-fluid">
<div class="row">
{{ form_start(form, {'method': "POST"}) }}
<div class="col-md-12">
{% for role in form.role %}
<div class="input-group">
{{ form_label(role) }}
{{ form_widget(role) }}
</div>
{% endfor %}
</div>
{{ form_rest(form) }}
{{ form_end(form) }}
</div>
</div>
Is ii possible without creating a new field type?
solution
concerning #xabbuh's answer I found the solution at How to Customize Form Rendering in Symfony 3
I created a folder in my Bundle:
src/RoleBundle/Resources/views/form
and I put a new file (fields.html.twig) into it with this content
{%- block choice_widget_expanded -%}
<div {{ block('widget_container_attributes') }}>
{%- for child in form %}
<div class="input-group">
{{- form_widget(child) -}}
{{- form_label(child, null, {translation_domain: choice_translation_domain}) -}}
</div>
{% endfor -%}
</div>
{%- endblock choice_widget_expanded -%}
In the meantime I found another question which deals with this problem:
Overriding symfony radio widget
You can customise the rendering of your form using a custom form theme. You can so either on the project level or even do that just for a particular form (see How to Customize Form Rendering and How to Work with Form Themes for more information).
Your example looks like you need to override the choice_widget and choice_label
When I play with symfony is not very skilled, I don't often use this accepted solution to solve it
view:
<label class="layui-form-label">所属地市:</label>
<div class="layui-inline">
<select name="{{ form.cityId.vars.full_name }}" lay-verify="" id="{{ form.cityId.vars.id }}" lay-filter="case_city">
<option value="0">请选择</option>
{% if citylist is not empty %}
{% for list in citylist %}
<option value="{{ list.id is defined ? list.id : 0 }}">{{ list.areaName is defined ? list.areaName : '' }}</option>
{% endfor %}
{% endif %}
</select>
</div>
controller:
$caseReg = new CaseRegister();
$form = $this->createForm('Alarm\Component\Form\CaseRegisterType', $caseReg);
$form->handleRequest($request);
$citylist = $this->getDoctrine()->getRepository('AlarmComponent:OfficeArea')->findBy(array('pid'=>0,'status'=>0));
$caseType = $this->getDoctrine()->getRepository('AlarmComponent:CaseType')->findBy(array('status'=>0));
if ($form->isSubmitted()) {
$em = $this->getDoctrine()->getManager();
$param = $request->request->get('alarm_component_caseregister');
if($param['receiveId'] == 0)
{
$case = new CaseType();
$case->setCaseName($param['receiveName']);
$em->persist($case);
$em->flush($case);
$caseReg->setReceiveId($case->getId());
$caseReg->setReceiveName($case->getCaseName());
}
if($param['cityId'] != 0)
{
$city = $this->getDoctrine()->getRepository('AlarmComponent:OfficeArea')->find($param['cityId']);
$caseReg->setCityId($city);
}
if($param['subofficeId'] != 0)
{
$suboffice = $this->getDoctrine()->getRepository('AlarmComponent:OfficeArea')->find($param['subofficeId']);
$caseReg->setSubofficeId($suboffice);
}
$em->persist($caseReg);
$em->flush($caseReg);
return $this->redirectToRoute('case_register_index');
}
return $this->render('AlarmWebBundle:BackendBat\CaseRegister:new.html.twig', array(
'caseReg' => $caseReg,
'form' => $form->createView(),
'citylist' => $citylist,
'casetype' => $caseType
));
It is also can solve my problem, but when I was late to optimize this or they must be altered. But I hope this answer can solve your current problem.
I am trying to set the "to" address for an email sent via an html form utilizing phpmailer. The end goal is to utilize a droplist to let the user select which person they are contacting depending on their needs. However, as I don't know much about PHP I'm going step by step and am already stuck.
I'm using the example that came with the site I purchased, but trying to modify it to include a to field. Right now I'm simply trying to get a new variable to show up but have not succeeded. This is the HTML form I have in place right now:
<form name="contact" method="post" action="phpmailer.php" class="af-form" id="af-form">
<div class="af-outer af-required">
<div class="af-inner">
<label for="toemail" id="toemail_label">To Whom:</label>
<input type="text" name="toemail" id="toemail" size="30" value="" class="text-input span8"/>
<label class="error" for="toemail" id="toemail_error">To field is required.</label>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<label for="name" id="name_label">Your Name:</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input span8"/>
<label class="error" for="name" id="name_error">Name is required.</label>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<label for="email" id="email_label">Your Email:</label>
<input type="text" name="email" id="email" size="30" value="" class="text-input span8"/>
<label class="error" for="email" id="email_error">Email is required.</label>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<label for="input-message" id="message_label">Your Message:</label>
<textarea name="message" id="input-message" cols="30" class="text-input span8"></textarea>
<label class="error" for="input-message" id="message_error">Message is required.</label>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<input type="submit" name="submit" class="form-button btn btn-large" id="submit_btn" value="Send Message!" />
</div>
</div>
The name, email, and message fields get picked up fine and populate in the email I recieve. However, the "toemail" field doesn't show up. Here is the process.php I am using for phpmailer.
<?php
if ((isset($_POST['toemail'])) && (strlen(trim($_POST['toemail'])) > 0)) {
$toemail = stripslashes(strip_tags($_POST['toemail']));
} else {$toemail = 'No to address entered';}
if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
$name = stripslashes(strip_tags($_POST['name']));
} else {$name = 'No name entered';}
if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 0)) {
$email = stripslashes(strip_tags($_POST['email']));
} else {$email = 'No email entered';}
if ((isset($_POST['message'])) && (strlen(trim($_POST['message'])) > 0)) {
$message = stripslashes(strip_tags($_POST['message']));
} else {$message = 'No phone entered';}
ob_start();d
?>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<table width="550" border="1" cellspacing="2" cellpadding="2">
<tr bgcolor="#eeffee">
<td>To Whom:</td>
<td><?=$toemail;?></td>
</tr>
<tr bgcolor="#eeffee">
<td>Name</td>
<td><?=$name;?></td>
</tr>
<tr bgcolor="#eeeeff">
<td>Email</td>
<td><?=$email;?></td>
</tr>
<tr bgcolor="#eeffee">
<td>Message</td>
<td><?=$message;?></td>
</tr>
</table>
</body>
</html>
<?
$body = ob_get_contents();
$to = 'personalemailtousefortesting.com';
$email = 'email#example.com';
$fromaddress = "you#example.com";
$fromname = "Online Contact";
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = "mail#yourdomain.com";
$mail->FromName = "Contact Form";
$mail->AddAddress("mypersonalemailfortestingpurposes#gmail.com","Name 1"); // addresses here
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Contact form submitted";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
if(!$mail->Send()) {
$recipient = 'mypersonalemailfortestingpurposes#gmail.com';
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content, "From: mail#yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
?>
The email I get populates the name, email, and message fields fine, however, I get my fall back error message of 'No to address entered' for the "toemail" input.
This is issue number one. I'll post the phpmailer.php that I'm using if anyone needs to see it. I do not know if I need to define the variable in there first.
After I get that working, I need to make it work for a dropdown list so the user cannot just send the email to whomever they would like.
THEN, I need to set that as the actual send-to address. I do not know how to set the actual address for sending purposes. I would assume that I change
$mail->AddAddress("mypersonalemailfortestingpurposes#gmail.com","Name 1"); // addresses here
to something like...
$mail->AddAddress($toemail,"Name 1"); // addresses here
...but odds are that's not correct.
Any help you can provide would be wonderful.
If you need to see my phpmailer.php I can post that too.
Thanks again,
-Rocky Colt TumTum
Just incase you didn't find this, this is what was listed on the PHP Manual site.
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
I have been trying to come up with a special 2 column layout.
Basically these are the requirements
Fixed width and centered
Height 100%
Header and Footer always visible
2 column content ( Main - Right )
Main column scroll on overflow
Right column fixed
Here is the code I have so far. I just haven't been able to center
the entire layout.
Live at www.onlinesportcenter.com
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SPECIAL 2 COLUMN LAYOUT</title>
<style type="text/css">
body {
margin:0;
border:0;
padding:0;
height:100%;
max-height:100%;
background:#fff;
font-family:arial, verdana, sans-serif;
font-size:76%;
overflow: hidden;
}
* html body {
padding:120px 0 20px 0;
}
#header {
position:absolute;
top:0;
left:0;
width:1004px;
height:119px;
overflow:auto;
background:#73a2bd;
border-bottom:1px solid #35759a;
text-align:center;
}
* html #header {height:120px;}
#header p {
color:#fff;
margin:2px 0 0 0;
}
#header ul {
clear:both;
text-align:center;
border-top:1px solid #73a2bd;
}
#header ul {
margin:0;
padding:0;
list-style-type:none;
background:transparent;
height:3em;
}
#header ul li {
display:inline;
color:#73a2bd;
}
#container {
font-family: Arial, sans-serif;
font-size: 1.2em;
position:fixed;
top:120px;
left:0;
bottom:20px;
right:0;
background:#fff;
padding:0;
width:1004px;
}
* html #container {
height:100%;
width:1004px;
}
#container img {margin:5px;}
#main {
width:774px;
height:100%;
float:left;
overflow:auto;
}
#right {
width:228px;
height:100%;
float:right;
border-left:1px solid #73a2bd;
border-right:1px solid #73a2bd;
}
#footer {
position:absolute;
bottom:0;
left:0;
width:1004px;
height:19px;
overflow:auto;
text-align:center;
background:#73a2bd;
border-top:1px solid #35759a;
}
* html #footer {height:20px;}
#footer p {
color:#fff;
margin:2px 0 0 0;
}
</style>
</head>
<body>
<div id="header">
<p>HEADER</p>
</div>
<div id="container">
<div id="main">
<p>
MAIN
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
</p>
</div>
<div id="right">
<p>
RIGHT
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
</p>
</div>
</div>
<div id="footer">
<p>FOOTER</p>
</div>
</body>
</html>
Wrap all top-level elements (header, container and main) in another div, give it a set width and a the following margin: "margin:0 auto;"
EDIT: I take my words back. I misunderstood your initial problem. For header and footer you need to wrap them in a absolute positioned div anchored to the top or bottom. Make sure those elements have width to 100%. Then set margin to "0 auto" for inside elements. I'll post a JS Fiddle example in a sec.
EDIT 2: Here's what I got so far: http://jsfiddle.net/NfSKf/