Comments (4)
Thank you for the feedback! Glad you're finding smpl useful :)
At the moment, the way the evaluation is implemented leaves no space for isolated parsing, so it's currently not possible to do what you're asking about.
Let me know if you'd have any interest in contributing to the project, potentially by rewriting the parser :)
from php-smplang.
on a second thought, if you don't care about performance, you could pass a var by reference through the constructor, and then run the same expression as many times as you'd like. for example:
$smpl = new \Le\SMPLang\SMPLang([ 'name' => &$name ]);
$expression = 'name == "foo"';
$name = 'bar';
var_dump($smpl->evaluate($expression)); // false
$name = 'foo';
var_dump($smpl->evaluate($expression)); // true
this of course still means you have to have both the expression string and the smpl object accessible where ever you'd like to run an evaluation
edit:
why not store the expression as one of the vars within the smpl object? then the expression string doesn't have to be accessible where you want to evaluate (of course we're still heavily ignoring performance implications):
$smpl = new \Le\SMPLang\SMPLang([ 'expression' => 'name == "foo"' ]);
$smplExec = fn ($vars) => $smpl->evaluate((fn() => $this->vars['expression'])->call($smpl), $vars);
var_dump($smplExec(['name' => 'bar'])); // false
var_dump($smplExec(['name' => 'foo'])); // true
here i created a wrapper $smplExec
that takes uses a bit hacky way to access the protected property vars and the expression saved as a var within.
second edit:
if you decide to do this it would probably make a lot more sense to just use smpl to get the expression out, like so:
$smpl = new \Le\SMPLang\SMPLang([ 'expression' => 'name == "foo"' ]);
var_dump($smpl->evaluate($smpl->evaluate('expression'), ['name' => 'bar'])); // false
var_dump($smpl->evaluate($smpl->evaluate('expression'), ['name' => 'foo'])); // true
sorry, at this point this is probably very unhelpful, but i have no idea where to start on rewriting the parser and this is way too fun
from php-smplang.
hi,
yes the purpose of "storing" the parsed expression would be performance :) i'd like to include it for my template project - a vuejs like single-file-component (sfc) template engine. on the other hand, i should probably not care about performance at all at this early stage. so i will have a deeper look at it and try it out. thinking about it this morning... at the end i would just need a translation to a php code string like for example:
expr string: start_date>now() && user.has_access(["future", "preview"])
php string: ($context->get_var("start_date") > $context->call_fun("now") &&
$context->call_fun("user.has_access", [["future", "preview"]]))
and this would probably be quite easy to add (i'm a bit naive haha)
from php-smplang.
I see, translating expressions to PHP code sounds really interesting, but I also very much like the idea to separate the parsing and evaluation steps into reusable/stateless objects (?).
i'd like to include it for my template project - a vuejs like single-file-component (sfc) template engine
Take a look at Dataplater, it's a template engine I made that also uses SMPL. Sounds at first very similar to what you're building - hope you'll find it interesting if not useful :)
from php-smplang.
Related Issues (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-smplang.