ExpressionEngine rendering JS code with { } brackets

前端 未结 4 2150
面向向阳花
面向向阳花 2020-12-16 06:16

Is there a way to force expression engine to NOT render items within curly brackets as EE code? The google chart tools uses javascript code that contains curly { } brackets,

4条回答
  •  天命终不由人
    2020-12-16 06:51

    ExpressionEngine's Template Class parses curly braces {} as template variables, looking for three kinds of variables: single, pair, and conditional variables:

    // Single Variable
    {summary}
    
    // Pair Variable
    {category} ... {/category}
    
    // Conditional Variable
    {if segment_2 != ""} ... {/if}
    

    Curly braces in CSS are considered a special condition.

    For example, the following CSS is acceptable to place anywhere in a template, and gets intelligently parsed:

    
    

    Unfortunately, JavaScript is handled differently and prevents the Advanced Conditionals Parser from processing anything in tags. For example, the following JavaScript with curly braces all on one line:

    
    

    Will be parsed by ExpressionEngine as a template variable and rendered as:

    
    

    You'll notice everything starting at the opening { and ending with the closing } curly brace gets parsed and replaced! As a workaround, you can place the braces on separate lines and avoid this problem:

    
    

    If you've written a JavaScript function that expects values from ExpressionEngine, just place your braces on separate lines — which is a good coding convention and is optimal for readability:

    
    

    As suggested by Ben, you can change this behavior by setting a Hidden Configuration Variable: $conf['protect_javascript'] = 'n';

提交回复
热议问题