Perry
August 3, 2016, 11:11pm
1
Hello,
what is the best solution to include a form snippet into a page, but not at a fixed position ?
My idea is to include the snippet in the template and then moved with
js on the right place.
$('div#my-form').appendTo(".include-my-form");
Is there a solution without js for example with kirby tag’s ?
<?php
kirbytext::$tags['div'] = array(
'attr' => array(
'class_name'
),
'html' => function($tag) {
$class_name = $tag->attr('div');
return '<div class="'.$class_name.'">test</div>';
}
);
?>
(div: include-my-form)
Well, you’re almost there! If you want to include a snippet in the textarea, you’ll have to create a custom tag, called ‘snippet’ or something. Texnine explains it here:
https://forum.getkirby.com/t/embedding-snippets/900/4
What is ‘the right place’ in this case? If you want full control, then you can try Tim Oetting’s Kirby Builder plugin, which gives you ‘modular blocks’ to create pages with. One of these blocks can be your form. But maybe that’s a too hightech solution to your problem :).
You can simply include a div in html in your text field:
# Your title
text bla blab bla
<div id="my-form"></div>
More text ...
Better yet, define a text snippet:
# Your title
text bla blab bla
{{form}}
More text ...
And then in your config file:
c::set('kirbytext.snippets', array(
'form' => '<div id="my-form"></div>',
));
Read more about text snippets: https://getkirby.com/docs/developer-guide/kirbytext/filters
1 Like
I wouldn’t do this via javascript. You could create a snippet and then use a kirbytag to include the snippet in the text as you thought. Does it not work as expected?
Perry
August 4, 2016, 8:35am
6
I tried with this:
tag
<?php
kirbytext::$tags['snippet'] = array(
'attr' => array(
),
'html' => function($tag) {
$file = $tag->attr('snippet');
return snippet($file, array(), true);
}
);
my embed snippet:
<?php
//FORM
$form_input_text="CHF";
$text_0='Veranstalter';
$text_1='Titel der Veranstaltung';
$text_2='E-Mail';
$text_3='Nachricht';
$text_4='Kalenderwoche';
$prepayment_send_text='senden';
$wait='Einen Augenblick bitte, die Daten werden gesendet.';
$form_val_error='Bitte alle Felder ausfüllen.';
$thx='Vielen Dank für Ihre Reservierung,<br>
Sie erhalten in Kürze eine Bestätigung <br>
per E-Mail.';
$txt_1='';
?>
<div id="formular" class="aaoc">
<h3>
<?php echo $txt_2 ?>
</h3>
<form method="post">
<div class="text">
<?php echo $txt_1 ?>
</div>
<li><input type="text" id="field-0" placeholder="<?php echo $text_0 ?>" /></li>
<li><input type="text" id="field-1" placeholder="<?php echo $text_1 ?>" /></li>
<li><input type="e-mail" id="field-2" placeholder="<?php echo $text_2 ?>" /></li>
<li><strong>Kultursäulen</strong></li><br>
<li>Format</li>
<li>
<select id="select-0">
<option value = "DinA3">Din A3</option>
<option value = "DinA2">Din A2</option>
</select>
</li>
<li><input id="select-1" type="number" value="" min="1" max="52" placeholder="<?php echo $text_4 ?>"></li>
<li><input id="select-2" type="number" value="" min="1" max="52" placeholder="<?php echo $text_4 ?>"></li>
<li><input id="select-3" type="number" value="" min="1" max="52" placeholder="<?php echo $text_4 ?>"></li>
<li><input id="select-4" type="number" value="" min="1" max="52" placeholder="<?php echo $text_4 ?>"></li>
<li><strong>Plakatwände (nur DinA 3 möglich)</strong></li><br>
<li><input id="select-5" type="number" value="" min="1" max="52" placeholder="<?php echo $text_4 ?>"></li>
<li><input id="select-6" type="number" value="" min="1" max="52" placeholder="<?php echo $text_4 ?>"></li>
<li><input id="select-7" type="number" value="" min="1" max="52" placeholder="<?php echo $text_4 ?>"></li>
<li><input id="select-8" type="number" value="" min="1" max="52" placeholder="<?php echo $text_4 ?>"></li>
<li><textarea id="field-3" rows="10" cols="49" placeholder="<?php echo $text_3?>" ></textarea></li>
<li><input id="bt_send" type=submit value="<?php echo $prepayment_send_text ?>" ></li>
</form>
<div id="messages">
<div id="form-message-1">
<?php echo $form_val_error ?>
</div>
<div id="form-message-2">
ups, error please try later.
</div>
<div id="form-message-3">
<?php echo $wait ?>
</div>
<div id="form-message-4">
<?php echo $thx ?>
</div>
</div>
</div>
?>
the output is the raw html:
<div id="formular" class="aaoc">
<h3>
</h3>
<form method="post">
<div class="text">
</div>
<li><input type="text" id="field-0" placeholder="Veranstalter" /></li>
<li><input type="text" id="field-1" placeholder="Titel der Veranstaltung" /></li>
<li><input type="e-mail" id="field-2" placeholder="E-Mail" /></li>
<li><strong>Kultursäulen</strong></li><br>
.
.
.
.
If the error is php and html mixed in the embed snippet?
Can’t test this right now, do you get any errors?
Your $txt_2 doesn’t exist
and $txt_1 is set as an empty string.
Is that on purpose?
We’re seeing some actual bullet points in your output, is that the case or something is weird in the display?
@Perry : Have you enable debugging in your config.php? If not, I suggest to do so.
Perry
August 4, 2016, 10:52am
10
@all :
now it works, the problem was the whitespaces in the html/php snippet.
thanks you for your help
@texnixe
c::set('debug',true);
thanks verry helpful