Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vQmod: How to escape <![CDATA[ inside <![CDATA[

Tags:

vqmod

I am trying to add Google code for remarketing tag in

catalog/view/theme/*/template/common/footer.tpl
for use in an Opencart project.

I have created this vQmod

<?xml version="1.0" encoding="UTF-8"?>
<modification>
    <id>Add Google Code for Remarketing Tag in footer</id>
    <version>1.0</version>
    <vqmver required="true">2.4.0</vqmver>
    <author>[email protected]</author>
    <file name="catalog/view/theme/*/template/common/footer.tpl">
        <operation>
            <search position="before" offset="0">
                <![CDATA[</body>]]>
            </search>
            <add><![CDATA[
                    <script type="text/javascript">
                    var google_conversion_id = XXXXXXXXX;
                    var google_custom_params = window.google_tag_params;
                    var google_remarketing_only = true;
                    </script>
                    <script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
                    </script>
                    <noscript>
                    <div style="display:inline;">
                    <img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/XXXXXXXXX/?value=0&amp;guid=ON&amp;script=0"/>
                    </div>
                    </noscript>
                ]]></add>
        </operation>
    </file>
</modification>

which works with one little problem. Tag Assisant (by Google) is complaining (as a minor issue) about "Missing CDATA comments" meaning this

<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = XXXXXXXXX;
var google_custom_params = window.google_tag_params;
var google_remarketing_only = true;
/* ]]> */
</script>

which already contains CDATA.

Is there a way to "escape" CDATA inside CDATA in vQmod?

Thank you!

like image 894
kanenas Avatar asked Dec 06 '25 11:12

kanenas


2 Answers

I am posting this as a workaround, but i will not accept it for a few days in case someone has to suggest something.

<?xml version="1.0" encoding="UTF-8"?>
<modification>
    <id>Add Google code for remarketing tag in footer</id>
    <version>1.0</version>
    <vqmver required="true">2.4.0</vqmver>
    <author>[email protected]</author>
    <file name="catalog/view/theme/*/template/common/footer.tpl">
        <operation>
            <search position="before" offset="0">
                <![CDATA[</body>]]>
            </search>
            <add><![CDATA[
                    <script type="text/javascript">
                        /* ]]><![CDATA[ */
                        var google_conversion_id = XXXXXXXXX;
                        var google_custom_params = window.google_tag_params;
                        var google_remarketing_only = true;
                        /* ]]><![CDATA[ */
                    </script>
                    <script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js"></script>
                    <noscript>
                        <div style="display:inline;">
                            <img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/XXXXXXXXX/?value=0&amp;guid=ON&amp;script=0"/>
                        </div>
                    </noscript>
                ]]></add>
        </operation>
    </file>
</modification>
like image 148
kanenas Avatar answered Dec 10 '25 10:12

kanenas


As @Alexandre Reis Ribeiro suggests, you can also use PHP to combine together the <![CDATA[ into strings. Tip: in large feeds or script arrays where <![CDATA[ is used often, a clean/sane way to do this is to make them variables first. Otherwise, you may end up with a mess to debug.

Simplistic Example:

$c_s = '<![' . 'CDATA['; // defines start of CDATA
$c_e = ']' . ']>'; // defines end of CDATA

// example XML output may need some CDATA, so use $c_s and $c_e:

$output .= "\t" . '<title type="html">' . $c_s  . $title .  $c_e . '</title>' . "\r\n";
$output .= "\t" . '<subtitle type="html">' . $c_s  . $description .  $c_e . '</subtitle>' . "\r\n";

// continue your output/indicies using as much CDATA's as required!
like image 36
dhaupin Avatar answered Dec 10 '25 11:12

dhaupin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!