Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading partials from memory #20

Closed
rstacruz opened this issue Feb 11, 2017 · 3 comments
Closed

Reading partials from memory #20

rstacruz opened this issue Feb 11, 2017 · 3 comments
Labels

Comments

@rstacruz
Copy link

I'd love to be able to have all my templates as strings in a plain object, something like:

templates = {
  'show.html': `
    <div>
      {% include 'menu.html' %}
      {% include 'body.html' %}
    </div>
  `,
  'menu.html': '...',
  'body.html': '...'
}

Is this possible with shopify-liquid? I understand I can simply do parseAndRender(templates['show.html'], { ... }), but is there a way I can get the partials/includes working as well?

@harttle
Copy link
Owner

harttle commented Feb 11, 2017

In short, yes. include and layout are registered as tags, so you can implement your own include/layout tags and register them into shopify-liquid.

Note: In your implementation, you need replace liquid.getTemplate (which deals with file lookup and parsing) with liquid.parse and pass in your template string.

@stewartknapman
Copy link

@harttle I've implemented my own include tag to handle keeping templates in an object (I'm actually parsing the template when I add it to the object and have been storing that), but I always seem to run into issues with this when I update LiquidJS. So I wanted to add to this question as a sanity check.

The render function looks like this:

    render: function (scope, hash) {
      var tmpl = _this.templates[this.value];
      return _this.engine
        .render(tmpl.template, scope.contexts[0])
        .then(function (html) {
          return html;
        });
    }

But I'm having issues with passing the correct data to the render function.
Passing scope or scope.contexts doesn't allow any of the global variables, or variables assigned in the same template as the include is called, to work inside that include.

Passing scope.contexts[0] works with the global variables, but doesn't account for anything assigned inside that template i.e. foo is not available inside my_snippet given the template below

{% assign foo = 'bar' %}
{% include 'my_snippet' %}

So what's the correct thing I should be passing here? Or should I just create a new object that contains everything from scope.contexts?

This also doesn't take into account variables passed to it by with or with named arguments.

@stewartknapman
Copy link

@harttle Ahhh, never mind, I think I got it. scope.getAll() gives me what I need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants