Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Brett Hadley 16 posts 36 karma points
    Oct 18, 2012 @ 10:57
    Brett Hadley
    0

    RegisterClientScriptBlock: How to build a JavaScript block from variables?

    Hello everyone,

    I'm making a macro that creates a gallery and I want to build a JavaScript block based on macro paramters for different options. In XSLT I would do it like this:

    <xsl:variable name="initstr">
        <xsl:text>
            $('#</xsl:text><xsl:value-of select="$galleryId"/><xsl:text>').royalSlider({
              fullscreen:{
                enabled:derpProof(</xsl:text><xsl:value-of select="$fullScreen"/><xsl:text>),
                nativeFS: true
              },
              fadeinLoadedSlide: true,
              controlNavigation: 'thumbnails',
              thumbs:{
              orientation: '</xsl:text><xsl:value-of select="$orientation"/><xsl:text>'
              },
              autoScaleSlider:derpProof(</xsl:text><xsl:value-of select="$autoScale"/><xsl:text>),
              autoScaleSliderWidth:</xsl:text><xsl:value-of select="$galleryWidth"/><xsl:text>,
              autoScaleSliderHeight:</xsl:text><xsl:value-of select="$galleryHeight"/><xsl:text>,
              loop:derpProof(</xsl:text><xsl:value-of select="$autoLoop"/><xsl:text>),
              numImagesToPreload:4,
              arrowsNav: derpProof(</xsl:text><xsl:value-of select="$showArrows"/><xsl:text>),
              arrowsNavAutohide: true,
              arrowsNavHideOnTouch: true,
              keyboardNavEnabled: true
            });
          });
        </xsl:text>
      </xsl:variable>
    <xsl:value-of select="umbraco.library:RegisterClientScriptBlock('RoyalInit', $initstr, 'True')" disable-output-escaping="yes"/>

     

    How would I go about implementing this into razor?

    Thanks -Brett

  • Brett Hadley 16 posts 36 karma points
    Oct 18, 2012 @ 17:55
    Brett Hadley
    0

    Found a way to do it but its messy having to double all the { and } to escape them. For those interested:

    string scriptBlock String.Format(
                    @"
                    $(document).ready(function(){{
                      $('#{0}').royalSlider({{
                        fullscreen:{{
                        enabled:{1},
                        nativeFS: true
                      }},
                      fadeinLoadedSlide: true,
                      controlNavigation: 'thumbnails',
                      thumbs:{{
                        orientation: '{2}'
                      }},
                      autoScaleSlider:{3},
                      autoScaleSliderWidth:{4},
                      autoScaleSliderHeight:{5},
                      loop:{6},
                      numImagesToPreload:4,
                      arrowsNav:{7},
                      arrowsNavAutohide: true,
                      arrowsNavHideOnTouch: true,
                      keyboardNavEnabled: true
                      }});
                    }});"
                    @Parameter.galleryId,
                    @Parameter.fullScreen,
                    @Parameter.orientation,
                    @Parameter.autoScale,
                    @Parameter.autoScaleWidth,
                    @Parameter.autoScaleHeight,
                    @Parameter.loop,
                    @Parameter.showArrows);

    If anyone knows a better way, please post!

    Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft