ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Safe way to put text in emebeded variables using {embed}

January 29, 2009 4:41pm

Subscribe [2]
  • #1 / Jan 29, 2009 4:41pm

    jayf

    41 posts

    I often use embedded templates, using the embed tag with variables:

    {embed="site/main" stuff="{var}"}

    In the above example, if the {var} contains quotes, the embed fails, e.g.,

    if {var} = Pooh’s “favorite” food

    I think this ends-up being processed like:

    {embed="site/main" stuff="Pooh's "favorite" food"}

    Which doesn’t work due to the confusion between the text value and the variable assignment syntax.

    So, in EE, is there a safe way to put text in embedded variables—e.g., that properly escapes quotes or otherwise avoids conflicts?

    Note too that EE’s automatic HTML entity escaping doesn’t match all the cases where I’d like to do this (it does match some). In some cases (e.g., <div class=“example”></div>), I really need the double-quote to be a double-quote in the end.

    Thanks!

  • #2 / Jan 29, 2009 9:17pm

    jayf

    41 posts

    I probably can do something custom with PHP or the Find / Replace plugin. But, I was hoping there was a standard, built-in, way to handle this.

    Is there no standard way to do this?

  • #3 / May 18, 2009 9:39pm

    jayf

    41 posts

    I now have two “safe” ways I handle this (I figured this out myself, apparently):

    This is an example of what I do for simple cases:

    {embed="site/.something" 
       myvar="{exp:replace find='QUOTE' replace='\\x22'}{body}{/exp:replace}"
    }

    And, then, in my embedded template, I do the reverse:

    {exp:replace find='\\x22' replace='QUOTE'}{embed:myvar}{/exp:replace}

     

    This is an example of what I do for more complex cases—it uses PHP, parsed on input, and the Expression Engine global $SESS object (see $SESS->cache in the Add-on Development Guidelines):

    <?php
    $i = 0;
    global $SESS;
    {categories}
    $SESS->cache['youruniquename']['categories'][$i]['url'] = '{category_url_title}';
    $i++;
    {/categories}
    ?>

    Then, in the embedded template, I do things like:

    <?php
    global $SESS;
    echo $SESS->cache['youruniquename']['categories'][0]['url'];
    ?>
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases