File Handling#
File handling in Miva Merchant allows developers to interact with files directly within the template language, enabling dynamic data processing, configuration management, and file generation. This guide explores how to read, create, and manage files using Miva Engine functions.
Reading Files#
The file_read
function reads the contents of a file and stores it in a variable. This is useful for processing data from text files, CSV files, or configuration files.
Syntax#
<mvt:assign name="g.file_read" value="file_read('FILE_PATH','VARIABLE_NAME', g.return_value)" />
Parameters#
Parameter | Description |
---|---|
FILE_PATH |
Path to the file to read. Can be absolute or relative. |
VARIABLE_NAME |
Variable to store the file’s contents. |
RETURN_VALUE |
Indicates success or failure of the operation. |
Reading a File#
<mvt:assign name="g.file_read" value="file_read('/data/example.csv', 'script', g.data)" />
<mvt:if expr="g.file_read NE -1">
<p>File read successfully!</p>
<mvt:else>
<p>Error reading file.</p>
</mvt:if>
Creating Files#
The file_create
function allows you to dynamically create new files and populate them with data.
Syntax#
<mvt:assign name="g.file_create" value="file_create('FILE_PATH', 'FILE_CONTENT', 'DIRECTORY_TYPE')" />
Parameters#
Parameter | Description |
---|---|
FILE_PATH |
Path to the file to create. Can be absolute or relative. |
FILE_CONTENT |
Content to write into the file. |
DIRECTORY_TYPE |
Directory where the file will be created (Data or Script ). Defaults to Script . |
Creating a File#
<mvt:assign name="g.file_create" value="file_create('/data/output.txt', 'This is a test file.', 'data')" />
<mvt:if expr="g.file_create EQ 0">
<p>File created successfully!</p>
<mvt:else>
<p>Error creating file.</p>
</mvt:if>
Error Handling#
Proper error handling ensures that your file operations are robust and predictable. Always validate paths and handle errors gracefully.
Error Handling with File Read#
<mvt:assign name="g.file_read" value="file_read('/invalid/path.txt', 'script', g.data)" />
<mvt:if expr="g.file_read NE -1">
<p>File content: &mvt:global:data;</p>
<mvt:else>
<p>Error: File could not be read. Please check the path and permissions.</p>
</mvt:if>
Processing File Data#
After reading a file, you can manipulate its contents for specific use cases, such as processing CSV files.
Parsing a CSV File#
<mvt:assign name="g.file_read" value="file_read('/data/example.csv', 'script', g.data)" />
<mvt:if expr="g.file_read NE -1">
<mvt:assign name="g.counter" value="1" />
<mvt:assign name="l.line" value="gettoken(g.data, asciichar(10), g.counter)" />
<mvt:while expr="NOT ISNULL l.line">
<mvt:assign name="l.column1" value="gettoken(l.line, asciichar(44), 1)" />
<mvt:assign name="l.column2" value="gettoken(l.line, asciichar(44), 2)" />
<p>Column 1: &mvt:line:column1; | Column 2: &mvt:line:column2;</p>
<mvt:assign name="g.counter" value="g.counter + 1" />
<mvt:assign name="l.line" value="gettoken(g.data, asciichar(10), g.counter)" />
</mvt:while>
<mvt:else>
<p>Error reading file.</p>
</mvt:if>
Summary#
Miva Engine’s file handling functions offer powerful capabilities for reading, creating, and managing files directly within your templates. By mastering these functions, you can:
- Process and manipulate external data.
- Dynamically generate files for export or logging.
- Ensure robust and error-free file operations.
Continue to explore advanced topics, such as System Variables →, to further enhance your Miva Merchant templates.