Developer forum

Forum » Development » Index query expression macro

Index query expression macro

Thomas Larsen
Reply

Is it possible with custom code to add your own expression macros?

BR
Thomas

dw_2016-02-29_15-59-03.png

Replies

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Yes, you just need to inherit Dynamicweb.Extensibility.Macros.Macro from Dynamicweb.Extensibility.dll

An example of one of our macros:

Imports Dynamicweb.Extensibility.Macros
Imports System.Collections.Generic

Namespace eCommerce.Common
    Public Class ContextMacro
        Inherits Macro

        Private Shared ReadOnly SupportedActionsInternal As New Dictionary(Of String, Func(Of Object)) From
            {
                {"LanguageID", Function() Context.LanguageID},
                {"ShopID", Function() Input.FormatString(Dynamicweb.Frontend.PageView.Current.Area.Value("AreaEcomShopID"))},
                {"CartID", Function() If(Context.Cart IsNot Nothing, Context.Cart.ID, Nothing)}
            }

        Private Shared ReadOnly LockObject As New Object()

        Public Overrides Function Evaluate(action As String) As Object
            'Prepare return value
            Dim value As Object = Nothing

            SyncLock LockObject
                'Make sure action is supported
                If SupportedActionsInternal.ContainsKey(action) Then
                    Try
                        'Get value from action
                        value = SupportedActionsInternal(action)()
                    Catch ex As Exception
                        'Maybe log this?
                    End Try
                End If
            End SyncLock

            'Return the value if one was found, otherwise Nothing
            Return value
        End Function

        Public Overrides ReadOnly Property Name As String
            Get
                Return "Dynamicweb.Ecommerce.Context"
            End Get
        End Property

        Public Overrides ReadOnly Property SupportedActions As IEnumerable(Of String)
            Get
                Dim result As IEnumerable(Of String)
                SyncLock LockObject
                    result = SupportedActionsInternal.Keys.ToList()
                End SyncLock
                Return result
            End Get
        End Property
    End Class
End Namespace

Votes for this answer: 1
 
Thomas Larsen
Reply

Thanks, just what i needed

 

You must be logged in to post in the forum