Kirby

Is it possible to create uniform on all pages

Hi there I would like to create snippet in the footer that will catch emails and send them to mail software.

how can I use this form on all pages? Shoud I add controllers for every template where is loading subscribe snippet? Or it could be more simple way?

You can put the logic directly into the form snippet.

I’m try, snippet don’t load at all…

<?php

use Uniform\Form;

return function ($kirby) {
   $subscribeform = new Form([
      'email' => [
         'rules' => ['required', 'email'],
         'message' => 'Please provide a valid email',
      ],
   ]);

   if ($kirby->request()->is('POST')) {
	  $form->honeypotGuard(['field' => 'url']) 
      ->emailAction([
         'to' => 'info@domain.com',
         'from' => 'no-reply@domain.com',
      ]);
   }

   return compact('subscribeform');
};
?>

<section class="bg-dark">
	<div class="container">
		<div class="row justify-content-center">
			<div class="col-xl-8 col-lg-10"> <span class="d-block h3 text-white "><?= t('never miss an update') ?></span>
				<?php if ($subscribeform->success()): ?>
					<div class="col-lg-6 mt-5 alert alert-success" role="alert"><?= t('hooray! your message has been successfully sent.') ?></div>
				<?php endif; ?>
				<form class="row align-items-center mb-2" action="<?php echo $page->url('subscribe') ?>" method="POST">
					<div class="col-md-9 input-group input-group-lg mb-2 mb-md-0">
						<div class="input-group-prepend">
							<span class="input-group-text">
								<i class="material-icons">mail_outline</i>
							</span>
						</div>
						<input type="email" class="form-control <?php if ($subscribeform->error('email')): ?>is-invalid<?php endif; ?>" placeholder="<?= t('enter your email address') ?>" aria-label="<?= t('enter your email address') ?>" id="email" name="email">
						<?php if ($subscribeform->error('email')): ?>
					    	<div class="invalid-feedback"><?php echo implode($subscribeform->error('email')) ?></div>
						<?php endif; ?>
					</div>
					<div class="col-md-3">
						<button class="btn btn-lg btn-block btn-primary" type="submit"><?= t('subscribe') ?></button>
					</div>
				</form>
				<div class="d-md-flex align-items-center"> <span class="d-block text-small text-white"><?= t('we\'ll never share your email with third parties') ?></span> </div>
			</div>
		</div>
	</div>
</section>

You can’t use controller syntax in a snippet.

not sure I understand how to do this other way…

By removing the anonymous function:

<?php

use Uniform\Form;

   $subscribeform = new Form([
      'email' => [
         'rules' => ['required', 'email'],
         'message' => 'Please provide a valid email',
      ],
   ]);

   if ($kirby->request()->is('POST')) {
	  $form->honeypotGuard(['field' => 'url']) 
      ->emailAction([
         'to' => 'info@domain.com',
         'from' => 'no-reply@domain.com',
      ]);
   }
1 Like