Scripts And Components

So far all of the code we've created has been part of "View". The view is the part which the user sees and with which they interact. There is more code, however, that drives a website than just the view and we want to separate that into its own files. This is called Separation of Concerns ( https://en.wikipedia.org/wiki/Separation_of_concerns ).

In BoxLang, the view layer is typically found in .bxm files. BXM stands for BoxLang Markup. Other formats include .bxs files which are scripts which generally process data, are meant to be run all at once, and don't have a large visual component. The last are BoxLang classes. These end in .bx and are typically a collection of functions which are called by other files to do a job. The tag / component syntax at which we've been looking is primarily used in templates. The other formats use almost exclusively a different syntax called script.

What does a BoxLang class look like? At its simplest, a .bx file looks like this:

class {
// Place your content here
}

There are no <> in sight. The class keyword is the first thing in the file followed by {. The very end of the file is } which indicates the end of the class.

Inside the component {}, there are functions which would look like this.

class {
    function myFunction(){
    }

    function anotherFunction(){
    }
}

There can technically be any number of functions in your class but convention and "best practices" typically say to have no more than 10. That's a guideline, not a "law".

The first thing we need to do is "connect" the .bxm and the .bx files. This way, the bxm can access functions in the bx and display the results. To do that, we assign the class to a variable in the bxm. This example assumes that the class is in the same folder as the bxm which is not always (actually, not usually) the case but it is a good place to start.

    ```<bx:set myComponent = createObject("myComponent") />

	The product of my two numbers is <bx:output>#mycomponent.multiplyTwoNumbers(5,7)#</bx:output>
    ```

Back in our component, we would then create a function called "multiplyTwoNumbers" that accepted 2 arguments and returned the product. It might look like this:

class {
    function multiplyTwoNumbers(required numeric firstNum, required numeric secondNum){
			return firstNum * secondNum
    }
}

In the above component, the function multipleTwoNumbers accepts two arguments.

firstNum is required meaning that the function HAS to receive it or it will throw an error. It also has to be a number, or be able to be converted to a number, or it will throw an error. This means that the string "5" will still be accepted but the string "fish" will throw an error.

secondNum is the same as first num - required and numeric.

return firstNum * secondNum <--- this means to multiply the two numbers together and return the product to the place that called it. In this case, this is the cfm page from where it was called.

Question

In the BoxLang folder, edit the file called component.bxm which is a template page and myComponent.bx which is a class. In component.bxm, make a form that accepts 3 numbers. When the form is submitted, send the three numbers to a function in myComponent and display the product of the three numbers on the page.

Lesson 10 of 12 - Learn BoxLang