Jump to content

Is it possible to remove debug html comments in tpl file?


Recommended Posts

Hi,

I am programming a module which has a single controller that sets the template file through `$this->setTemplate('path_to_file');` and in that .tpl file I am simply outputting the JSON object through a template variable that I assigned in my controller with ` $this->context->smarty->assign(['nsCheckStock' => $this->checkStatus()]);`

The problem.

When PrestaShop is in production mode everything works fine. However, when in debug mode I get the following error because PrestaShop adds HTML style comments to my tpl file.

`SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data`

i.e. <!-- begin module:path/to/module/checkstatus.tpl -->

Is there anyone to tell PrestaShop or Smarty not to output these comments when in debug mode?

Here is the complete code of the controller

<?php
class ns_****_stockCheckStockModuleFrontController extends ModuleFrontController {
    private $ch;    

    public function initContent() {
        $this->context->smarty->assign(['nsCheckStock' => $this->checkStatus()]);

        
        $this->setTemplate('module:path/to/module/checkstatus.tpl');

        $this->cleanUp();
    }

    private function checkStatus() {
        $reference = Tools::getValue('reference');
        if(!$reference) return json_encode(["status" => "error", "message" => "no reference number given"]);

        // Validate Twindis reference
        if(!$this->validateTwindisReference($reference)) return json_encode(["status" => "error", "message" => "reference validation error"]);

        $headers = array();
        $headers[] = 'Authorization: Basic ' . base64_encode('***********************************');

        // create curl resource
        $this->ch = curl_init();

        // set url
        curl_setopt($this->ch, CURLOPT_URL, "https://www.example.com/api/product/$reference/");

        // set headers
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);        

        //return the transfer as a string
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);

        // $output contains the output string
        return curl_exec($this->ch);
    }

    private function validateTwindisReference($ref) {
        if(!preg_match('/P\d+/', $ref)) {
            return false;
        }

        return true;
    }

    private function cleanUp() {
        // close curl resource to free up system resources
        if(!is_null($this->ch)) curl_close($this->ch);    
    }
}

The template file

{Header('content-type:application/json')}
{$nsCheckStock nofilter}

Thanks in advance to anyone who replies.

I am still quite new to this so please feel free to tell me if I am taking the wrong approach.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...