Jump to content

Using comments as template expressions


skin27
 Share

Recommended Posts

In some cases it's more readable and convenient (for example when generating an email or a html page) to write the code in multiple lines, instead of one large line or building it with variables and macros. Of course AutoIt doesn't support template expressions, so here's an example to work around it (using comments as template expressions).

The templates can be placed in the same script or in a separated file. Variables in the template are evaluated before passing it to the new variable. For beta users: Variables needs a $ sign in the template to get recognized.

These are just some examples of using templates. This is not recommended for general use, because there is no error-handling for templates from comments! Maybe some next version of Autoit will have builtin support for template expressions.

; AutoIt: V3.3.8.1
#include <String.au3>
#include <Constants.au3>
#include <File.au3>
#include <IE.au3>

Local $name1 = "Joe", $name2 = "Sara", $userID = "452ce2", $file = @ScriptDir & "\" & @ScriptName

; *******************************************************
; Example A - Opens a messagebox with text from the Template A
; variables are automatically evaluated
; *******************************************************
$ExampleA = TemplateExpression("A", $file)
MsgBox(0, "Example A", $ExampleA)
#cs Template(A)

    Hello $name1,

    Your userId is $userID.

    Sincerly

    $name2

#ce EndTemplate
; *******************************************************
; Example B - Open a messagebox which is executed
; from template B
; *******************************************************
$ExampleB = TemplateExecute("B", $file)
#cs Template(B)
    MsgBox
    0
    "Example B"
    "My name is $name1"
#ce EndTemplate

; *******************************************************
; Example C - Opens a browser with hmlt code based on Template C
; *******************************************************

$ExampleC = TemplateExpression("C", $file)
Local $oIE = _IECreate()
_IEBodyWriteHTML($oIE, $ExampleC)

#cs Template(C)

    <p>
    <big>Example C by $name1</big>
    </p>
    <p><b>This text is bold</b></p>
    <p><strong>This text is strong</strong></p>
    <p><big>This text is big</big></p>
    <p><em>This text is emphasized</em></p>
    <p><i>This text is italic</i></p>
    <p><small>This text is small</small></p>
    <p>This is<sub> subscript</sub> and <sup>superscript</sup></p>

#ce EndTemplate


; Version: 1.00. AutoIt: V3.3.8.1
; Evaluates the variables and put template text in a String variable
Func TemplateExpression($templateName = 0, $script = 1)
    Local $variables, $template
    $rawTemplate = GetTemplate($templateName, $script)
    If IsArray($rawTemplate) Then
        $template = $rawTemplate[0]
        $variables = StringRegExp($rawTemplate[0], '\$\w+', 3)
        If IsArray($variables) Then
            For $variable In $variables
                $template = StringReplace($template, $variable, Eval(StringTrimLeft($variable, 1)), 0, 1)
            Next
        EndIf
    Else
        Return -1
    EndIf
    Return $template
EndFunc   ;==>TemplateExpression


; Version: 1.00. AutoIt: V3.3.8.1
; Builds the function from the template expression and executes it
Func TemplateExecute($templateName = 0, $script = 1)
    Local $rawFunction, $function = 1
    $template = TemplateExpression($templateName, $script)
    $rawFunction = StringSplit($template, @CR)
    For $i = 1 To $rawFunction[0] Step 1
        If StringStripCR(StringStripWS($rawFunction[$i], 8)) = Not "" And $function = 1 Then
            $function = $rawFunction[$i] & "("
        Else
            $function = $function & StringStripWS($rawFunction[$i], 3) & ","
        EndIf
    Next
    $function = StringStripCR(StringTrimRight(StringStripWS($function, 1), 2)) & ")"
    Execute($function)
EndFunc   ;==>TemplateExecute


; Version: 1.00. AutoIt: V3.3.8.1
; Extracts the template from a file
Func GetTemplate($templateName = 0, $script = 1)
    Local $scriptText, $rawTemplate
    $scriptText = FileRead($script)
    $rawTemplate = StringRegExp($scriptText, "Template\(" & $templateName & "\)((\n|.)*?)#ce EndTemplate", 3)
    Return $rawTemplate
EndFunc   ;==>GetTemplate
Link to comment
Share on other sites

And what if i compile the script? :)

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...