Code Monkey home page Code Monkey logo

Comments (7)

jbboehr avatar jbboehr commented on May 28, 2024

Well, you can inspect the AST. See Mustache::parse and MustacheAST::toArray.

from php-mustache.

jbboehr avatar jbboehr commented on May 28, 2024

Or do you mean accessible from the template itself? In that case, you could use a lambda which receives the content of it's block unexecuted. Just note that lambdas are not supported using compile, only render. They're about the same speed anyway IIRC, so nbd.

from php-mustache.

renoirb avatar renoirb commented on May 28, 2024

(Writing quickly from a phone, I will miss exact references, hopefully this'll be still expand my point)

I'm asking because I did not see --nor have enough experience reading C-- to have a mental model of other possiblilties outside of the tests.

I see that MustacheAST has a method that returns an array, where each members has a type property with an i teger.

I assume that means one integer means comment and I could get that string payload. I guess I could walk the array recursively from the top of the AST, and if I se (and follow) the convention of having a comment first, i'd be able to get that string, pass it to a yaml parser, and so on.

That being said, writing this use-case makes me realize that i'd better leverage opcache (and/and APCu) once I've done the work of mangling with the templates files.

Which leads me to think that if I just walk a folder tree, manipulate files in some way, dfigure out metadata, "hydrate" the mustache template string into a mustache class, serialize all this work for next request.

Using exclusively mustache may be stretching this a bit too far. And would't get benefits because i'll have a "cache warmer" system to have in-memory mustache templates (you classes do have __sleep and looks serializable 👍

So, maybe I could contribute to tests here and see if I could use comments and MustacheAST as a data source for template metadata.

Or do it as a Service in my BFF microservice layer.

I'll see what I can assemble and publish publicly once I have something more concrete.

Since this is an unofficial PHP extension that isn't documented lfficially, I rely on tests. Anyway, reading tests is always useful along the code to see what's possible.

from php-mustache.

renoirb avatar renoirb commented on May 28, 2024

For more context, I'm building a PHP microservice that'll serve over HTTP (with appropriate Cache-Control, and so on) representation of a folder of templates.

The template input, would ideally be a directory tree of Mustache files with self contained description.

The container sill have a in-memory (e.g. tmpfs, and/or in RAM) of the metadata I wanna build from the files in that folder.

In the end, it should do things similar to

https://github.com/zalando/tailor/blob/master/examples/fragment-performance/fragment.js

Where I'll have Node.js (and Tailor, or BigPipe, or Fractal, or ...) do HTtP/2 streaming layout assembly from a normalized data source.

from php-mustache.

jbboehr avatar jbboehr commented on May 28, 2024

Since this is an unofficial PHP extension that isn't documented lfficially, I rely on tests. Anyway, reading tests is always useful along the code to see what's possible.

There is some basic documentation in the php stub: https://github.com/jbboehr/php-mustache/blob/master/mustache.php

(you classes do have __sleep and looks serializable

You might want to benchmark serialization vs reparsing the template. Mustache has very simple syntax, it may not be slower to just reparse.

If you need to extract the data separate from execution, I would go with either comments and walking the AST or preprocessing the template.

If you only need to extract the data after execution, then lambdas might be a good choice.

from php-mustache.

renoirb avatar renoirb commented on May 28, 2024

To double-check, is the ::tokenize() array outcome, for each type property comes from enum Type { ...} in jbboehr/libmustache.

Could it be possible to export to PHP’s MustacheAST class as a constant. For instance, MustacheAST::NODE_COMMENT using same int value as libmustache's Note::TypeComment’s.

And all other Node constant integers to the PHP class?

That would be nice if we could

// Pseudo code, I haven't worked with this, yet.
foreach($tokenized as $node) {
  if ($node['type'] === \MustacheAST::NODE_COMMENT) {
    /* $node['data'] should be Node’s comment contents  */
  }
}

Because, so far, when I look at this data structure shown from the tests, I have no idea what flags, and type should be, and in what context.

from php-mustache.

jbboehr avatar jbboehr commented on May 28, 2024

Yes, it does, and flags comes from the enum below it.

from php-mustache.

Related Issues (18)

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.