Giter Club home page Giter Club logo

Comments (5)

ericmartineau avatar ericmartineau commented on September 21, 2024 1

btw - I really like the project. I have a fork of it where I'm working on a few things to help with the way I need to use it. It's a partial port to kotlin, so it's a bit of a mess, but there might be something that interests you.

  • Making the engine more stateless (getting rid of statics)
  • Creating components for separate concerns: parsing (TemplateFactory), and rendering (TemplateEngine)
  • Making the engine immutable as much as possible, with "thaw" and builder support
  • Added "accessor" logic that can support a variety of different template input models, including lazy-loading maps, reflection, fallback resolution, etc.
  • Configurable caching for parsed templates, models, and accessors
  • Allowing custom tags to produce per-node instances that do calculations up-front where possible
  • Expanded nested RenderContext to a more formal stack/frame so tags can interact with other tags without polluting the variable space. This could also could support some level of variable shading, though I don't know if the liquid "spec" really defines much about that
  • AssertJ assertions. They allow this type of syntax (this uses kotlin extension functions):
TemplateFactory.newInstance()
        .parse("{% for i in (1..10000) %}{{ i }}{% endfor %}")
        .rendering(model="{}", settings={
          maxRenderTimeMillis = 1
          executor = Executors.newSingleThreadExecutor()
        }
        .hasRenderError(TimeoutException::class.java)

In java, it would be like this:

Template parsed = TemplateFactory.newInstance()
        .parse("{% for i in (1..10000) %}{{ i }}{% endfor %}");
assertThat(parsed)
         .rendering("{}",  settings-> 
            settings.maxRenderTimeMillis(1)
            settings.executor(Executors.newSingleThreadExecutor())
         )
        .hasRenderError(TimeoutException::class.java)

The fork is at https://github.com/kidgamesclub/Liqp/ - like I said, it's pretty messy right now.

from liqp.

ericmartineau avatar ericmartineau commented on September 21, 2024

Here's a simple test to verify the template above, I'm happy to submit it as a PR:

package liqp.filters;

import static org.hamcrest.core.IsEqual.equalTo;

import liqp.Template;
import liqp.TemplateContext;
import liqp.nodes.LNode;
import liqp.tags.Tag;
import org.antlr.runtime.RecognitionException;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class CustomTagNesting {

  @BeforeClass
  public static void registerTags() {
    Tag.registerTag(new XmlTag("red"));
    Tag.registerTag(new XmlTag("bold"));
    Tag.registerTag(new XmlTag("yellow"));
    Tag.registerTag(new XmlTag("green"));
  }

    @Test
    public void applyTest() throws RecognitionException {

      final String rendered = Template.parse("{%red%}{%bold%}Red and {%yellow%}yellow{%endyellow%} and " +
            "{%green%}green{%endgreen%}{%endbold%}{%red%}")
            .render("{}");

      Assert.assertThat(rendered, equalTo("<red><bold>Red and <yellow>yellow</yellow> and " +
            "<green>green</green></bold></red>"));
    }

    public static class XmlTag extends Tag {

      public XmlTag(String tag) {
        super(tag);
      }

      @Override
      public Object render(TemplateContext context, LNode... nodes) {
        final StringBuilder output = new StringBuilder();
        output.append("<").append(this.name).append(">");
        for (LNode node : nodes) {
          output.append(node.render(context));
        }
        output.append("</").append(this.name).append(">");
        return output.toString();
      }
    }
}

The test fails with this output:

java.lang.AssertionError: 
Expected: "<red><bold>Red and <yellow>yellow</yellow> and <green>green</green></bold></red>"
     got: "<red><bold>Red and <yellow>yellow</yellow> and <green></green>green</bold></red><red></red>"

Expected :"<red><bold>Red and <yellow>yellow</yellow> and <green>green</green></bold></red>"
Actual   :"<red><bold>Red and <yellow>yellow</yellow> and <green></green>green</bold></red><red></red>"

from liqp.

ericmartineau avatar ericmartineau commented on September 21, 2024

Yes. I'm an idiot. My template was wrong.

from liqp.

bkiers avatar bkiers commented on September 21, 2024

😄

Good to see you have it sorted!

from liqp.

bkiers avatar bkiers commented on September 21, 2024

Nice, Eric! Thanks!

from liqp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.