24/7 Support: 800.608.6482

Get Started

Videos | Writing a CSV file

ver. 9.0 and later

Video Transcript

One great part of using mvt:assign and mvt:eval in the template language is you get full access to all the file read write functions that are part of Miva Merchant Empressa. Access to these functions turns Miva Merchant Template Language into a powerful programming language. Let's dive in and take a look a little deeper.

The read write functions give you full access to the file system. You can read write directories, files, change permissions, append files, copy files, all the things that you'd expect within a programming language that you can actually do within the template language which is very powerful. Within Miva Merchant there are two different directory structures. One of them is called Data and the other one is called Script or scripts. The data folder is a non-public folder and that's used to save things like the database connection strings, sensitive data, all your exports will be exported to the data directory. This is a specific directory that gets configured when you set up your engine. The scripts directory on the other hand is really just your web root. It's the public folder where all your Mica Merchant files will be located. Both of these directories are defined when you set up and configure the engine. However, because they are completely separate directories and have different permissions, there are actually different functions within Empressa to read and write to one directory or the other. So you need to know ahead of time where you're reading and writing data from to be able to use the right function. We'll dive into that here in a second. so the path to the file that you're going to be reading or writing is always going to be dependent on which directory you're writing to. It's an easy thing to mix up in the beginning where you may be using a function that you could only write to the data directory, but you're trying to write to the scripts directory. The path to the file is always specified from the root and again, the roots going to be either the root of the data or the root of the scripts directory. Before we jump into the code, let me show you a great resource at Mivascript.com. This will tell you what functions are available to use and how to use them.

So here I'm at Mivascript.com and I click on File system over here on the left. This brings up all of the available functions that you can use to access the file system. You'll see here as we talked about earlier, there's separate functions for the Data Folder, the Script Folder and theirs actually newer functions that can be used on Either folder where there's a parameter that defines which folder you want to work with. This should be a go to resource anytime you want to work with the file system. it will tell you which functions are available, what parameters they use, and what their return values are. All these functions are built in to the Miva Merchant engine and this is what allows us to use them within the template language. They are available on any page in Miva Merchant without having to do anything special like calling in an include file or any time of header. So let's jump into some code and see how they use these functions in action. So here we already have some code written which will check to see if the file exists, if it does it will delete it and create a new file. If not, it will just create a new file. The file will be created with a single header row that has three columns. While this code may look complicated, it's actually fairly straightforward. Let's talk through it line by line and see what it does.

So these first three lines up here set up some variables. So we have the file name, which I'm calling Export.csv and that's a string. We have the file path, which in this case is just test. Now we have to define whether this is relative to the data directory or relative to the scripts directory. That's all going to depend on the function we use below. We also have a variable for the newline character. This will be appended to the header row to force it to the next line. This header row variable here is just defining what data we want to write to the file. So right now we have a comma separated list of three headers; product code, product name and product price. Again, we have that newline character appended to that string. That entire string will be written to the file when it's created. So here's the first function we're using to access the file system. So here it's sexists and this "s" stands for script. So that means we're working with the scripts directory. This folder actually needs to be within our web root. All this function is doing is checking for the existence of the file. So we're saying, "If a filename at export.csv in the director test, if that file exists, this function will return 1 back in this variable for file exists that we created. This allows us to test to see if that file exists here. So if file exists = 1, which means the file already exists on the server, then we want to delete the file first so we can create a new one. We use s.delete because we're working in a scripts directory and it's the same file path and file name. Down here we verify that the file was deleted, and if it was deleted we create the file. This file create is actually one of the hybrid functions that works either on the scripts directory or the data directory. You can see that on the second parameter here where we pass in a string value of script. If this function were operating within the data directory instead of script here, it would say Data. So this file create function is going to work from the scripts directory and it will create a file named exports.csv inside the test directory. This third parameter is the data that is being written to the file. So it's this header row that we defined up here. Now this line down here is exactly the same as the line above. This is just for the use case where the file didn't exist already. So let's take this code and run it within Miva and see what happens.

So here I am back in the Miva Merchant Admin. I'm going to go to the Storefront page. I'm going to paste this code in and hit "Update." Now I'll come back to the front end and hit "Refresh." So we don't have any code that output to the page to tell us what happened. So, hitting refresh just refreshes the page, but I can't really see what happened. In order to see what actually happened, we need to go look at the ftp and the server. So here I am logged into the server. Now I'm in the httpdocs folder, which is our scripts directory. Now, how do I know that's our scripts directory? That's defined by the engine configuration when Miva is set up. But it's always going to be your web root. Webfoot is going to be your scripts directory. The data directory let me show you that real quick. If I go out of this directory here, it's typically in "Private" and then this folder called "Mivadata." So this Mivadata folder is your data directory. I open it here and this has files like merchantdb.dat is your database connection string. This has a lot of sensitive information. This is not a web accessible directory. It's only accessible through ftp. So I'm going to go back out here and I'm going to go to my scripts folder. Here I have a directory called test. Now I had to create this ahead of time. This wasn't created automatically for me. If this directory didn't exist, my file write would fail because the path to the file wouldn't exist. If I go into this directory here, you should have a file called cave. I'm going to open it real quick to validate what's in there. You'll see it's a three-column file written as a csv with the column headers that we've defined within our file.

Let's take this one step further. What if we didn't want to check ftp every time just to see if the file was created or not? We can do that by adding some logic here that will help us see that the file was created or not. So here I have two if statements. The first one is if the file was deleted equals one. This will tell us if a file was deleted on the server. The second one is if file is not equal to negative one. So if the file created fails, the function will return negative one. If it succeeds it returns the length of the file. So here the file created is not equal to negative one, that means the file was created, else, there was an error. So this code will tell us what's going on without having to look at ftp all the time. So I'm going to bring this back over to Miva, paste it in and save it and then go back over the front end and hit Refresh. So here we see the two lines were output. The file was deleted and the file was created. The reason the file was deleted was because the file already exists on the server. So the file first got deleted and then it was created. If I go into ftp and I delete the file manually, and then come back here and refresh, I should only see one line, the file was created, which is what appears. Having full access to the file system is an incredibly powerful part of Mica Merchant. These functions are flexible, easy to use and available anywhere you need them.



Example Code




comments powered by Disqus

Looking for Developer Docs?

We have a whole section for that, including: Developer Training Series, Template Language docs, Module Development tutorials and much, much more.

Head to the Developer Section

Miva believes that all online businesses should have access to a scalable ecommerce platform that can meet their unique business requirements. Miva offers PCI compliant ecommerce, hosting, and custom website design and development solutions. Miva customers have processed over $100 billion in online sales since 1997.

Copyright © 2016 Miva, Inc - All Rights Reserved   Privacy Policy | Store Policy

Links
Contact Us
Receive Tips & Updates

Copyright © 2017 Miva, Inc - All Rights Reserved

Back To The Top