Blog

SmoothAnchors jQuery plugin

Permalink

Most web browsers now use smooth scrolling when you hit spacebar or use the page up/down shortcuts. I like this behaviour because it helps you keep your bearings as you navigate through a page — but for some reason modern web browsers don’t animate to internal anchor locations and insist on skipping straight to them.

To extend this to anchor links, I’ve written a jQuery plugin to easily add smooth scrolling to all anchored locations on a page with no need to make any changes to your markup.

The plugin is really easy to use. You just include the .js file and initialise the plugin like this:

If you’re using jQuery 1.7+, the plugin will use the jQuery .on() function to work with links that have been added after the plugin has been initialised (i.e. if you’re dynamically adding content to your page).

How to manually build CMS Made Simple forms

Permalink

I’ve recently migrated my website to CMS Made Simple, and something that I struggled to get working was the FormBuilder module. I had a contact form on my old site, so I wanted to bring that across to CMSMS. The problem is that it doesn’t support extensionless pretty URLs. I considered adding a .html or .php extension to the end of my URLs, but in the interest of keeping the URLs relevant and easily migratable in the future, I decided against it.

I was just about to give up and do it in PHP, but then I found a 4 year old forum post by calguy1000 describing how to submit forms using Smarty and User Defined Tags (UDTs).

Code for ‘cmsmailer’ UDT:

global $gCms;
$smarty = & $gCms->GetSmarty();
$cmsmailer = $gCms->GetModuleInstance('CMSMailer');
$smarty->assign_by_ref('cmsmailer',$cmsmailer);

You may notice that there’s a tiny difference between my UDT and calguy1000’s. His references the CMSMailer module by calling & $gCms->modules['CMSMailer']['object'];, but for me this caused a few errors so I checked the CMSMailer documentation and discovered that it should now be referenced like this: $gCms->GetModuleInstance('CMSMailer');.   Basically, the UDT provides us with a way to access CMSMailer. We can use some Smarty functions to check POST values, and work out what needs to be displayed and whether the form was valid. If it’s all valid, we send the message - otherwise we display some validation errors.

Below is an example of how to use the UDT in a contact form. A lot of it is taken from calguy1000’s post, but I’ve made a few improvements to it. Remember to replace YOUR-EMAIL-ADDRESS@EXAMPLE.COM with the email address that you want to receive the form submission.

The easiest way to use this is to paste it in to a Global Content Block, and then reference that in the content of one of your pages.

{if isset($smarty.post.contactformsubmit)}
    
    {$assign "recipient" "YOUR-EMAIL-ADDRESS@EXAMPLE.COM"}

    {$assign "isValid" "true"}

    <div class="validation-errors">
    {* Check for any invalid responses. *}
    {if empty($smarty.post.name)}
        <p><font color="red">Please enter your name</font></p>
        {$assign "isValid" "false"}
    {/if}
    {if empty($smarty.post.email)}
        <p><font color="red">Please enter your email address</font></p>
        {$assign "isValid" "false"}
    {/if}
    {if empty($smarty.post.subject)}
        <p><font color="red">Please enter a subject</font></p>
        {$assign "isValid" "false"}
    {/if}
    {if empty($smarty.post.message)}
        <p><font color="red">Please enter a message</font></p>
        {$assign "isValid" "false"}
    {/if}
    </div>

    {* All good - send the message *}
    {if $isValid != "false"}
        <!--
        {* prepare the message subject *}
        {capture assign='subject'}Website form submission: {$smarty.post.subject}{/capture}
        
        {* prepare the message body *}
        {capture assign='body'}
            Message From: {$smarty.post.name} ({$smarty.post.email})
            Message Sent:  {$smarty.now|cms_date_format}
            Message Text:
            {$smarty.post.message}
        {/capture}

        {* send it *}
        {cmsmailer}
        {$cmsmailer->reset()}
        {$cmsmailer->AddAddress($recipient)}
        {$cmsmailer->SetSubject($subject)}
        {$cmsmailer->SetBody($body)}
        {capture assign='junk'}{$cmsmailer->Send()}{/capture}
        -->
        <p>Thanks for getting in touch!</p>
    {/if}
{/if}
{if $isValid == "false" || !isset($smarty.post.contactformsubmit)}
    {* Display the form if it: 1. Wasn't valid; or 2. Hasn't been submitted yet. *}
    <form name="contact-form" id="contact-form" method="post">
        <div>
            <label for="name">Name:</label>
            <input type="text" name="name" id="name" class="formfield" value="{if isset($smarty.post.name)}{$smarty.post.name}{/if}" />
        </div>
        <div>
            <label for="email">Email:</label>
            <input type="email" name="email" id="email" class="formfield" value="{if isset($smarty.post.email)}{$smarty.post.email}{/if}" />
        </div>
        <div>
            <label for="subject">Subject:</label>
            <input type="text" name="subject" id="subject" class="formfield" value="{if isset($smarty.post.subject)}{$smarty.post.subject}{/if}" />
        </div>          
        <div>
            <label for="message">Comments:</label>
            <textarea name="message" id="message" rows="10" class="formfield">{if isset($smarty.post.message)}{$smarty.post.message}{/if}</textarea>
        </div>
        <div><input type="submit" name="contactformsubmit" id="submitbutton" value="Send"></div>
    </form>
{/if}

Once you've got all that server-side stuff working, you can obviously add client-side validation for a smoother experience and a bit less work on the server.

See something like this in action on my about page.

GitHub 'Clone in Mac' for Chrome

Permalink

About 6 months ago, GitHub released GitHub for Mac - a very useful (but sometimes slightly crashy) app to keep all your repositories organised. If you’re using Safari, you get this handly little “Clone in Mac” button when you visit a repository page on GitHub. Clicking that will open up GitHub for Mac and easily set you up with a clone of that project.

Screenshot of the Clone in Mac button on GitHub 

I switch back and forth between Safari and Chrome every now and then, and one day when I was sifting through a heap of projects on Cocoa Controls (a great resource for Mac & iOS devs), I noticed that this button isn’t there in Chrome.

So I went ahead and made a Chrome extension to put that button in there, and set up my first GitHub repository to host it on.

If you want to install the extension, head over to the GitHub repo and download it.