harrydeluxe / php-liquid

A PHP port of Ruby's Liquid Templates
http://www.delacap.com/artikel/Liquid-Templates/
MIT License
239 stars 119 forks source link

500 error in response in codeigniter. #53

Closed vishal-px closed 6 years ago

vishal-px commented 6 years ago

Hi @harrydeluxe

Thanks for this package. I have used this package in my localhost and its working fine. Now I want to use this package in CodeIgniter, so I have added this package at this path "application/third_party/php-liquid-compiler/liquid". And now I have created a library for using this package.

Here is my library code, This code inside in "application/libraries/".

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class Phpliquidcompiler
{
    public function phpLiquid(){
        $path = __DIR__ . "/../third_party/php-liquid-compiler/liquid/";
        $loader = require  $path . 'vendor/autoload.php';
        $loader->addPsr4('Liquid\\',  $path . 'src/Liquid');

        print_r($loader);
        use Liquid\Liquid;    // At here I getting 500 error.
        use Liquid\Template; 

    Liquid::set('INCLUDE_SUFFIX', '');
    Liquid::set('INCLUDE_PREFIX', '');
    Liquid::set('INCLUDE_ALLOW_EXT', true);
    Liquid::set('ESCAPE_BY_DEFAULT', true);

    $liquid = new Template();
    $liquid->parse('{% assign special= "sd" %} {% if special != "" %} <div><a
    href="">[special]sdsdsd</a></div> {% endif %}');
    echo $liquid->render();
    }
}
?>

Calling above code from controller.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller{
    public function index(){
    $this->load->library('phpliquidcompiler');
        $this->phpliquidcompiler->phpLiquid();
    }
}

?>

Will you please tell me where I am wrong ?

Thanks.

vishal-px commented 6 years ago

I got the solution. This is a correct library code.

<?php 
if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

$path = __DIR__ . "/../third_party/php-liquid-compiler/liquid/";
$loader = require  $path . 'vendor/autoload.php';
$loader->addPsr4('Liquid\\',  $path . 'src/Liquid');
use Liquid\Liquid;
use Liquid\Template;
use Liquid\Cache\Local;

class Phpliquidcompiler
{
    public function phpLiquid($html = NULL){
        Liquid::set('INCLUDE_SUFFIX', '');
        Liquid::set('INCLUDE_PREFIX', '');
        Liquid::set('INCLUDE_ALLOW_EXT', true);
        Liquid::set('ESCAPE_BY_DEFAULT', true);
        $liquid = new Template();
        $liquid->parse('{% assign special= "" %} {% if special != "" %} <div><a
        href="">[special]sdsdsd</a></div> {% endif %}');
        echo $liquid->render();
    }
}
?>