Bending Form Inputs to my Will with Smarty -- MUAHAHAHAHAHA
Posted: Fri Aug 30, 2013 11:13 am
First my confession: I've nearly stopped designing original websites and I now just buy a responsive template from ThemeForest. Best $15 bucks I've ever spent (per site). I only use it as a starter theme and it is usually unrecognisable when I've finished. But the code... ...there is so much to work with.
The downside is that I have to shoehorn the code into CMSms and sometimes it doesn't all play well together.
Now a tricky bit is the forms. The ThemeForest themes have great forms but they usually require very specific multiple classes, id's, placeholder text etc.
And CMSms generates most form inputs from the core, so classes, id's and other attributes are fixed.
So this is how I bend them to my will:
This is what I did with the Self Registration 'Registration Template 1'.
1) I added this: to the end of the default Self Reg template (Registration Template 1).
This prints out an array of all the controls, this is the first one - email address as username:
2) The form input from the ThemeForest theme that I need to duplicate looks like this:
After checking with FireBug I found the id isn't used for styling or anything else so it could change. This class needed to replace the default class in the Self Reg form input and the placeholder needed to be carried over.
3) Next I took the Self Reg default input and the ThemeForest theme input and put them together.
Based on what I saw here I could swap the class and then I needed to find something else to swap out for the placeholder. So I chose to lose the 'size="40"' because this was being set in the CSS.
4) So this is where Smarty does its magic. Smarty can use 'replace' to replace arrays. With this I built a Smarty find & replace.
I set up the 'find' variable with the things I wanted replaced.
Then the 'replace' variable with the values from the ThemeForest theme form input.
Finally I inserted a separate named control (plus find & replace vars) into its appropriate input field in the ThemeForest theme code.
Three fields look like this:
And the output of the code above looks like this:
5) And last but not least, how to change the standard Submit input into a very styleable <button>.
This is the Submit input generated by Self Reg default template:
And this is the <button> from my ThemeForest theme:
So I just added the 'id' and 'name' values from the Self Reg input into the button code:
Then the finished form ended up looking like the image below.
Hope this helps somebody.
Simon66
The downside is that I have to shoehorn the code into CMSms and sometimes it doesn't all play well together.
Now a tricky bit is the forms. The ThemeForest themes have great forms but they usually require very specific multiple classes, id's, placeholder text etc.
And CMSms generates most form inputs from the core, so classes, id's and other attributes are fixed.
So this is how I bend them to my will:
This is what I did with the Self Registration 'Registration Template 1'.
1) I added this:
Code: Select all
{$controls|@print_r}
This prints out an array of all the controls, this is the first one - email address as username:
Code: Select all
[username] => stdClass Object (
[color] => blue
[marker] => *
[required] => 1
[prompt] => Email Address
[name] => username
[control] => <input id="mc4758sr_input_username" class="cms_textfield" type="text" maxlength="40" size="40" value="" name="mc4758sr_input_username">
)
Code: Select all
<label for="txt_email">
<input id="txt_email" class="txt fill-width" type="email" placeholder="Enter your e-mail address"/>
</label>
3) Next I took the Self Reg default input and the ThemeForest theme input and put them together.
Code: Select all
<input id="mc4758sr_input_username" class="cms_textfield" type="text" maxlength="40" size="40" value="" name="mc4758sr_input_username">
<label for="txt_email">
<input id="txt_email" class="txt fill-width" type="email" placeholder="Enter your e-mail address"/>
</label>
4) So this is where Smarty does its magic. Smarty can use 'replace' to replace arrays. With this I built a Smarty find & replace.
I set up the 'find' variable with the things I wanted replaced.
Then the 'replace' variable with the values from the ThemeForest theme form input.
Finally I inserted a separate named control (plus find & replace vars) into its appropriate input field in the ThemeForest theme code.
Code: Select all
{assign var=find value=['cms_textfield', 'size="40"']}
{assign var=replace value=['txt fill-width', 'placeholder="Enter your e-mail address"']}
{$username->control|replace:$find:$replace}
Code: Select all
<div class="form-action">
<label for="mc4758sr_input_fullname">
{assign var=find value=['cms_textfield', 'size="80"']}
{assign var=replace value=['txt fill-width', 'placeholder="Enter full name"']}
{$fullname->control|replace:$find:$replace}
</label>
<div class="wrap-2col clearfix">
<div class="col">
<label for="mc4758sr_input_username">
{assign var=find value=['cms_textfield', 'size="40"']}
{assign var=replace value=['txt fill-width', 'placeholder="Enter your e-mail address"']}
{$username->control|replace:$find:$replace}
</label>
<label for="mc4758sr_input_username_again">
{assign var=find value=['cms_textfield', 'size="40"']}
{assign var=replace value=['txt fill-width', 'placeholder="Re-enter your e-mail adress"']}
{$username_again->control|replace:$find:$replace}
</label>
</div>
Code: Select all
<div class="form-action">
<label for="mc4758sr_input_fullname">
<input type="text" class="txt fill-width" name="mc4758sr_input_fullname" id="mc4758sr_input_fullname" value="" placeholder="Enter full name" maxlength="255" />
</label>
<div class="wrap-2col clearfix">
<div class="col">
<label for="mc4758sr_input_username">
<input type="text" class="txt fill-width" name="mc4758sr_input_username" id="mc4758sr_input_username" value="" placeholder="Enter your e-mail address" maxlength="40" />
</label>
<label for="mc4758sr_input_username_again">
<input type="text" class="txt fill-width" name="mc4758sr_input_username_again" id="mc4758sr_input_username_again" value="" placeholder="Re-enter your e-mail adress" maxlength="40" />
</label>
</div>
This is the Submit input generated by Self Reg default template:
Code: Select all
<input class="cms_submit" name="mc4758sr_submit" id="mc4758sr_submit" value="Submit" type="submit" />
Code: Select all
<button class="btn btn-red btn-submit" type="submit">Register</button>
Code: Select all
<button id="mc4758sr_submit" class="btn btn-red btn-submit" type="submit" name="mc4758sr_submit">Register</button>
Hope this helps somebody.
Simon66