How to disable PHP ASP-style tags in a specific file?

I have a problem with a PHP file that puts a Javascript into a page. JS uses an underline templating engine which is used to use ASP-style tags ( <% ... %>

) for output.

The PHP client config has ASP-style tags that break the file's syntax.

Is there any directive I can add at the top of the file, or any other trick to get the PHP processor to ignore these tags? I am specifically looking for a way to do this without modifying php.ini . But if this is the only way, please let me know.

+3


source to share


3 answers


Try this .htaccess

file:

php_flag asp_tags off

      



And if you just want to disable asp tags for just one file, use this rule here:

<files file-with-asp-syntax.php>
php_flag asp_tags off
</files>

      

+4


source


Is the PHP client config on Windows server? If so, you should be able to set the PHP configuration for each directory by adding an .ini file to it.

It used to be possible to change the use of tags through init_set()

at runtime, for example ini_set('asp_tags', 0)

, but I'm not sure if the latest PHP versions still allow this.



On Apache server you can change the parameter by adding php_flag asp_tags Off

to your .htaccess file

I can't test this now, but hopefully this will give you a head start.

+1


source


I found a way to do this that works GREAT for underscore shortcuts:

page.php:

...
<script type="text/javascript">
    var template = '<?php echo <<<HEREDOC

        <h1>\
            <%- model.title %>\
        </h1>\
        <% if (model.body) { %>\
            <p>\
                <%- model.body %>\
            </p>\
        <% } %>\

HEREDOC;
    ?>';
</script>
...

      

Notes

Blank lines at the beginning and end of HEREDOC serve two purposes:

  • Prevents accidental escaping of closing quote ( '

    ) in javascript
  • Makes it so that the indents and line breaks are exactly as they would be without the wrapper added HEREDOC

    .

Cautions

This is a poor choice for escaping ASP tags associated with actual PHP, because the tag is HEREDOC

interpolated in the same way as a double quoted string ( "

) in PHP, where the variables are output to the resulting file. Docs: PHP String - Variable Version

If you are in control of your server environment and can rely on PHP> 5.3, then you can use NOWDOC to avoid this caveat altogether. NOWDOC differs from HEREDOC in single quotes around the identifier and prevents PHP variable interpolation within the enclosed text. Docs: PHP String - Nowdoc

+1


source







All Articles