miva_template_compile()#
Think of the Storemorph® template compiler as a stripped down Miva Script compiler; a compiled template is nothing but a compiled MivaScript file that behaves in a certain way. Templates may contain HTML, Text, Java Script, XML and elements of the template language. Compiled templates contain a single function Template_Render()
. Execute the template by calling that function, not by executing the compile template .mvc file directly
miva_template_compile( signat, source var, sourceitems var, target, errors var )
Returns#
Returns 1 if compilation succeeds, 0 if error occurred.
Parameters#
Parameter | Description |
---|---|
signat |
Any text. The md5 hash value of this text will bestored in the MVC file. |
source |
The source to compile into a template file. |
sourceitems |
array of valid item names.The compiler will return an error if an mvt:item tag has a “name” attribute that is not in this list. |
target |
The name of the file (relative to the script directory) to put the .mvc file. |
errors |
If the function returns 0, this will contain error text. |
Template Compiler Language#
The template compiler language contains many of the basic constructs of other programming languages and follows the same rules as Miva Script. In the examples below refer to variablename
, expressions
and operators
as documented elsewhere on this site.
Global variables are prefixed with g.
(e.g. g.firstname
); local variables however must be prefixed with l.
(e.g. l.results
or l.settings:product
). Miva Merchant local variables are contained within the single construct l.settings
.
Also MivaScript functions and operators may be used in expressions and conditionals.
Important
The syntax for expressions and literals is different between MivaScript and Storemorph.
- Expressions: MivaScript
VALUE="{ g.fname $ g.lname }"
is equivalent to Storemorphvalue="g.fname $ g.lname"
. - Literals: MivaScript
VALUE="Miva Script"
is equivalent to Storemorphvalue=" 'Miva Script' "
. Note the single quotes inside the double quotes indicating a string literal.
Examples#
Variables#
Allows the printing of variables to the browser screen. There are 4 variations on this command:
&mvt:variablename;
— Prints a variable directly to the screen.&mvte:variablename;
— Prints a variable after applying theencodeenteties()
function. Usingencodeenteties
prevents HTML injection.&mvta:variablename;
— Prints a variable after applying theencodeattribute()
function.&mvtj:variablename;
— Prints a variable output with the correct encoding for use within JavaScript quoted strings.&mvts:variablename;
— Printsslugify()
encoded output for use when creating URI friendly (SEO and human readable) links.
Logged in as &mvt:global:username;
<h1>&mvt:product:name;</h1>
<input type="text" name="username" value="&mvte:global:username;" size="25" />
<a href="/&mvta:category:code;.html">&mvte:category:name;</a>
Comments#
Allows the addition of notes or block lines of code that are removed from compilation and execution.
<mvt:comment> Your comments go here. </mvt:comment>
Assign#
Assigns the value of a number, string or complex expression to a named variable. String expressions must be enclosed in single quotes.
<mvt:assign name="varname" value="expression" />
<mvt:assign name="g.discount" value="l.settings:product:price * .25" />
<mvt:assign name="g.html_description" value="'<p>' $ g.description $ '</p>'" />
Call#
Exposes MvCALL
functionality to the template language. See MvCALL
for complete documentation.
Important
Notice the difference between MVCALL
and mvt:call
attribute literals. In MivaScript METHOD="GET"
— GET
is a string literal; in templates method="GET"
is looking for a variable and method=" 'GET' "
is the equivalent string literal.
<mvt:call action="'fully qualified url'" method="'method_keyword'"> process the call in a loop... </mvt:call>
<mvt:call action = "string: { expression } | 'literal'"
method = "'literal keyword' containing 'GET|POST|HEAD|XML|RAW|OPTIONS|PUT|DELETE|TRACE|CONNECT'"
content-type = "string: { expression } | 'literal'"
fields = "{ expression } | 'literal variable-name list'"
files = "{ expression } | 'literal'"
certfile = "{ expression } | 'literal'"
certtype = "{ expression } | 'literal'"
certpass = "{ expression } | 'literal'"
timeout = "{ expression } | 'literal'"
headers = "{ expression } | 'literal'">
<mvt:callstop>
<mvt:callcontinue>
</mvt:call>
This example loads content from an external website and displays it on the page.
<mvt:call ACTION="'<a href="http://www.content_server.com/incude/X_Files_Cast.html">http://www.content_server.com/incude/X_Files_Cast.html</a>'" METHOD="'GET'">
<mvt:assign name="g.return" value="g.return $ $ s.callvalue" />
</mvt:call>
<mvt:eval value= "{ g.return }">
Do#
Exposes MvDO
functionality to the template language. See MvDO
for complete documentation.
<mvt:do file = "string: { expression } | 'literal'"
name = "string: { expression } | 'literal'"
value = "{ function_name( paramiters ) }" />
<!--
This example accesses the compiled .mvc file, stored in the variable g.Module_Library_DB,
and calls the function Basket_SubTotal(). The results are returned in g.basket_total.
-->
<mvt:do file ="g.Module_Library_DB" name="g.basket_total" value="Basket_SubTotal(g.Basket:BASKET_ID)" />
Eval#
Outputs the results of an expression to the browser screen.
<mvt:eval value="expression" />
If#
Branches execution based on the results of conditional expressions.
<mvt:if expr="conditional">
<mvt:elseif expr="conditional">
<mvt:else>
</mvt:if>
Exit#
This tag causes the page template code to exit from the current process rather than continuing through the rest of the code on the page.
<mvt:if expr="l.settings:basket:empty">
Your shopping basket is currently empty.<br>
<mvt:exit />
</mvt:if>
Miva#
Allows templates to control whitespace compression and disable all non-explicit output. If output is disabled, only StoreMorph tokens and explicit <mvt:eval>
tags will generate output.
<mvt:miva output="on|off" compresswhitespace="on|off" />
Foreach#
Used for looping through arrays of data where the array
attribute contains the array variable. In the example below products_list
refers to the local array l.settings:products_list
. Global variable arrays may be referenced with the special syntax array="global:products_list"
.
The iterator
attribute refers to a local “copy by reference” of a single array record.
Internally the foreach command keeps a counter (i.e. pos1
) that you can access in the conditional expression as shown below. The foreach commands may be nested and so a new counter variable is used for each nested loop (i.e. pos2
, pos3
etc.)
<mvt:foreach iterator="item" array="products_list">
<mvt:if expr="pos1 GT 12">
<mvt:foreachstop />
<mvt:comment> Stop after 12 </mvt:comment>
</mvt:if>
<mvt:if expr="pos1 MOD 3">
<mvt:foreachcontinue />
<mvt:comment> Skip each 3rd item</mvt:comment>
</mvt:if>
<mvt:eval value="l.settings:item:code" /><br>
</mvt:foreach>
While#
Loops through the code between mvt:while
and /mvt:while
until the condition in condition1
is false. Optionally you can exit the loop using mvt:whilestop
or skip code execution with mvt:whilecontinue
.
<mvt:while expr="condition1">
<mvt:if expr="condition2">
<mvt:whilestop />
</mvt:if>
<mvt:if expr="condition3">
<mvt:whilecontinue />
</mvt:if>
</mvt:while>