Comments (7)
Well, you can inspect the AST. See Mustache::parse
and MustacheAST::toArray
.
from php-mustache.
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.
(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.
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.
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.
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.
Yes, it does, and flags comes from the enum below it.
from php-mustache.
Related Issues (18)
- What's licence for code
- Code coverage HOT 1
- Can't store MustacheTemplate in APC HOT 22
- PHP7: php-mustache do not pass "make test" (segfaults) HOT 12
- Segmentation fault when $data contains objects HOT 7
- Error when building HOT 1
- Strange error "free(): invalid pointer" HOT 6
- Allow ArrayAccess for partials HOT 5
- Improve install instructions HOT 4
- crash when HTML is inside tags HOT 3
- Segfault on Fedora 28 HOT 3
- Error compiling on OS X HOT 2
- Deprecate and/or remove VM HOT 2
- pecl install error HOT 3
- PHP 7.4 support
- Memory Leak with PHP-CLI HOT 4
- Test against PHP7 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-mustache.