package org.jliquid.liqp.tags;

import org.jliquid.liqp.Template;
import org.jliquid.liqp.nodes.LNode;

import java.io.File;
import java.net.URL;
import java.util.Map;

class Include extends Tag {

    public static String snippetsFolder = "snippets";
    public static String extension = ".liquid";

    @Override
    public Object render(Map<String, Object> context, LNode... nodes) {

        try {
            String fileNameWithoutExt = super.asString(nodes[0].render(context));

            
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            URL url = loader.getResource(snippetsFolder + "/" + fileNameWithoutExt + extension);
            
            Template include = Template.parse(new File(url.toURI()));

            // check if there's a optional "with expression"
            if (nodes.length > 1) {
                Object value = nodes[1].render(context);
                context.put(fileNameWithoutExt, value);
            }

            return include.render(context);

        } catch (Exception e) {
            return "";
        }
    }
}
    
@@ -483,32 +483,34 @@
 ;%0A%0A            %0A
+//
             Clas
@@ -574,16 +574,18 @@
 ader();%0A
+//
         
@@ -673,29 +673,21 @@
 nsion);%0A
-            %0A
+//%0A//
         
@@ -744,25 +744,29 @@
 .toURI()));%0A
-%0A
+//%0A//
             
@@ -814,16 +814,18 @@
 ession%22%0A
+//
         
@@ -848,24 +848,26 @@
 ngth %3E 1) %7B%0A
+//
             
@@ -911,16 +911,18 @@
 ntext);%0A
+//
         
@@ -969,16 +969,18 @@
 value);%0A
+//
         
@@ -985,17 +985,21 @@
       %7D%0A
-%0A
+//%0A//
         
@@ -1031,24 +1031,93 @@
 (context);%0A%0A
+            //TODO Support includes properly%0A            return %22%22;%0A%0A
         %7D ca

Tweak include support