<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Áine Poole on Medium]]></title>
        <description><![CDATA[Stories by Áine Poole on Medium]]></description>
        <link>https://medium.com/@toru_aine?source=rss-bf29e5d7cd70------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lt0Gp-amXT46WFSl4mgBkA.png</url>
            <title>Stories by Áine Poole on Medium</title>
            <link>https://medium.com/@toru_aine?source=rss-bf29e5d7cd70------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 16:43:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@toru_aine/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to get more out out of Mandrill’s HTML Email Templates by using Handlebars.]]></title>
            <link>https://medium.com/@toru_aine/how-to-get-more-out-out-of-mandrills-html-email-templates-by-using-handlebars-7c51feff3764?source=rss-bf29e5d7cd70------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c51feff3764</guid>
            <category><![CDATA[mandrill]]></category>
            <category><![CDATA[gulp]]></category>
            <category><![CDATA[web-design-agency]]></category>
            <category><![CDATA[scss]]></category>
            <category><![CDATA[handlebars]]></category>
            <dc:creator><![CDATA[Áine Poole]]></dc:creator>
            <pubDate>Thu, 30 Jun 2016 11:51:07 GMT</pubDate>
            <atom:updated>2017-05-04T10:22:22.768Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pr1hJYveWyy_OaVEH_zsUw.png" /></figure><p>Being able to create your own styled emails from within the Mandrill site is great, but with inline styling and limited templating options you can soon hit a brick wall when trying to build up more interesting designs.</p><p>This is a brief guide on how to implement an efficient local workflow will let you create templates easily that can be updated really quickly. To do this we are going to use a combination of Gulp, Handlebars.js, SCSS and HTML to make the template.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/931/1*zU4FF_WXBsuZr6W1iqxX8A.png" /></figure><p>Download the code from this tutorial at <a href="https://bitbucket.org/toru_aine/blog_handlebars_mandrill"><em>https://bitbucket.org/toru_aine/blog_handlebars_mandrill</em></a></p><p>Lets start with an introduction to Gulp, Handlebars.js and Mandrill, although we’ll keep it short as there are plenty of other great resources on the web that can run you through the basics, or you can just skip down to ‘Project Setup &amp; Code’.</p><h3>Handlebars.js</h3><p>Handelbars.js is a minimal logic templating language, it allows the coder to use placeholders in a HTML template, that when compiled will be replaced by JSON data and rendered to the end user as regular HTML. This is great because it allows the coder to make templates that have dynamic content. Although what makes Handlebars really cool is that it allows some logic to placed into the template, this means you can keep your template neat and minimal.</p><h3>Mandrill</h3><p>Mandrill is a transactional email API, it is currently running over the MailChimp platform and in many respects the two platforms are very similar. The main features of Mandrill are that emails can be sent to Mandrill via PHP or JavaScript, this allows a lot of flexibility when creating web apps. Mandrill allows custom templates to be created with dynamic content, it also has some basic analytics which can provide some useful insights into bounce rates, open rate, etc.</p><h3>Gulp</h3><p>Gulp is a node.js based task automator. There are many plugins that can be included in the project using the Node Pluging Manager (NPM). These perform specific actions such as LiveReload, compiling SCSS to CSS, processing Handlebars.js, minifying javascript, and so on. Using Gulp you can create different tasks that run independently or chained together, and even setup different tasks depending on parameters sent to gulp on initialisation.</p><p>In the following project you will see how we can use Gulp to compile SCSS to inline styling in the template and how Handlebars can be compiled so the coder can preview the email as the recipient would view the email. So next time you need to change the template don’t worry, its going to be really quick and easy.</p><h3>Project Setup &amp; Code</h3><p>Enough background, lets get to the fun bit, the code. There’s Pizza too but we’ll come back to that later.</p><p>A lot of time can be wasted by copying the template into Mandrill, triggering the email, waiting for the email to arrive, realise something else needs to be tweaked and going back to the start again. You can go through this process several times whilst designing an email.</p><p>What would be really cool is if you could view your email without having to go through all of that, so lets take care of that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/291/1*3d_e5eSpFhYxWRrTYkzOrw.png" /><figcaption>Project Structure</figcaption></figure><p>First let’s look at the folder structure for this project is very simple, there are three folders in the root of the project; <em>input</em>, <em>output</em> and <em>preview</em>. Inside the <em>input</em> folder we have two more folders <em>json</em> and <em>scss. </em>Them there are a couple of other files in the root of the project which can be ignored for now.</p><p>We’re going to start by building up a JSON file which will hold that data for the Handlebars expression, some basics that you would want in the email is the sender’s details and a logo for the company/project the email is coming from, so we’ll make an object with the key of sender and set the email, first and last name as well a providing a link for the logo. Now lets add some actual content the recipient might actually like, so how about a list of Pizzas and their toppings. If we make the list an array, Handlebars will iterate the array for us so our template can not only be efficiently minimal but it means we don’t have to restrict the number of Pizzas that have to be sent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/433/1*hP0L3cFJ4mRXlxxF6L6JPw.png" /><figcaption>Extract from template.handlebars</figcaption></figure><p>Now we have the JSON data we can start to build up the HTML template. In all likelihood most of your HTML template is going to be static with only a few Handlebars expressions, so let not waste time with the regular HTML and instead skip straight to using Handlebars expressions. So straight in at the deep end then, on lines 22 to 26 we have a Handlebars if statement, which uses the sender object we setup in the JSON file earlier. Handlebars also allows iteration, so lets make use of that to loop through the results array, line 31 shows the start of the loop, there is also a closing tag on line 62. Anyone familiar with Angular or jQuery might expect to have a variable ‘<em>result</em>’ that you need to access to get to the Pizza name and topping, but in Handlebars you can directly access the object key in the expression. All fairly simple so far right? That’s as complicated as Handlebars.js gets. As you can see from this snippet of code, there is no (or nearly no) inline styling in the template, so lets sort that next.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*GMu0BFi-4AliOUM08QUeow.png" /><figcaption>Extract from template.scss</figcaption></figure><p>All of the styling for the template is contained in either <em>global.scss</em> or <em>template.scss</em>. In the example included in the references section, we have overridden some of the standard styling from Mandrill, this has gone in the <em>global.scss</em> file as these changes would almost certainly need to be used across all templates in a project. How you structure your SCSS is totally your choice, suffice it to say that all that’s important is that its easy to maintain and that it compiles into the Handlebars template as inline styling.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/505/1*h06NNXwwWRpf8NPy3H5wLA.png" /><figcaption>Snippet of gulpfile.js</figcaption></figure><p>The Gulp file is the shortest file but possibly the most important part of the whole project. The Gulp file is pretty much what you see is what you get, we have an array of templates, here we can add the names of the template files. For each template we will setup its own gulp task, for each template we will create four sub-tasks, one for SCSS, one for inline SCSS, one for Handlebars and finally a watch task. The four tasks need to run in order, so that the SCSS can be compiled in CSS, which is then pulled into the Handlebars template, then Handlebars is compiled with the data from the JSON file. The full gulpfile can be seen in the demo link at the bottom of this post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*q2hhL0kRdt6h2YB2gV494g.png" /><figcaption>Extract of complied output HTML template</figcaption></figure><p>The file in the output folder is what will be copied into Mandrill, as can be seen in the image to the left, the template now includes the inline css but the Handlebars expressions are still there. We need the expressions to be left so that we may pass the data to Mandrill. Even in this small snippet there is duplication of styling and this template doesn’t have an overly complex style, so its not hard to imagine how much duplication there’ll be on a larger, more intricate template.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/521/1*hTAvs06cJzPJvr3HsLAp_w.png" /><figcaption>Extract of the compiled preview HTML template</figcaption></figure><p>The snippet to the left shows how the email code will look when its been sent to the recipient, by compiling the Handlebars expressions, in conjunction with LiveReload, its really quick to fix any bugs with the design and you can see how it will look in a browser with ‘real’ data immediately, no need to wait for Mandrill to send the email anymore.</p><p>Once you are happy that the template is styled and structured as you want you are now ready to add it to Mandrill. Log into Mandrill, create a new template, setup the template name, and copy all of the code from the output HTML template into HTML text area in Mandrill and click Publish. You’re now ready to send a test email. If you do need to tweak any part of the template you can edit it, Gulp will re-build, you preview in browser and when you’re ready you can update the template in Mandrill.</p><p>Here’s the final result…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*6mn31c6Etm4mD5xKeBjvAQ.png" /></figure><p>learn more about html on our website…</p><p><a href="https://www.toruinteractive.com/index.html">Digital Marketing Agency | Toru Interactive</a></p><h4><strong>References</strong></h4><p><strong>Handlebars Tutorial. </strong><a href="http://code.tutsplus.com/tutorials/an-introduction-to-handlebars--net-27761"><em>http://code.tutsplus.com/tutorials/an-introduction-to-handlebars--net-27761</em></a></p><p><strong>Mandrill &amp; Handlebars.</strong> <a href="https://mandrill.zendesk.com/hc/en-us/articles/205582537-Using-Handlebars-for-Dynamic-Content"><em>https://mandrill.zendesk.com/hc/en-us/articles/205582537-Using-Handlebars-for-Dynamic-Content</em></a></p><p><strong>Gulp Tutorial.</strong> <a href="https://scotch.io/tutorials/automate-your-tasks-easily-with-gulp-js"><em>https://scotch.io/tutorials/automate-your-tasks-easily-with-gulp-js</em></a></p><p><strong>Source Code.</strong> <a href="https://bitbucket.org/toru_aine/blog_handlebars_mandrill"><em>https://bitbucket.org/toru_aine/blog_handlebars_mandrill</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c51feff3764" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Clean Code — Part 1]]></title>
            <link>https://medium.com/@toru_aine/clean-code-part-1-4f6c9f04e743?source=rss-bf29e5d7cd70------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f6c9f04e743</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[mobile-app-developers]]></category>
            <category><![CDATA[web-software-development]]></category>
            <category><![CDATA[good-practices]]></category>
            <dc:creator><![CDATA[Áine Poole]]></dc:creator>
            <pubDate>Fri, 12 Feb 2016 14:49:44 GMT</pubDate>
            <atom:updated>2017-05-04T10:24:43.962Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RidkyQAK19fix3sy6TKMgA.png" /></figure><p>In an ever changing software landscape, languages come and go with whatever is fashionable at the time, but certain fundamentals of coding remain the same regardless of the current language. Anyone who has been coding for a significant length of time, will have undoubtedly inherited badly written code, where most of your time is spent attempting to workout what the function is meant to do. Then you make a change, and you get an unexpected result. In addition, you may have even been guilty of leaving bad code yourself. Have you ever had a tight deadline, been in a rush to finish the project, and sacrificed clean code to finish the functionality for the deadline?</p><p>I recently read a book and it said the “difference between good code and bad code could be expressed as ‘<em>WTFs per minute</em>’”. I have to agree, coding big projects which have functionality continually added and removed, it can be a frustrating enough task trying to keep everything working, without having to deal with badly written code into the equation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*kb7FmX-anYDycB4NiHBRUQ.jpeg" /><figcaption>Good code v Bad code</figcaption></figure><p>The main benefits of clean code are; it significantly reduces the amount of time spent trying to deduce the intention of the code. Once you know the intention, you can easily check if it does what its supposed to. Because you can see the intention, updating and debugging code becomes a lot easier, you can begin to see how changing code is going to affect other functions and classes. From my own experience of working a big project, and returning to it once every six months, you start to forget what the function is meant to do, what else does it affect? If you have clean code it’s really easy to see what it’s meant to do, and developers who have never worked on the project can come in and quickly start working on it.</p><p>Lets get started with what should be one of the easier changes to make, variable and function naming conventions. Code is a lot easier to read if the variables and functions tell you what they do/represent. Consider the code in Example 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/402/1*A1C8gyQX_0IqBnIktYmqtw.png" /><figcaption>Example 1, Badly named functions and variables</figcaption></figure><p>See left is an example of bad code. It’s not the worst code. You can still follow the flow, but you can’t be totally sure what the intention of the code is. What does ‘<em>Thing</em>’ and ‘<em>OtherThing</em>’ represent? How does changing either of these classes affect the rest of the code? You would need to add comments for anyone other than the original developer to be sure of what the code is doing. When naming variables, functions and even classes, giving them self-explanatory names makes code a lot easier to read and understand.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*oDQfW_mA5CobirlQy8MrCQ.png" /><figcaption>Example 2, Intuitive naming of functions, variables and classes</figcaption></figure><p>In Example 2 (<em>left</em>) even at first glance, you can get more of an idea what the intention of the code is. You can see that there is a lamp and a light switch, the light switch turns the lamp on and can also close the curtains. You can clearly read how the code is making decisions, and each function is really simple. There are no convoluted exits to the function. Which means you, as the programmer, don’t have to try and work out which exit the code is going to take. All that extra clarity for just over 10 lines of code, and in this particular example, its actually quicker to make Example 2 as you don’t need to make up and remember variable or function names because they are intuitive and flow.</p><p>In the above examples, because it is a really simple program, the functions are already very small, but these are about the length most of your functions should be. Functions should be on average no longer than 20 lines, and never more than 100 lines of code. If you remember that a function should only do one thing, very few ‘one thing’s will take 20 lines of code to accomplish. I can’t think of any functions that are 100 lines of code, or more, that can’t be broken down into several shorter functions. One particular project I have worked on, regularly had functions that were over 200–300 lines of code, some had more! There was duplicated code all over the project. Obsolete code was left over from several updates ago, model attributes were manipulated in the controller, and many other bad habits, that made updating and maintaining a nightmare. When refactored with clean code, it was much easier to manage, as there were no nasty surprises inside functions and everything was where you expected it to be.</p><p>A recent discovery I have made about functions relates to conditional statements, conditional statements should call another function as the outcome, it should not perform actions inside the statement. This actually makes sense, the condition is one “thing” and the outcome is another “thing”. See Example 2, lines 35 to 39. In the <em>if</em> statement, a function is called instead of setting the lamp status (<em>_is_on</em> happens to be private, but calling a function is the preferred method even on public attributes).</p><p>One of the hardest things I find to implement in clean code, is the number of arguments passed to a function. Ideally, you should pass no arguments (niladic), one (monadic) or two arguments (dyadic) are ok, but you should try not to use three arguments (triadic) where possible, and you shouldn’t ever use more than three arguments (polyadic). When refactoring the big project mentioned above, this was quite a challenge, as quite a few functions has four or more arguments being passed.</p><p>And now my least favourite part of clean code. Some programmers think you can’t have clean code without testing. Personally I think you can go overboard with testing. Some tests feel to me like comments, you’re just doing it for the sake of it. Do you really need to test a model which only has two or three attributes? I do think that functional tests are very useful to a developer. It saves so much time when functional tests have been well written. They are definitely worth the time it takes to write and maintain, (and man does it take a long time? Phew!). They help you to make sure that your changes haven’t affected other parts of the system, which in turn keeps the end users happy. (read: quiet life).</p><p>When I was researching clean code there was one thing that made me celebrate, “comments, you shouldn’t need them”. Personally I have always been a fan of “ninja comments”. If you feel the need to add comments, you could probably write the code better. What can you really express in a comment that you can’t express in a high level programming language? There are times where comments can be useful, if a certain function takes a long time to test, or to very occasionally highlight something. Almost exclusively, I only use comments to mark what I need to come back to, and always prefix the comment so its easy to find later. One of the main reasons for writing bad code is, because you are up against a tight deadline. If that’s the case are you really going to go looking for comments that need updating? So do your future self a favour and spend your time writing the code well and ditch the comments.</p><p>In conclusion, writing clean code the first time around makes your life a lot easier and anyone who takes over your code will definitely thank you. It doesn’t take any longer to write clean code, and if you think about it, you wouldn’t write a messy incomprehensible letter, so why write messy code? Keeping code clean helps, especially with big projects, which can soon get out of control, with messy code where bugs can quickly start to appear for no apparent reason. If you have inherited code, you don’t have to refactor it all in one go, even if you only clean up the function you need to update thats a start. At some point, the code is going to get a lot cleaner, and when you are allowed to refactor, half of the work will already be done.</p><p>The main points to take from this post is:</p><p>1, to give useful and relevant names to everything,</p><p>2, keep functions short,</p><p>3, don’t send too many arguments to a function,</p><p>4, write useful tests and</p><p>5, only add comments where absolutely necessary.</p><p>Hope you have enjoyed this post, check back soon for part 2.</p><p>Read more about clean code on our website…</p><p><a href="https://www.toruinteractive.com/web-software-development.html">Web software development | Toru Interactive</a></p><p>Further Reading:</p><p>Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin (2008) (ISBN : 978–0132350884)</p><p><a href="http://www.amazon.co.uk/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882">Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f6c9f04e743" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to integrate an Angular Widget into Yii]]></title>
            <link>https://medium.com/@toru_aine/how-to-integrate-an-angular-widget-into-yii-6355b5372cb3?source=rss-bf29e5d7cd70------2</link>
            <guid isPermaLink="false">https://medium.com/p/6355b5372cb3</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[learn]]></category>
            <category><![CDATA[angularjs]]></category>
            <category><![CDATA[creative-digital-agencies]]></category>
            <category><![CDATA[yii]]></category>
            <dc:creator><![CDATA[Áine Poole]]></dc:creator>
            <pubDate>Wed, 04 Nov 2015 11:29:15 GMT</pubDate>
            <atom:updated>2017-05-04T10:31:33.241Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f-sfTGXriTCul-yJgjct-g.jpeg" /></figure><p>This is a brief guide on how to integrate Angular into an existing Yii 2.0 web app. The instructions will provide you with the basics on how to create a very simple Angular widget which will show the logged in user’s details, add the widget to a Yii view and pass some basic information into the widget. This guide will also briefly touch on how to load more information via RESTful web calls.</p><h4>Things you’ll need</h4><ul><li>Existing Yii 2.0 Advanced Template Web App</li><li>Basic knowledge of Angular, Yii 2.0, Grunt and Bower</li></ul><h4>1. Getting Ready</h4><p>In this how to guide we are going to make a very simple widget which will be passed an id, and will show a user’s details based on the id supplied. There will only be two modules used, the frontend which is where the angular widget will be shown, and an api module which will be used to load the user’s information. We use MAMP Pro to setup the modules to subdomains, the domain we are going use throughout is example.dev for the “frontend” module and api.example.dev for the api module.</p><p>Here is the link for the site before adding code for this guide: <a href="https://bitbucket.org/toruinteractive/blog_yii_angular/get/step_1.zip"><em>https://bitbucket.org/toruinteractive/blog_yii_angular/get/step_1.zip</em></a></p><h4>2. Creating a basic Angular Widget</h4><p>First you need to determine which modules you will require your new Angular widget to be accessible from. In theory it doesn’t really matter which module, but at <a href="https://medium.com/u/fa11cbeacb9b">Toru Interactive</a> we tend to organise widgets into modules depending on how they are accessed, i.e. if a widget will only be available to the “frontend” module we will add a “widgets” folder to the front end and likewise with the backend. If the widget will need to be accessed from multiple modules we will add a widgets folder to the common module and store the widget there instead as it is a ‘common’ widget to multiple modules. You do not need to follow this convention, but we have found on larger projects that this is a very useful way to organise widgets.</p><p>For the purposes of this guide we are going to access the widget from the front end, so go ahead and add a “widgets” folder under the “frontend” module folder, in the widgets folder create a “details” folder.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*__-BqPQNfp3Oy_5zLbK6Gg.png" /><figcaption>Widget Folder Structure</figcaption></figure><p>Because we use Grunt to minify our angular widgets we add another folder called “src” to the details folder, generally we call the folder with the source files “src” and send the minified files to a build folder, these both sit next to each other in the details folder. So go ahead and add the “src” folder, the build will be added automatically by Grunt.</p><p>Next we need to create the angular app that we are going to show on the site, to start with we are going to make a very simple three file app. I’m going to assume you know about factories, directives, controllers and templates, if not, checkout <a href="https://docs.angularjs.org/tutorial"><em>this really great tutorial</em></a> from Angular which works through these key components of an Angular app. The first thing we are going to create is the factory, the factory is going to be very basic to start, and its just a holder at the moment ready for getting the user’s details.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb07468a02b6a77814759e2304e85c2b/href">https://medium.com/media/eb07468a02b6a77814759e2304e85c2b/href</a></iframe><p>Then we are going to add the details directive, which again is going to be very basic, its only going to allow us to pull in the factory and show the template.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62b73d032c6b2a1fa1c67398d31d0c5f/href">https://medium.com/media/62b73d032c6b2a1fa1c67398d31d0c5f/href</a></iframe><p>So we can check that everything is working the template file is going to show, so enter as text into the <em>details.tpl.html</em> file</p><pre>Hello World!</pre><p>As this how to guide isn’t focussing on Grunt I have added links to the <a href="https://bitbucket.org/toruinteractive/blog_yii_angular/src/a63e96ba44c0277770327c67bc1cdbe082894e5e/frontend/widgets/details/gruntfile.js?at=master">gruntfile.js</a> and <a href="https://bitbucket.org/toruinteractive/blog_yii_angular/src/a63e96ba44c0277770327c67bc1cdbe082894e5e/frontend/widgets/details/package.json?at=master">package.json</a>.<strong> </strong>In Terminal or Command Prompt, navigate to the details folder and run</p><pre>npm install</pre><p>followed by</p><pre>grunt</pre><p>to start listening for changes to the angular widget, if you save one of the files you should see a new ‘build’ folder appear in the root.</p><p>Here is the link to the code from this step: <a href="https://bitbucket.org/toruinteractive/blog_yii_angular/get/step_1.zip"><em>https://bitbucket.org/toruinteractive/blog_yii_angular/get/step_1.zip</em></a></p><h4>3. Adding the widget to Yii</h4><p>Now that you have an angular widget you probably want to see it in action right? We need to setup Yii to import the necessary libraries, we use Bower and Composer to manage our library imports, to allow Composer to pull in Bower libraries you just need to add line 6 to the list of required packages in Composer, see below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb460d39004c78c0d94fc89b60520a40/href">https://medium.com/media/bb460d39004c78c0d94fc89b60520a40/href</a></iframe><p>Next we need to tell Bower what libraries we want to import, we can do this in Composer in the “extra” section, add a new key “require-bower” and make it an object, then add the angular libraries as you would in bower, see below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/255d6a384248fb2f66deca0ecebf11a8/href">https://medium.com/media/255d6a384248fb2f66deca0ecebf11a8/href</a></iframe><p>Now if you update composer you should see a <em>bower.json</em> file has been generated and the two angular libraries are now in the vendor/bower folder. <em>n.b. don’t manually edit the bower.json file as next time you run composer it will be overwritten.</em></p><p>Now we need to make a Yii widget to load our Angular App, in the details folder we need to make two new files, the first one will be “<em>Details.php</em>”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83485891cb3be70f478aebee5634a260/href">https://medium.com/media/83485891cb3be70f478aebee5634a260/href</a></iframe><p>The above code registers the asset that will pull in the angular libraries and the angular app minified file. It then adds the HTML that will trigger the angular app and finally it bootstraps the app. You can add the bootstrap code directly to the layout file, but we find that bootstrapping the widget allows widgets to be added on an ‘as and when’ basis rather then including all apps all of the time, this helps with load speed and we feel its just a more efficient way of loading widgets.</p><p>So we still need to load in the angular libraries, lets go ahead and make a “DetailsAsset.php” file in the details folder. In this file we are going to make two asset bundles, the first one DetailsAsset will load the minified angular app and also set the other required assets, one of which being the second asset bundle we are going to create. The second asset bundle imports the two angular libraries we downloaded from Bower, see below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c22f6dbb5aadf4068b0600b2556f6eeb/href">https://medium.com/media/c22f6dbb5aadf4068b0600b2556f6eeb/href</a></iframe><p><em>n.b. while you are developing the widget you should set forceCopy on Line 7 to true, this forces the changes you make to be shown to the user. When you go live you should change it back to false, it makes page loading much quicker when forceCopy is set to false.</em></p><p>So now we have made our Yii widget we can add it to a view, go to the site/index.php file and clear out the jumbotron and body-content divs. Then we need to declare the Details class name and then echo the widget, see Lines 2 and 7–9 below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10a7b880698ed82c37ff8219afdb74ae/href">https://medium.com/media/10a7b880698ed82c37ff8219afdb74ae/href</a></iframe><p>If you now navigate to <a href="http://example.dev/"><em>http://example.dev/</em></a> you should see as the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/823/1*uJWNY7PCV5aSdGNaa5deQg.png" /><figcaption>site/index showing the Details widget</figcaption></figure><p>Here is the link to the code from this step: <a href="https://bitbucket.org/toruinteractive/blog_yii_angular/get/step_3.zip"><em>https://bitbucket.org/toruinteractive/blog_yii_angular/get/step_3.zip</em></a></p><h4>4. RESTful API calls</h4><p>So we have an Angular/Yii widget, but it doesn’t do anything, what would be really useful is if it could pull information from the website right? So we said earlier that we would load a user’s details, we could convert the user model to JSON and pass into Angular as a scope variable, but this seems a bit cumbersome and it wouldn’t take much information before this starts to get very messy. Yii provides us a way to do this using RESTful API calls, there is however a snag with this, to be properly RESTful it should also be stateless (i.e. it shouldn’t know who is logged in!), but thats fine we can pass the API call the user’s id from the UserIdentity that Yii provides.</p><p>First we should setup our data, in the api module in Yii add a new Controller called UserController.php and it inherits from the <em>yii\rest\ActiveController</em> class. This controller is going to return data from the User model, which is automatically made with any new Yii project, so we’re going to set the modelClass to <em>common\models\User</em> and the controller will automatically return data for this model. Next we need to setup the routing in the “<em>api\config\main.php”</em> file, we need to add ‘user’ to the urlManager, rules controller array, see line 11 below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13200330e8f8cba2c1cfd710bd820c5e/href">https://medium.com/media/13200330e8f8cba2c1cfd710bd820c5e/href</a></iframe><p>If you haven’t already, if you create a database, link it to the Yii app, add a user table and add some rows with data, if you want to follow this guide exactly we created the user table with the following command:</p><pre>CREATE TABLE `user` (<br> `id` int(11) unsigned NOT NULL AUTO_INCREMENT,<br> `full_name` varchar(128) DEFAULT NULL,<br> `address` varchar(64) DEFAULT NULL,<br> `county` varchar(64) DEFAULT NULL,<br> `post_code` varchar(10) DEFAULT NULL,<br> PRIMARY KEY (`id`)<br>) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;</pre><p>As Yii provides us with some default actions when using the RESTful ActiveController, if we go to<em> </em><a href="http://api.example.dev/users"><em>http://api.example.dev/users</em></a> we can see all of the users and all of their data, see below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*NrFxg1PI-eINIVphez_QWA.png" /><figcaption><a href="http://api.example.dev/users">http://api.example.dev/users</a></figcaption></figure><p>If we then pick one of the users and add their id to the url you can see only their information, so we have a user with the id of 1, if we go to <a href="http://api.example.dev/users/1"><em>http://api.example.dev/users/1</em></a> we can see all of their information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*ONUtcDTsTf_KT8MLw9VnOg.png" /><figcaption><a href="http://api.example.dev/users/1">http://api.example.dev/users/1</a></figcaption></figure><p>So, we can now view the data from the api subdomain, but if we were to try and load this from <a href="http://example.dev/"><em>http://example.dev/</em></a><em> </em>we would get a cross domain error, this is a security feature built into all of the popular browsers. To get around this you can make the requesting domain as safe, you can do this with the following code in the UserController in the API module.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c3a6fe30628320ef95a243a928605c1/href">https://medium.com/media/2c3a6fe30628320ef95a243a928605c1/href</a></iframe><p>Now we can access our data, we need go back to the angular widget, if we wanted to get user 1 we would need to pass data to the angular app. This is done in the Details.php file as you would if the angular app was being called from a static page, see below where the attribute “user-id” is being set to 1, if your app allows you to login you can set “user-id” to <em>Yii::$app-&gt;user-&gt;id</em> to dynamically get the data for the currently logged in user.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e06e02ee665a1bcb2c22e040acad3a45/href">https://medium.com/media/e06e02ee665a1bcb2c22e040acad3a45/href</a></iframe><p>Next we need to call the api to get the user’s details, if you have followed the Angular tutorial you can probably work this bit out for yourself. At <a href="https://medium.com/u/fa11cbeacb9b">Toru Interactive</a> we setup our factory a little differently as we like to perform any data manipulation in the factory as well as using it to make the API calls. So as you can see below, we have an “api” key on the factory object which returns the resource, here we can setup the call to <a href="http://api.example.dev/users/1"><em>http://api.example.dev/users/1</em></a>, as we are using the userId passed to the Angular app we can swap “<em>users/1”</em> to “<em>users/:id”</em>, this allows us to pass a variable into the query.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c18e84b78558ed9bc9b596b846b1f4b1/href">https://medium.com/media/c18e84b78558ed9bc9b596b846b1f4b1/href</a></iframe><p>Next we can setup the call from the “<em>details.directive.js</em>” file and pass in the “<em>userId”</em> scope variable, then set the user variable on the scope to equal the response from the call, see below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c8c1a81836597b9ffa2a46b925f5a24/href">https://medium.com/media/8c8c1a81836597b9ffa2a46b925f5a24/href</a></iframe><p>Then we just need to display the data to the user, so all we need to do is update the template and we’re done.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/803440a9d5fdd8c989575ab28021f69a/href">https://medium.com/media/803440a9d5fdd8c989575ab28021f69a/href</a></iframe><p>If you go to <a href="http://example.dev/"><em>http://example.dev/</em></a> now you should something similar to the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*M12IyYo_VqvFAWX_AT1SFg.png" /><figcaption><a href="http://example.dev/"><em>http://example.dev/</em></a></figcaption></figure><p>Here is the link to the code from this step: <a href="https://bitbucket.org/toruinteractive/blog_yii_angular/get/step_4.zip"><em>https://bitbucket.org/toruinteractive/blog_yii_angular/get/step_4.zip</em></a></p><h4>5. What Next?</h4><p>This guide has shown you how to make an angular app, turn it into a Yii widget and retrieve data from the database via a RESTful web call. Using the steps above you can go on to make much more powerful angular widgets which can allow you to login to the site, search your data and show the results without the delay waiting for pages to load. The combination between Angular and RESTful when submitting form data is awesome, Because of the way angular binds data its really simple and efficient to update views, you can easily show users that something is happening, that it has finished or that there was an error.</p><p>If there is any topic that you would like me to cover please email or tweet me.</p><p>Happy Coding!</p><h4>6. Resources</h4><ul><li><strong>Angular API. </strong><a href="https://docs.angularjs.org/api"><em>https://docs.angularjs.org/api</em></a></li><li><strong>Angular Tutorial.</strong> <a href="https://docs.angularjs.org/tutorial"><em>https://docs.angularjs.org/tutorial</em></a></li><li><strong>Yii API. </strong><a href="http://www.yiiframework.com/doc-2.0/index.html"><em>http://www.yiiframework.com/doc-2.0/index.html</em></a></li><li><strong>Yii RESTful Web Services. </strong><a href="http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html"><em>http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html</em></a></li><li><strong>Bower.</strong> <a href="http://bower.io/"><em>http://bower.io/</em></a></li><li><strong>Example Repository.</strong> <a href="https://bitbucket.org/toruinteractive/blog_yii_angular/get/b953d96be544.zip"><em>https://bitbucket.org/toruinteractive/blog_yii_angular</em></a></li></ul><p>Read more about Widgets on our website…</p><p><a href="https://www.toruinteractive.com/our-work.html">Bespoke software development company - Web application developers | Toru Interactive</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6355b5372cb3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to integrate Mandrill into Yii 2.0]]></title>
            <link>https://medium.com/@toru_aine/how-to-integrate-mandrill-into-yii-2-0-2f4a685beb46?source=rss-bf29e5d7cd70------2</link>
            <guid isPermaLink="false">https://medium.com/p/2f4a685beb46</guid>
            <category><![CDATA[mandrill]]></category>
            <category><![CDATA[yii]]></category>
            <category><![CDATA[mobile-app-developers]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[web-software-development]]></category>
            <dc:creator><![CDATA[Áine Poole]]></dc:creator>
            <pubDate>Tue, 27 Oct 2015 17:17:02 GMT</pubDate>
            <atom:updated>2017-05-04T10:41:05.314Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xVxtPAR1MAFX4EzGzHdUNg.jpeg" /></figure><p>This is a brief guide to how to integrate Mandrill into an existing Yii 2.0 web app. These instructions have been written for Mac and MAMP has been setup to host the Yii site on localhost using port 80. If you are using Windows please use Command Prompt instead of Terminal and WAMP instead of MAMP.</p><h4>Things you’ll need</h4><ul><li>Existing Yii 2.0 Basic Web App</li><li>Existing Mandrill account</li><li>Basic PHP/HTML skills</li></ul><h4>1. Downloading Mandrill</h4><p>If you didn’t create your Yii project with Composer, install Composer now using <a href="http://symfony.com/doc/current/cookbook/composer.html">this guide</a>.</p><p>Once Composer has been installed you will need to download the Mandrill PHP SDK, download Mandrill with the following command and press enter:</p><pre><em>php composer.phar require mandrill/mandrill 1.0</em></pre><p>If you are using Terminal it will look similar to below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*nSEoBuhq6ZqUl_fX1EAX_A.png" /></figure><p>Once Mandrill has finished downloading Terminal will return to the cursor.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*tdxkwwcz8JHrsKx7IquKOQ.png" /></figure><h4>2. Making the template</h4><ul><li>Log into Mandrill,</li><li>go to Outbound (the envelope on the left hand menu),</li><li>click on Templates on the new menu that appears at the top of the page,</li><li>click the blue ‘create a template’ button near the top of the page.</li><li>Enter the template name, in this case we are going to call it ‘avengers-contact-us’</li><li>finally click ‘Start Coding’, you should then see the following layout</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A3rggjZOT29Z5gTE." /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*SzPZT0PCfn0ZZGego3xCFQ.png" /><figcaption>setting the template up in Mandrill</figcaption></figure><p><strong>Template Info: </strong>This can be left as it is, it is just the name of the template that you have entered and the template slug. You can change the name if you wish but the slug will remain the same. If you want to use a different slug you will need to delete the template and create a new template with the new name.</p><p><strong>Template Defaults: </strong>This is where you can set the details from name and email address, this can be useful if you are emailing a user, in that scenario you can set it to the admin email address for the site.</p><p>Otherwise it’s best to leave it blank as you will probably want to set it to the user’s name and email address. Set the subject to something quick to identify.</p><p>Finally we need to code the email message that will be sent, generally a quick introductory line and then into the data you want to receive. Wrapping text with *| |*<em> </em>informs Mandrill that these are variable names, this allows you to pass dynamic data into the email. In this case as we are sending a contact email, lets just add NAME, EMAIL and QUESTION.</p><h4>3. Adding Code</h4><p>If you didn’t use Composer to create the Yii web app, you will need to add the following code to the web/index.php file. If you are using the advanced Yii template you will need to add this line to all modules you want to be able send emails from.</p><pre>require(__DIR__ . ‘/../vendor/autoload.php’);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/0*wJlxhlzExOXQg31T." /></figure><p>So the index.php file will look something like this, where the new line of code is line 5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/382/1*wfb6b1nZwVcDxFjQxbuShg.png" /></figure><p>To enable easy identification and if necessary a one place update, we add a key to the params file which is called “mandrillKey”, this key holds the API key from Mandrill. If you are using the advanced Yii template it can be useful to set the params-local.php to have the test API key and the params.php file to use live API key, in the case of the advanced template we would tend to put the keys in the common config params files so they are accessible across all modules. Where <em>xxxxxx</em> is your 23 character Mandrill key.</p><p>For the purpose of this guide we are going to trigger the email being sent from a button click, but you can easily take this code and change it to be triggered from a form submission. So on the default root of the project we are going to have a button that triggers the email which will be sent from the SiteController in the Send action. At the top of the SiteController.php file we need to import Mandrill, Yii does this using the ‘use’ command, because we downloaded Mandrill using Composer we have already registered the SDK. To import Mandrill add the following code to the top of the SiteController.php file, after use yii\web\Controller</p><pre><em>use Mandrill;</em></pre><pre><em>use Mandrill_Error;</em></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/0*9ssGoPjDDLk8sPiv." /></figure><p>So the first few lines of SiteController.php will look something like the below image, where the new lines of code are Lines 8 and 9.</p><p>Next we need make the new action on the SiteController, the action is going to be called actionSend. In this function there is going to be a try / catch statement, Mandrill is going be initialised with the key we added to the params earlier and we’re going to set a static to, from and message. At a later point you can change these values to either come from the database or from a form submission.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0e3cad80a08fd60a1304029e4feac2b/href">https://medium.com/media/e0e3cad80a08fd60a1304029e4feac2b/href</a></iframe><p>In the above print screen, line 34 is initialising Mandrill with the key we set in the params, the message array sets who the email is coming from, where the message is being sent and setting the variables we set up in the Mandrill template. Finally on line 52, the template is selected and the message passed through. Now when the action is run the email is sent and outcome is sent to the view so we can display either a success or an error message.</p><h4>4. Sending the message</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*SL7bfF4vn6di2Sx_sXvEMw.png" /></figure><p>Now if you go to <a href="http://localhost/">http://localhost</a> and click on the button you added to go to <a href="http://localhost/site/send">http://localhost/site/send</a>, the email should be sent, but you want to check out the email right? Ok, so go to the SiteController and change line 28 to your email address, and re-send the email. When the email comes through it should look something like the image to the right.</p><h4>5. What Next?</h4><p>This guide has shown you how to implement the very basics of integrating Mandrill into Yii. In addition to just sending variables, you can also attach PDFs, JPGs, PNGs, etc, that have been generated or stored on the server. In addition to attachments you can also add users to CC and BCC, set up webhooks and much more. Check out <a href="https://mandrillapp.com/docs/">Mandrill’s API</a> to get ideas on what you can achieve. Depending on where you are going to be sending emails from in your code, you could create function on an inherited controller to handle setting the template and adding the required variables, or if it’s only going to send on the ContactForm.php you could move the code from the SiteController and add to a function on the ContactForm and then check the email sent as part of the validation of the form.</p><p>Happy Coding!</p><h4>6. Resources</h4><ul><li><strong>Installing Composer. </strong><a href="http://symfony.com/doc/current/cookbook/composer.html"><em>http://symfony.com/doc/current/cookbook/composer.html</em></a></li><li><strong>Installing Yii</strong>. <a href="http://www.yiiframework.com/doc-2.0/guide-start-installation.html"><em>http://www.yiiframework.com/doc-2.0/guide-start-installation.html</em></a></li><li><strong>Yii API</strong>. <a href="http://www.yiiframework.com/doc-2.0/index.html"><em>http://www.yiiframework.com/doc-2.0/index.html</em></a></li><li><strong>Mandrill API</strong> . <a href="https://mandrillapp.com/docs/"><em>https://mandrillapp.com/docs/</em></a></li><li><strong>Mandrill Installation</strong> . <a href="https://mandrillapp.com/api/docs/index.php.html"><em>https://mandrillapp.com/api/docs/index.php.html</em></a></li></ul><p>find more information on our website…</p><p><a href="https://www.toruinteractive.com/index.html">Digital Marketing Agency | Toru Interactive</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2f4a685beb46" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>