INTRODUCTION TO UMBRACO MACROS

In order to introduce custom functionality and specialty output to your Umbraco site, you will need to know about macros. A macro is essentially a callable action that executes a predefined chunk of code. This code can be made up of the following technologies/languages:

  • .NET C# and VB User Controls
  • .NET Custom Control (Server Control)
  • XSLT Template
  • IronPython, LOL Code, Ruby, and more

Macros are created in the Developer section image Macros node. You can create one using the following steps:

  1. Navigate to the Developer section in the backoffice, found at http://<install domain>/umbraco.
  2. Right-click Macros, then click the Create menu option, as shown in Figure 5-1.

    FIGURE 5-1

    image

  3. Select the .NET User Control, XSLT template, or other code source and save your Macro.

Figure 5-2 shows a “bird's-eye-view” of how to create macros in Umbraco, giving an overview of the fields and functions, which are described in Table 5-1.

image Umbraco will create the Alias automatically by concatenating the Name value, stripping out spaces and special characters.

In addition to creating the macro manually, you can specify that your XSLT file automatically generate the macro and preselect the newly create XSLT file as the source for this macro. This is the best practice way of creating your XSLT macros.

In a typical site there is little concern about caching your macros. The Cache Period option is mostly for high-traffic sites and should be used with care because it may impact the rendering of certain content. For example, if you are using a .NET user control to pull in stock quotes, the caching set here can impact the information if it is presented in real-time.

FIGURE 5-2

image

TABLE 5-1: Macro Functions

FIELD/FUNCTION DESCRIPTION
Name The human readable name/description of your macro.
Alias This targets the macro in templates and within content when it's later used.
Use XSLT file Select the XSLT file that you want to use. XSLT files are stored in the xslt folder in your Umbraco installation.
or .NET User Control Select the .ascx file that you want like to use. The User Control files must be placed in the usercontrols folder in your Umbraco installation in order to appear in the drop-down list.
or .NET Custom Control Specify the assembly and type for the custom control that you have placed in the site's /bin folder.
or script file Select the script file that you want to use.
Use in editor Selecting this option allows you to select this macro for insertion in the Rich Text Editor data type.
Render content in editor This option renders the contents of your .NET user control within the Rich Text Editor data type in the backoffice.
Cache Period The number of seconds you want the macro to be cached on the client. This reduces the number of trips to the server to regenerate the content.
Cache By Page This is the default option, used to cache the output by URL.
Cache Personalized If selected, the macro caches content by member login. This is useful for personalization features like names, etc.

Macro Parameters

You've surely noticed that we have two tabs for editing a macro. The Parameters tab allows you to specify public properties that can be used to pass in values to the macro. Umbraco comes with a set of predefined types that pick the intended value for the specified property. Table 5-2 shows the details of the various types, while Figure 5-3 shows the output of the various types and how they are presented to the user when inserting your macro.

TABLE 5-2: Macro Parameter Types

image

image

image Naming your macro parameters with reserve .NET tag property names will not work. For example, do not create a macro parameter named Id or CssClass as those are reserved for use in standard .NET tags.

FIGURE 5-3

image

Rendering Macros In Your Site

You have two ways of displaying the results of your created macros independent of how they are sourced (XSLT, .NET, or other). Depending on how you configured the macro in the settings, as described earlier in this chapter in Table 5-2, you can add the macro tag in a Rich Text Editor data type control within your content, as shown in Figure 5-4, or you can add the macro tag directly in your master page templates, as shown in Figure 5-5.

FIGURE 5-4

image

FIGURE 5-5

image

A macro inserted in your template will have the following format:

<umbraco:Macro Alias=“NameOfMacroHere” parameter1=“” parameter2=“”
runat=“server”></umbraco:Macro>

This example shows that parameters, as we discussed in the previous section, are added as simple tag attributes. The result in the Rich Text Editor is slightly different and is not meant to be edited by hand: you use the Macro button shown in Figure 5-4 to edit and control your macro.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset