Monday, April 30, 2018

How to Avoid Common Travel Video Mistakes

How to Use TensorFlow Mobile in Android Apps

International Artist Feature: New Zealand

How to Draw a Palm Tree

How to Quickly Customize a PowerPoint Template Design

Regular Expressions With Go: Part 2

Regular Expressions With Go: Part 2

Overview

This is part two of a two-part series of tutorials about regular expressions in Go. In part one we learned what regular expressions are, how to express them in Go, and the basics of using the Go regexp library to match text against regular expression patterns. 

In part two, we will focus on using the regexp library to its full extent, including compiling regular expressions, finding one or more matches in the text, replacing regular expressions, grouping submatches, and dealing with new lines.

Using the Regexp Library

The regexp library provides full-fledged support for regular expressions as well as the ability to compile your patterns for more efficient execution when using the same pattern to match against multiple texts. You can also find indices of matches, replace matches, and use groups. Let's dive in.

Compiling Your Regex

There are two methods for compiling regexes: Compile() and MustCompile(). Compile() will return an error if the provided pattern is invalid. MustCompile() will panic. Compilation is recommended if you care about performance and plan to use the same regex multiple times. Let's change our match() helper function to take a compiled regex. Note that there is no need to check for errors because the compiled regex must be valid.

Here is how to compile and use the same compiled regex multiple times:

Finding

The Regexp object has a lot of FindXXX() methods. Some of them return the first match, others return all matches, and yet others return an index or indexes. Interestingly enough, the names of all 16 methods of functions match the following regex: Find(All)?(String)?(Submatch)?(Index)?

If 'All' is present then all matches are returned vs. the leftmost one. If 'String' is present then the target text and the return values are strings vs. byte arrays. If 'Submatch' is present then submatches (groups) are returned vs. just simple matches. If 'Index' is present then indexes within the target text are returned vs. the actual matches.

Let's take one of the more complex functions to task and use the FindAllStringSubmatch() method. It takes a string and a number n. If n is -1, it will return all matching indices. If n is a non-negative integer then it will return the n leftmost matches. The result is a slice of string slices. 

The result of each submatch is the full match followed by the captured group. For example, consider a list of names where some of them have titles such "Mr.", "Mrs.", or "Dr.". Here is a regex that captures the title as a submatch and then the rest of the name after a space: \b(Mr\.|Mrs\.|Dr\.) .*.

As you can see in the output, the full match is captured first and then just the title. For each line, the search resets.

Replacing

Finding matches is great, but often you may need to replace the match with something else. The Regexp object has several ReplaceXXX() methods as usual for dealing with strings vs. byte arrays and literal replacements vs. expansions. In the great book 1984 by George Orwell, the slogans of the party are inscribed on the white pyramid of the ministry of truth: 

  • War is Peace 
  • Freedom is Slavery 
  • Ignorance is Strength 

I found a little essay on The Price of Freedom that uses some of these terms. Let's correct a snippet of it according to the party doublespeak using Go regexes. Note that some of the target words for replacement use different capitalization. The solution is to add the case-insensitive flag (i?) at the beginning of the regex. 

Since the translation is different depending on the case, we need a more sophisticated approach then literal replacement. Luckily (or by design), the Regexp object has a replace method that accepts a function it uses to perform the actual replacement. Let's define our replacer function that returns the translation with the correct case.

Now, we can perform the actual replacement:

The output is somewhat incoherent, which is the hallmark of good propaganda.

Grouping

We saw how to use grouping with submatches earlier. But it is sometimes difficult to handle multiple submatches. Named groups can help a lot here. Here is how to name your submatch groups and populate a dictionary for easy access by name:

Dealing With New Lines

If you remember, I said that the dot special character matches any character. Well, I lied. It doesn't match the newline (\n) character by default. That means that your matches will not cross lines unless you specify it explicitly with the special flag (?s) that you can add to the beginning of your regex. Here is an example with and without the flag.

Another consideration is whether to treat the ^ and $ special characters as the beginning and end of the whole text (the default) or as the beginning and end of each line with the (?m) flag.  

Conclusion

Regular expressions are a powerful tool when working with semi-structured text. You can use them to validate textual input, clean it up, transform it, normalize it, and in general deal with a lot of diversity using concise syntax. 

Go provides a library with an easy-to-use interface that consists of a Regexp object with many methods. Give it a try, but beware of the pitfalls.


20 Best WordPress Video Themes: With Beautiful Gallery and Fullscreen Features

Create a Library Finder App in Angular: HomeComponent and LibraryListComponent

20 Coolest Cinema 4D Templates

10 of the Most Useful Free Mac Apps for Travellers

How to Create a Summer Portrait With Ice Cream in Procreate

15 of the Best Rated Breakbeat and Drum & Bass Music Kits

Friday, April 27, 2018

Translating Stimulus Apps With I18Next

Translating Stimulus Apps With I18Next

In my previous article I covered Stimulus—a modest JavaScript framework created by Basecamp. Today I'll talk about internationalizing a Stimulus application, since the framework does not provide any I18n tools out of the box. Internationalization is an important step, especially when your app is used by people from all around the world, so a basic understanding of how to do it may really come in handy.

Of course, it is up to you to decide which internationalization solution to implement, be it jQuery.I18n, Polyglot, or some other. In this tutorial I would like to show you a popular I18n framework called I18next that has lots of cool features and provides many additional third-party plugins to simplify the development process even further. Even with all these features, I18next is not a complex tool, and you don't need to study lots of documentation to get started.

In this article, you will learn how to enable I18n support in Stimulus applications with the help of the I18next library. Specifically, we'll talk about:

  • I18next configuration
  • translation files and loading them asynchronously
  • performing translations and translating the whole page in one go
  • working with plurals and gender information
  • switching between locales and persisting the chosen locale in the GET parameter
  • setting locale based on the user's preferences

The source code is available in the tutorial GitHub repo.

Bootstrapping a Stimulus App

In order to get started, let's clone the Stimulus Starter project and install all the dependencies using the Yarn package manager:

We're going to build a simple web application that loads information about the registered users. For each user, we'll display his/her login and the number of photos he or she has uploaded so far (it does not really matter what these photos are). 

Also, we are going to present a language switcher at the top of the page. When a language is chosen, the interface should be translated right away without page reloads. Moreover, the URL should be appended with a ?locale GET parameter specifying which locale is currently being utilized. Of course, if the page is loaded with this parameter already provided, the proper language should be set automatically.

Okay, let's proceed to rendering our users. Add the following line of code to the public/index.html file:

Here, we are using the users controller and providing a URL from which to load our users. In a real-world application, we would probably have a server-side script that fetches users from the database and responds with JSON. For this tutorial, however, let's simply place all the necessary data into the public/api/users/index.json file:

Now create a new src/controllers/users_controller.js file:

As soon as the controller is connected to the DOM, we are asynchronously loading our users with the help of the loadUsers() method:

This method sends a fetch request to the given URL, grabs the response, and finally renders the users:

renderUsers(), in turn, parses JSON, constructs a new string with all the content, and lastly displays this content on the page (this.element is going to return the actual DOM node that the controller is connected to, which is div in our case).

I18next

Now we are going to proceed to integrating I18next into our app. Add two libraries to our project: I18next itself and a plugin to enable asynchronous loading of translation files from the back end:

We are going to store all I18next-related stuff in a separate src/i18n/config.js file, so create it now:

Let's go from top to bottom to understand what's going on here:

  • use(I18nXHR) enables the i18next-xhr-backend plugin.
  • fallbackLng tells it to use English as a fallback language.
  • whitelist allows only English and Russian languages to be set. Of course, you may choose any other languages.
  • preload instructs translation files to be preloaded from the server, rather than loading them when the corresponding language is selected.
  • ns means "namespace" and accepts either a string or an array. In this example we have only one namespace, but for larger applications you may introduce other namespaces, like admincart, profile, etc. For each namespace, a separate translation file should be created.
  • defaultNS sets users to be the default namespace.
  • fallbackNS disables namespace fallback.
  • debug allows debugging information to be displayed in the browser's console. Specifically, it says which translation files are loaded, which language is selected, etc. You will probably want to disable this setting before deploying the application to production.
  • backend provides configuration for the I18nXHR plugin and specifies where to load translations from. Note that the path should contain the locale's title, whereas the file should be named after the namespace and have the .json extension
  • function(err, t) is the callback to run when I18next is ready (or when an error was raised).

Next, let's craft translation files. Translations for the Russian language should be placed into the public/i18n/ru/users.json file:

login here is the translation key, whereas Логин is the value to display.

English translations, in turn, should go to the public/i18n/en/users.json file:

To make sure that I18next works, you may add the following line of code to the callback inside the i18n/config.js file:

Here, we are using a method called t that means "translate". This method accepts a translation key and returns the corresponding value.

However, we may have many parts of the UI that need to be translated, and doing so by utilizing the t method would be quite tedious. Instead, I suggest that you use another plugin called loc-i18next that allows you to translate multiple elements at once.

Translating in One Go

Install the loc-i18next plugin:

Import it at the top of the src/i18n/config.js file:

Now provide the configuration for the plugin itself:

There are a couple of things to note here:

  • locI18next.init(i18n) creates a new instance of the plugin based on the previously defined instance of I18next.
  • selectorAttr specifies which attribute to use to detect elements that require localization. Basically, loc-i18next is going to search for such elements and use the value of the data-i18n attribute as the translation key.
  • optionsAttr specifies which attribute contains additional translation options.
  • useOptionsAttr instructs the plugin to use the additional options.

Our users are being loaded asynchronously, so we have to wait until this operation is done and only perform localization after that. For now, let's simply set a timer that should wait for two seconds before calling the localize() method—that's a temporary hack, of course.

Code the localize() method itself:

As you see, we only need to pass a selector to the loc-i18next plugin. All elements inside (that have the data-i18n attribute set) will be localized automatically.

Now tweak the renderUsers method. For now, let's only translate the "Login" word:

Nice! Reload the page, wait for two seconds, and make sure that the "Login" word appears for each user.

Plurals and Gender

We have localized part of the interface, which is really cool. Still, each user has two more fields: the number of uploaded photos and gender. Since we can't predict how many photos each user is going to have, the "photo" word should be pluralized properly based on the given count. In order to do this, we'll require a data-i18n-options attribute configured previously. To provide the count, data-i18n-options should be assigned with the following object: { "count": YOUR_COUNT }.

Gender information should be taken into consideration as well. The word "uploaded" in English can be applied to both male and female, but in Russian it becomes either "загрузил" or "загрузила", so we need data-i18n-options again, which has { "context": "GENDER" } as a value. Note, by the way, that you can employ this context to achieve other tasks, not only to provide gender information.

Now update the English translations:

Nothing complex here. Since for English we don't care about the gender information (which is the context), the translation key should be simply uploaded. To provide properly pluralized translations, we are using the photos and photos_plural keys. The part is interpolation and will be replaced with the actual number.

As for the Russian language, things are more complex:

First of all, note that we have both uploaded_male and uploaded_female keys for two possible contexts. Next, pluralization rules are also more complex in Russian than in English, so we have to provide not two, but three possible phrases. I18next supports many languages out of the box, and this small tool can help you to understand which pluralization keys should be specified for a given language.

Switching Locale

We are done with translating our application, but users should be able to switch between locales. Therefore, add a new "language switcher" component to the public/index.html file:

Craft the corresponding controller inside the src/controllers/languages_controller.js file:

Here we are using the initialize() callback to display a list of supported languages. Each li has a data-action attribute which specifies what method (switchLanguage, in this case) should be triggered when the element is clicked on.

Now add the switchLanguage() method:

It simply takes the target of the event and grabs the value of the data-lang attribute.

I would also like to add a getter and setter for the currentLang attribute:

The getter is very simple—we fetch the value of the currently used language and return it.

The setter is more complex. First of all, we use the changeLanguage method if the currently set language is not equal to the selected one. Also, we are storing the newly selected locale under the data-current-lang attribute (which is accessed in the getter), localizing the body of the HTML page using the loc-i18next plugin, and lastly highlighting the currently used locale.

Let's code the highlightCurrentLang():

Here we are iterating over an array of locale switchers and comparing the values of their data-lang attributes to the value of the currently used locale. If the values match, the switcher is assigned with a current CSS class, otherwise this class is removed.

To make the this.switcherTargets construct work, we need to define Stimulus targets in the following way:

Also, add data-target attributes with values of switcher for the lis:

Another important thing to consider is that translation files may take some time to load, and we must wait for this operation to complete before allowing the locale to be switched. Therefore, let's take advantage of the loaded callback:

Lastly, don't forget to remove setTimeout from the loadUsers() method:

Persisting Locale in the URL

After the locale is switched, I would like to add a ?lang GET parameter to the URL containing the code of the chosen language. Appending a GET param without reloading the page can be easily done with the help of the History API:

Detecting Locale

The last thing we are going to implement today is the ability to set the locale based on the user's preferences. A plugin called LanguageDetector can help us to solve this task. Add a new Yarn package:

Import LanguageDetector inside the i18n/config.js file:

Now tweak the configuration:

The order option lists all the techniques (sorted by their importance) that the plugin should try in order to "guess" the preferred locale:

  • querystring means checking a GET param containing the locale's code.
  • lookupQuerystring sets the name of the GET param to use, which is lang in our case.
  • navigator means getting locale data from the user's request.
  • htmlTag involves fetching the preferred locale from the lang attribute of the html tag.

Conclusion

In this article we have taken a look at I18next—a popular solution to translate JavaScript applications with ease. You have learned how to integrate I18next with the Stimulus framework, configure it, and load translation files in an asynchronous manner. Also, you've seen how to switch between locales and set the default language based on the user's preferences.

I18next has some additional configuration options and many plugins, so be sure to browse its official documentation to learn more. Also note that Stimulus does not force you to use a specific localization solution, so you may also try using something like jQuery.I18n or Polyglot

That's all for today! Thank you for reading along, and until the next time.


20 Cool Photoshop Photo Effects to Add Style & Wow

7+ Best Antivirus Software for Macs in 2018 (Free + Paid)

Regular Expressions With Go: Part 1

Regular Expressions With Go: Part 1

Overview

Regular expressions (AKA regex) are a formal language that defines a sequence of characters with some pattern. In the real world they can be used to solve a lot of problems with semi-structured text. You can extract the important bits and pieces from text with a lot of decorations or unrelated content. Go has a strong regex package in its standard library that lets you slice and dice text with regexes. 

In this two-part series, you'll learn what regular expressions are and how to use regular expressions effectively in Go to accomplish many common tasks. If you're not familiar with regular expressions at all, there are lots of great tutorials. Here is a good one.

Understanding Regular Expressions

Let's start with a quick example. You have some text, and you want to check if it contains an email address. An email address is specified rigorously in RFC 822. In short, it has a local part followed by an @ symbol followed by a domain. The mail address will be separated from the rest of the text by space. 

To figure out if it contains an email address, the following regex will do: ^\w+@\w+\.\w+$. Note that this regex is a little permissive and will allow some invalid email addresses through. But it's good enough to demonstrate the concept. Let's try it on a couple of potential email addresses before explaining how it works:

Our regular expression works on this little sample. The first two addresses were rejected because the domain didn't have a dot or didn't have any characters after the dot. The third email was formatted correctly. The last candidate had two @ symbols.

Let's break this regex down: ^\w+@\w+\.\w+$

Character/Symbol Meaning
^ Beginning of the target text
\w Any word characters [0-9A-Za-z_]
+ At least one of the previous characters
@ Literally the @ character 
\. The literal dot character. Must be escaped with \
$ End of target text

Altogether, this regex will match pieces of text that start with one or more word characters, followed by the "@" character, followed again by one or more word characters, followed by a dot and followed by yet again one or more word characters.  

Dealing With Special Characters

The following characters have special meanings in regular expressions: .+*?()|[]{}^$\. We have already saw many of them in the email example. If we want to match them literally, we need to escape them with a backslash. Let's introduce a little helper function called match() that will save us a lot of typing. It takes a pattern and some text, uses the regexp.Match() method to match the pattern to the text (after converting the text to a byte array), and prints the results:

Here's an example of matching a regular character like z vs. matching a special character like ?:

The regex pattern \? contains a backslash that must be escaped with another backslash when represented as a regular Go string. The reason is that backslash is also used to escape special characters in Go strings like newline (\n). If you want to match the backslash character itself, you'll need four slashes! 

The solution is to use Go raw strings with the backtick (`) instead of double quotes. Of course, if you want to match the newline character, you must go back to regular strings and deal with multiple backslash escapes.

Placeholders and Repetitions

In most cases, you don't try to literally match a sequence of specific characters like "abc", but a sequence of unknown length with maybe some known characters injected somewhere. Regexes support this use case with the dot  . special character that stands for any character whatsoever. The * special character repeats the previous character (or group) zero or more times. If you combine them, as in .*, then you match anything because it simply means zero or more characters. The + is very similar to *, but it matches one or more of the previous characters or groups. So .+ will match any non-empty text.

Using Boundaries

There are three types of boundaries: the start of the text denoted by ^, the end of the text denoted by $, and the word boundary denoted by \b. For example, consider this text from the classic movie The Princess Bride: "My name is Inigo Montoya. You killed my father. Prepare to die." If you match just "father" you get a match, but if you're looking for "father" at the end of the text, you need to add the $ character, and then there will be no match. On the other hand, matching "Hello" at the beginning works well.

Word boundaries look at each word. You can start and/or end a pattern with the \b. Note that punctuation marks like commas are considered a boundary and not part of the word. Here are a few examples:

Using Classes

It's often useful to treat all groups of characters together like all digits, whitespace characters, or all alphanumeric characters. Golang supports the POSIX classes, which are:

Character Class Meaning
[:alnum:]
alphanumeric (≡ [0-9A-Za-z])
[:alpha:]
alphabetic (≡ [A-Za-z])
[:ascii:] 
ASCII (≡ [\x00-\x7F])
[:blank:] 
blank (≡ [\t ])
[:cntrl:]
control (≡ [\x00-\x1F\x7F])
[:digit:]
digits (≡ [0-9])
[:graph:]
graphical (≡ [!-~] == [A-Za-z0-9!"#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~])
[:lower:] 
lower case (≡ [a-z])
[:print:] 
printable (≡ [ -~] == [ [:graph:]])
[:punct:]
punctuation (≡ [!-/:-@[-`{-~])
[:space:]
whitespace (≡ [\t\n\v\f\r ])
[:upper:]
upper case (≡ [A-Z])
[:word:]
word characters (≡ [0-9A-Za-z_])
[:xdigit:]
hex digit (≡ [0-9A-Fa-f])

In the following example, I'll use the [:digit:] class to look for numbers in the text. Also, I show here how to search for an exact number of characters by adding the requested number in curly braces.

You can define your own classes too by putting characters in square brackets. For example, if you want to check if some text is a valid DNA sequence that contains only the characters ACGT then use the ^[ACGT]*$ regex:

Using Alternatives

In some cases, there are multiple viable alternatives. Matching HTTP URLs may be characterized by a protocol schema, which is either http:// or https://. The pipe character | lets you choose between alternatives. Here is a regex that will sort them out: (http)|(https)://\w+\.\w{2,}. It translates to a string that starts with http:// or https:// followed by at least one word character followed by a dot followed by at least two word characters.

Conclusion

In this part of the tutorial, we covered a lot of ground and learned a lot about regular expressions, with hands-on examples using the Golang regexp library. We focused on pure matching and how to express our intentions using regular expressions. 

In part two, we will focus on using regular expressions to work with text, including fuzzy finding, replacements, grouping, and dealing with new lines.


Get Wear OS and Android Talking: Exchanging Information via the Wearable Data Layer

Get Started With the New Bootstrap 4 Grid System in Our New Course

How to Create an Alice in Wonderland Themed Stencil in Adobe Illustrator

Thursday, April 26, 2018

Create a Library Finder App in Angular: Library Service and Routing

How to Find and Fix Poor Page Load Times With Raygun

How to Create a Furry, White Rabbit Inspired Text Effect in Adobe Photoshop

How to Insert Word Documents Into PowerPoint in 60 Seconds

Personal Branding Guide for Designers 2018

Create 2D Tree Shapes With Code

Create an Alice in Wonderland Themed iPhone Wallpaper in Pixelmator

Create Beautiful Page Layouts and a Striking Cover for a Children’s Fiction Book

Monday, April 23, 2018

What Is WordPress Gutenberg?

How to Create a Hookah Smoke Text Effect in Adobe Photoshop

Quick Tip: Beware When Using the Slim Version of jQuery

How to Use Google Sheets to Make Your First Spreadsheet

Notifications in Laravel

Notifications in Laravel

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications over the mail channel.

Basics of Notifications

During application development, you often need to notify users about different state changes. It could be either sending email notifications when the order status is changed or sending an SMS about their login activity for security purposes. In particular, we're talking about messages that are short and just provide insight into the state changes.

Laravel already provides a built-in feature that helps us achieve something similar—notifications. In fact, it makes sending notification messages to users a breeze and a fun experience!

The beauty of that approach is that it allows you to choose from different channels notifications will be sent on. Let's quickly go through the different notification channels supported by Laravel.

  • Mail: The notifications will be sent in the form of email to users.
  • SMS: As the name suggests, users will receive SMS notifications on their phone.
  • Slack: In this case, the notifications will be sent on Slack channels.
  • Database: This option allows you to store notifications in a database should you wish to build a custom UI to display it.

Among different notification channels, we'll use the mail channel in our example use-case that we're going to develop over the course of this tutorial.

In fact, it'll be a pretty simple use-case that allows users of our application to send messages to each user. When users receive a new message in their inbox, we'll notify them about this event by sending an email to them. Of course, we'll do that by using the notification feature of Laravel!

Create a Custom Notification Class

As we discussed earlier, we are going to set up an application that allows users of our application to send messages to each other. On the other hand, we'll notify users when they receive a new message from other users via email.

In this section, we'll create necessary files that are required in order to implement the use-case that we're looking for.

To start with, let's create the Message model that holds messages sent by users to each other.

We also need to add a few fields like to, from and message to the messages table. So let's change the migration file before running the migrate command.

Now, let's run the migrate command that creates the messages table in the database.

That should create the messages table in the database.

Also, make sure that you have enabled the default Laravel authentication system in the first place so that features like registration and login work out of the box. If you're not sure how to do that, the Laravel documentation provides a quick insight into that.

Since each notification in Laravel is represented by a separate class, we need to create a custom notification class that will be used to notify users. Let's use the following artisan command to create a custom notification class—NewMessage.

That should create the app/Notifications/NewMessage.php class, so let's replace the contents of that file with the following contents.

As we're going to use the mail channel to send notifications to users, the via method is configured accordingly. So this is the method that allows you to configure the channel type of a notification.

Next, there's the toMail method that allows you to configure various email parameters. In fact, the toMail method should return the instance of \Illuminate\Notifications\Messages\MailMessage, and that class provides useful methods that allow you to configure email parameters.

Among various methods, the line method allows you to add a single line in a message. On the other hand, there's the action method that allows you to add a call-to-action button in a message.

In this way, you could format a message that will be sent to users. So that's how you're supposed to configure the notification class while you're using the mail channel to send notifications.

At the end, you need to make sure that you implement the necessary methods according to the channel type configured in the via method. For example, if you're using the database channel that stores notifications in a database, you don't need to configure the toMail method; instead, you should implement the toArray method, which formats the data that needs to be stored in a database.

How to Send Notifications

In the previous section, we created a notification class that's ready to send notifications. In this section, we'll create files that demonstrate how you could actually send notifications using the NewMessage notification class.

Let's create a controller file at app/Http/Controllers/NotificationController.php with the following contents.

Of course, you need to add an associated route in the routes/web.php file.

There are two ways Laravel allows you to send notifications: by using either the notifiable entity or the Notification facade.

If the entity model class utilizes the Illuminate\Notifications\Notifiable trait, then you could call the notify method on that model. The App\User class implements the Notifiable trait and thus it becomes the notifiable entity. On the other hand, you could also use the Illuminate\Support\Facades\Notification Facade to send notifications to users.

Let's go through the index method of the controller.

In our case, we're going to notify users when they receive a new message. So we've tried to mimic that behavior in the index method in the first place.

Next, we've notified the recipient user about a new message using the notify method on the $toUser object, as it's the notifiable entity.

You may have noticed that we also pass the $fromUser object in the first argument of the __construct method, since we want to include the from username in a message.

On the other hand, if you want to mimic it using the Notification facade, it's pretty easy to do so using the following snippet.

As you can see, we've used the send method of the Notification facade to send a notification to a user.

Go ahead and open the URL http://your-laravel-site-domain/notify/index in your browser. If you're not logged in yet, you'll be redirected to the login screen. Once you're logged in, you should receive a notification email at the email address that's attached with the user 1.

You may be wondering how the notification system detects the to address when we haven't configured it anywhere yet. In that case, the notification system tries to find the email property in the notifiable object. And the App\User object class already has that property as we're using the default Laravel authentication system.

However, if you would like to override this behavior and you want to use a different property other than email, you just need to define the following method in your notification class.

Now, the notification system should look for the email_address property instead of the email property to fetch the to address.

And that's how to use the notification system in Laravel. That brings us to the end of this article as well!

Conclusion

What we've gone through today is one of the useful, yet least discussed, features in Laravel—notifications. It allows you to send notifications to users over different channels.

After a quick introduction, we implemented a real-world example that demonstrated how to send notifications over the mail channel. In fact, it's really handy in the case of sending short messages about state changes in your application.

For those of you who are either just getting started with Laravel or looking to expand your knowledge, site, or application with extensions, we have a variety of things you can study in Envato Market.

Should you have any queries or suggestions, don't hesitate to post them using the feed below!


Create Two Colorful Children's Book Illustrations in Illustrator

How to Draw Lips and a Mouth

Friday, April 20, 2018

How to Use Medium Telephoto Photography Lenses

15 Fresh Spring Resources for Photographers and Designers

Travel Video: Choose the Right Accessories for Your Trip

5 Hot Video Artists on Envato Market This Month (April 2018)

How to Animate a Twirling Girl in Toon Boom

20 Cool Photoshop Text Effects, Actions & Styles for 2018

How to Rotate Text & Shapes in Keynote in 60 Seconds

Introduction to the Stimulus Framework

Introduction to the Stimulus Framework

There are lots of JavaScript frameworks out there. Sometimes I even start to think that I'm the only one who has not yet created a framework. Some solutions, like Angular, are big and complex, whereas some, like Backbone (which is more a library than a framework), are quite simple and only provide a handful of tools to speed up the development process.

In today's article I would like to present you a brand new framework called Stimulus. It was created by a Basecamp team led by David Heinemeier Hansson, a popular developer who was the father of Ruby on Rails.

Stimulus is a small framework that was never intended to grow into something big. It has its very own philosophy and attitude towards front-end development, which some programmers might like or dislike. Stimulus is young, but version 1 has already been released so it should be safe to use in production. I've played with this framework quite a bit and really liked its simplicity and elegance. Hopefully, you will enjoy it too!

In this post we'll discuss the basics of Stimulus while creating a single-page application with asynchronous data loading, events, state persistence, and other common things.

The source code can be found on GitHub.

Introduction to Stimulus

Stimulus was created by developers at Basecamp. Instead of creating single-page JavaScript applications, they decided to choose a majestic monolith powered by Turbolinks and some JavaScript. This JavaScript code evolved into a small and modest framework which does not require you to spend hours and hours learning all its concepts and caveats.

Stimulus is mostly meant to attach itself to existing DOM elements and work with them in some way. It is possible, however, to dynamically render the contents as well. All in all, this framework is quite different from other popular solutions as, for example, it persists state in HTML, not in JavaScript objects. Some developers may find it inconvenient, but do give Stimulus a chance, as it really may surprise you.

The framework has only three main concepts that you should remember, which are:

  • Controllers: JS classes with some methods and callbacks that attach themselves to the DOM. The attachment happens when a data-controller "magic" attribute appears on the page. The documentation explains that this attribute is a bridge between HTML and JavaScript, just like classes serve as bridges between HTML and CSS. One controller can be attached to multiple elements, and one element may be powered up by multiple controllers.
  • Actions: methods to be called on specific events. They are defined in special data-action attributes.
  • Targets: important elements that can be easily accessed and manipulated. They are specified with the help of data-target attributes.

As you can see, the attributes listed above allow you to separate content from behaviour logic in a very simple and natural way. Later in this article, we will see all these concepts in action and notice how easy it is to read an HTML document and understand what's going on.

Bootstrapping a Stimulus Application

Stimulus can be easily installed as an NPM package or loaded directly via the script tag as explained in the docs. Also note that by default this framework integrates with the Webpack asset manager, which supports goodies like controller autoloading. You are free to use any other build system, but in this case some more work will be needed.

The quickest way to get started with Stimulus is by utilizing this starter project that has Express web server and Babel already hooked up. It also depends on Yarn, so be sure to install it. To clone the project and install all its dependencies, run:

If you'd prefer not to install anything locally, you may remix this project on Glitch and do all the coding right in your browser.

Great—we are all set and can proceed to the next section!

Some Markup

Suppose we are creating a small single-page application that presents a list of employees and loads information like their name, photo, position, salary, birthdate, etc.

Let's start with the list of employees. All the markup that we are going to write should be placed inside the public/index.html file, which already has some very minimal HTML. For now, we will hard-code all our employees in the following way:

Nice! Now let's add a dash of Stimulus magic.

Creating a Controller

As the official documentation explains, the main purpose of Stimulus is to connect JavaScript objects (called controllers) to the DOM elements. The controllers will then bring the page to life. As a convention, controllers' names should end with a _controller postfix (which should be very familiar to Rails developers).

There is a directory for controllers already available called src/controllers. Inside, you will find a  hello_controller.js file that defines an empty class:

Let's rename this file to employees_controller.js. We don't need to specifically require it because controllers are loaded automatically thanks to the following lines of code in the src/index.js file:

The next step is to connect our controller to the DOM. In order to do this, set a data-controller attribute and assign it an identifier (which is employees in our case):

That's it! The controller is now attached to the DOM.

Lifecycle Callbacks

One important thing to know about controllers is that they have three lifecycle callbacks that get fired on specific conditions:

  • initialize: this callback happens only once, when the controller is instantiated.
  • connect: fires whenever we connect the controller to the DOM element. Since one controller may be connected to multiple elements on the page, this callback may run multiple times.
  • disconnect: as you've probably guessed, this callback runs whenever the controller disconnects from the DOM element.

Nothing complex, right? Let's take advantage of the initialize() and connect() callbacks to make sure our controller actually works:

Next, start the server by running:

Navigate to http://localhost:9000. Open your browser's console and make sure both messages are displayed. It means that everything is working as expected!

Adding Events

The next core Stimulus concept is events. Events are used to respond to various user actions on the page: clicking, hovering, focusing, etc. Stimulus does not try to reinvent a bicycle, and its event system is based on generic JS events.

For instance, let's bind a click event to our employees. Whenever this event happens, I would like to call the as yet non-existent choose() method of the employees_controller:

Probably, you can understand what's going on here by yourself.

  • data-action is the special attribute that binds an event to the element and explains what action should be called.
  • click, of course, is the event's name.
  • employees is the identifier of our controller.
  • choose is the name of the method that we'd like to call.

Since click is the most common event, it can be safely omitted:

In this case, click will be used implicitly.

Next, let's code the choose() method. I don't want the default action to happen (which is, obviously, opening a new page specified in the href attribute), so let's prevent it:

e is the special event object that contains full information about the triggered event. Note, by the way, that this returns the controller itself, not an individual link! In order to gain access to the element that acts as the event's target, use e.target.

Reload the page, click on a list item, and observe the result!

Working With the State

Now that we have bound a click event handler to the employees, I'd like to store the currently chosen person. Why? Having stored this info, we can prevent the same employee from being selected the second time. This will later allow us to avoid loading the same information multiple times as well.

Stimulus instructs us to persist state in the Data API, which seems quite reasonable. First of all, let's provide some arbitrary ids for each employee using the data-id attribute:

Next, we need to fetch the id and persist it. Using the Data API is very common with Stimulus, so a special this.data object is provided for each controller. With its help, we can run the following methods:

  • this.data.get('name'): get the value by its attribute.
  • this.data.set('name', value): set the value under some attribute.
  • this.data.has('name'): check if the attribute exists (returns a boolean value).

Unfortunately, these shortcuts are not available for the targets of the click events, so we must stick with getAttribute() in their case:

But we can do even better by creating a getter and a setter for the currentEmployee:

Notice how we are using the this.currentEmployee getter and making sure that the provided id is not the same as the already stored one.

Now you may rewrite the choose() method in the following way:

Reload the page to make sure that everything still works. You won't notice any visual changes yet, but with the help of the Inspector tool you'll notice that the ul has the data-employees-current-employee attribute with a value that changes as you click on the links. The employees part in the attribute's name is the controller's identifier and is being added automatically.

Now let's move on and highlight the currently chosen employee.

Using Targets

When an employee is selected, I would like to assign the corresponding element with a .chosen class. Of course, we might have solved this task by using some JS selector functions, but Stimulus provides a neater solution.

Meet targets, which allow you to mark one or more important elements on the page. These elements can then be easily accessed and manipulated as needed. In order to create a target, add a data-target attribute with the value of {controller}.{target_name} (which is called a target descriptor):

Now let Stimulus know about these new targets by defining a new static value:

How do we access the targets now? It's as simple as saying this.employeeTarget (to get the first element) or this.employeeTargets (to get all the elements):

Great! How can these targets help us now? Well, we can use them to add and remove CSS classes with ease based on some criteria:

The idea is simple: we iterate over an array of targets and for each target compare its data-id to the one stored under this.currentEmployee. If it matches, the element is assigned the .chosen class. Otherwise, this class is removed. You may also extract the if (this.currentEmployee !== id) { condition from the setter and use it in the chosen() method instead:

Looking nice! Lastly, we'll provide some very simple styling for the .chosen class inside the public/main.css:

Reload the page once again, click on a person, and make sure that person is being highlighted properly.

Loading Data Asynchronously

Our next task is to load information about the chosen employee. In a real-world application, you would have to set up a hosting provider, a back-end powered by something like Django or Rails, and an API endpoint that responds with JSON containing all the necessary data. But we are going to make things a bit simpler and concentrate on the client side only. Create an employees directory under the public folder. Next, add four files containing data for individual employees:

1.json

2.json

3.json

4.json

All photos were taken from the free stock photography by Shopify called Burst.

Our data is ready and waiting to be loaded! In order to do this, we'll code a separate loadInfoFor() method:

This method accepts an employee's id and sends an asynchronous fetch request to the given URI. There are also two promises: one to fetch the body and another one to display the loaded info (we'll add the corresponding method in a moment).

Utilize this new method inside choose():

Before coding the displayInfo() method, we need an element to actually render the data to. Why don't we take advantage of targets once again?

Define the target:

And now utilize it to display all the info:

Of course, you are free to employ a templating engine like Handlebars, but for this simple case that would probably be overkill.

Now reload the page and choose one of the employees. His bio and image should be loaded nearly instantly, which means our app is working properly!

Dynamic List of Employees

Using the approach described above, we can go even further and load the list of employees on the fly rather than hard-coding it.

Prepare the data inside the public/employees.json file:

Now tweak the public/index.html file by removing the hard-coded list and adding a data-employees-url attribute (note that we must provide the controller's name, otherwise the Data API won't work):

As soon as controller is attached to the DOM, it should send a fetch request to build a list of employees. It means that the connect() callback is the perfect place to do this:

I propose we create a more generic loadFrom() method that accepts a URL to load data from and a callback to actually render this data:

Tweak the choose() method to take advantage of the loadFrom():

displayInfo() can be simplified as well, since JSON is now being parsed right inside the loadFrom():

Remove loadInfoFor() and code the displayEmployees() method:

That's it! We are now dynamically rendering our list of employees based on the data returned by the server.

Conclusion

In this article we have covered a modest JavaScript framework called Stimulus. We have seen how to create a new application, add a controller with a bunch of callbacks and actions, and introduce events and actions. Also, we've done some asynchronous data loading with the help of fetch requests.

All in all, that's it for the basics of Stimulus—it really does not expect you to have some arcane knowledge in order to craft web applications. Of course, the framework will probably have some new features in future, but the developers are not planning to turn it into a huge monster with hundreds of tools. 

If you'd like to find more examples of using Stimulus, you may also check out this tiny handbook. And if you’re looking for additional JavaScript resources to study or to use in your work, check out what we have available in the Envato Market

Did you like Stimulus? Would you be interested in trying to create a real-world application powered by this framework? Share your thoughts in the comments!

As always, I thank you for staying with me and until the next time.


Single-Page React Applications With the React-Router and React-Transition-Group Modules

New Course: Four Bootstrap 4 Projects

Art for All: Celebrate Diversity in Design—Volume 8

Thursday, April 19, 2018

Auto-Update Your WordPress Salts With WP-Salts-Update-CLI

Best Practices in UX Design for a Global Audience

How to Set Up a Full-Text Search Using Scout in Laravel

How to Set Up a Full-Text Search Using Scout in Laravel

Full-text search is crucial for allowing users to navigate content-rich websites. In this post, I'll show you how to implement full-text search for a Laravel app. In fact, we'll use the Laravel Scout library, which makes implementation of full-text search easy and fun.

What exactly is the Laravel Scout? The official documentation sums it up like this:

Laravel Scout provides a simple, driver-based solution for adding full-text search to your Eloquent models. Using model observers, Scout will automatically keep your search indexes in sync with your Eloquent records.

Basically, Laravel Scout is a library that manages manipulation of the index whenever there's a change in the model data. The place where the data will be indexed depends on the driver that you've configured with the Scout library.

As of now, the Scout library supports Algolia, a cloud-based search engine API, and that's what we'll use in this article to demonstrate the full-text search implementation.

We'll start by installing the Scout and Algolia server libraries, and as we move on we'll go through a real-world example to demonstrate how you could index and search your data.

Server Configurations

In this section, we're going to install the dependencies that are required in order to make the Scout library work with Laravel. After installation, we'll need to go through quite a bit of configuration so that Laravel can detect the Scout library.

Let's go ahead and install the Scout library using Composer.

That's pretty much it as far as the Scout library installation is concerned. Now that we've installed the Scout library, let's make sure that Laravel knows about it.

Working with Laravel, you're probably aware of the concept of a service provider, which allows you to configure services in your application. Thus, whenever you want to enable a new service in your Laravel application, you just need to add an associated service provider entry in the config/app.php.

If you're not familiar with Laravel service providers yet, I would strongly recommend that you do yourself a favor and go through this introductory article that explains the basics of service providers in Laravel.

  • Laravel
    How to Register & Use Laravel Service Providers
    Sajal Soni

In our case, we just need to add the ScoutServiceProvider provider to the list of service providers in config/app.php, as shown in the following snippet.

Now, Laravel is aware of the ScoutServiceProvider service provider. The Scout library comes with a configuration file that allows us to set API credentials.

Let's go ahead and publish the assets provided by the Scout library using the following command.

As you can see, it has copied the vendor/laravel/scout/config/scout.php file to config/scout.php.

Next, go ahead and create an account with Algolia as we'll need API credentials in the first place. Once you have the API information, let's go ahead and configure the necessary settings in the config/scout.php file, as shown in the following snippet.

Note that we've set the value of SCOUT_DRIVER to algolia driver. Thus, it's required that you configure the necessary settings for the Algolia driver at the end of the file. Basically, you just need to set the id and secret that you've got from the Algolia account.

As you can see, we're fetching values from environment variables. So let's make sure that we set the following variables in the .env file properly.

Finally, we need to install the Algolia PHP SDK, which will be used to interact with the Algolia using APIs. Let's install it using the composer as shown in the following snippet.

And with that, we've installed all the dependencies that are necessary in order to post and index data to the Algolia service.

Make Models Indexable and Searchable

In the previous section, we did all the hard work to set up the Scout and Algolia libraries so that we could index and search data using the Algolia search service.

In this section, we'll go through an example that demonstrates how you could index the existing data and retrieve search results from Algolia. I assume that you have a default Post model in your application that we'll use in our example.

The first thing that we'll need to do is to add the Laravel\Scout\Searchable trait to the Post model. That makes the Post model searchable; Laravel synchronizes post records with the Algolia index every time the post record is added, updated, or deleted.

With that, the Post model is search-friendly!

Next, we would like to configure the fields that should get indexed in the first place. Of course, you don't want to index all the fields of your model in Algolia to keep it effective and lightweight. In fact, more often than not, you won't need it.

You can add the toSearchableArray in the model class to configure the fields that'll be indexed.

Now, we're ready to import and index existing Post records into Algolia. In fact, the Scout library makes this easy by providing the following artisan command.

That should import all the records of the Post model in a single go! They are indexed as soon as they're imported, so we're ready to query records already. Go ahead and explore the Algolia dashboard to see the imported records and other utilities.

How It Works Altogether

In this section, we'll create an example that demonstrates how to perform search and CRUD operations that are synced in real time with the Algolia index.

Go ahead and create the app/Http/Controllers/SearchController.php file with the following contents.

Of course, we need to add the associated routes as well.

Let's go through the query method to see how to perform a search in Algolia.

Recall that we made the Post model searchable by adding the Searchable trait. Thus, the Post model can use the search method to retrieve records from the Algolia index. In the above example, we're trying to fetch records that match the title keyword.

Next, there's the add method that imitates the workflow of adding a new post record.

There's nothing fancy in the above code; it just creates a new post record using the Post model. But the Post model implements the Searchable trait, so Laravel does some extra work this time around by indexing the newly created record in Algolia. So as you can see, the indexing is done in real time.

Finally, there's the delete method. Let's go through it as well.

As you would have expected, the record is deleted right away from the Algolia index as soon as it's deleted from the database.

Basically, there's no extra effort required from your side if you want to make existing models searchable. Everything is handled by the Scout library using model observers.

And that also brings us to the end of this article!

Conclusion

Today, we discussed how you can implement full-text search in Laravel using the Laravel Scout library. In the process, we went through the necessary installations and a real-world example to demonstrate it.

Feel free to ask if you have any queries or doubts using the comment feed below!