Create content preview page

In this tutorial you will learn how to create a page to preview the content you are editing, without saving it. The preview will be created using JavaScript and you will add a button to one of KTML's toolbars.

 

Before starting off in this tutorial you must have:

  1. A working KTML control applied on a page - either from Dreamweaver, Visual Studio or manually. The steps to follow in order to create such a page can be found here.

  2. The preview button's image - you can find it inside the downloaded package, in docs/tutorials/Extending KTML/Create preview.

  3. A text editor.

The preview feature will not be integrated as a module, but directly on the page containing the KTML control. You will have to add JavaScript code for the function that creates the preview action and for the button creation. You will then add the button to each KTML control on the page.

To create the preview function, follow the next steps:

  1. Copy the preview.gif file from the downloaded package, in docs/tutorials/Extending KTML/Create preview/img/ to <site_root>\includes\ktm\core\img\. This image will be displayed as the preview button.

  2. Open the page where you have KTML applied onto with a text editor.

  3. Locate the code that creates the KTML control, after the textarea or hidden field definition. The code should look like (replace ktml1 with the actual control name):

    <script type="text/javascript">
       // KTML4 Object
      ktml_ktml1 = new ktml('ktml1');
    </script>

     

  4. Right before it, add the following code block:

    <script>
    function previewKTML(k) {
    k.save(function() {
    var value = k.formElement.value;
    wnd = utility.window.openWindow("ktml_preview_window", k.iframeSRC2, window.screen.availWidth-5, window.screen.availHeight - 45);
    if (wnd) {
    html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' + "\r\n";
    html += '<html><head><link href="' + k.getModuleProperty("css", "PathToStyle") + '" rel="stylesheet" type="text/css"></head><body>';
    html += value;
    html += '<input type="button" value="Close" onclick="window.close()" />';
    html += '</body></html>';
    wnd.moveTo(-3,-3);
    wnd.document.open("text/html; charset=" + k.charset);}
    wnd.document.write(html);
    wnd.document.close();
    }
    });
    }

     

  5. The code block above does the following:

  6. Next you have to create a toolbar button. After the last closing brace in the block above add the following code:

    var previewButton = new ToolbarButton({
    'name': 'preview',
    'button_type': 'img',
    'button_icon': KtmlRelativeImagePath + 'preview.gif',
    'command_type': $KT_JS_STRING,
    'command_string':'previewKTML(this.ktml)'
    });

     

  7. The code above is the standard way of creating an instance of the ToolbarButton class. The properties this call sets are:

  8. The last thing to do is add the button to the toolbar. This is done by adding the code:

    ktml_ktml1.addCustomButton(previewButton, 'toggle_fullscreen', 0);
     

  9. You have to replace ktml1 with the actual name of the KTML control you have on the page. If you have more than one control on page, simply add another call as the one above. You do not need to duplicate the entire code.

  10. Add the ending </script> tag.

  11. Save the file and close it.

 

When you preview the page in the browser, next to the full screen button, the preview will be displayed as well:

 

 

Formatting, images and even inserted media will be displayed correctly: